Asp.Net.Core log4net 写入Elasticsearch demo地址
github:https://github.com/foreverhot1019/AutoMapper_GZipTest
Elasticsearch REST APIs
ContentType:application/json
my_index:index名称
logType:索引类型
docId:自定义的文档Id
1.创建Index
put http://localhost:9200/my_index
创建字段mapping及字段类型(也可不传,传文档时es会自动创建)
{
"mappings" : {
"properties" : {
"user_id" : {"type" : "integer"},
"date":{"type":"date","format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"}
}
}
}
put http://localhost:9200/my_index?include_type_name=true
参数:include_type_name=true 必须带上,不然es不让手动创建_type
带自定义_type,创建Index
logType:t_michael
{
"settings" : {
"number_of_shards" : 2
},
"mappings" : {
"t_michael":{
"properties" : {
"user_id" : {"type" : "integer"},
"action":{"type":"text"},
"Message":{"type":"text"},
"@date":{"type":"date","format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"}
}
}
}
}
2.上传文档
post http://localhost:9200/my_index/logType/docId(post json文档数据,logId不传:自动产生文档Id)
put http://localhost:9200/my_index/logType/docId
结合 put http://localhost:9200/my_index 不创建任何type和mapping时
自动设置_type为参数logType和docId
{
"user_id" : 1456,
"action":"action1456Test",
"Message":"1测试数据456Message",
"Exception":"1456Nullable exception",
"StackTrace":"1456asdasdagafas adasda-------ab asdasasd",
"@date": "2020/09/25 10:15:03"
}
3.查看文档
get http://localhost:9200/my_index/logType/docId
get http://localhost:9200/my_index/logType/docId/_source (直接返回source)
获取Index下所有文档
get http://localhost:9200/my_index/_search
_source:文档source
query:搜索条件
{
"_source": false,
"query" : {
"match_all" : {}
}
}
4.修改文档
post http://localhost:9200/my_index/_update/docId
注:在 Elasticsearch 中文档是 不可改变 的,不能修改它们。相反,如果想要更新现有的文档,需要 重建索引 或者进行替换。在内部,Elasticsearch 已将旧文档标记为已删除,并增加一个全新的文档。 尽管你不能再对旧版本的文档进行访问,但它并不会立即消失。当继续索引更多的数据,Elasticsearch 会在后台清理这些已删除文档。
_update:不能变
内容 必须在 "doc" 节点后面,只会更新部分数据
{
"doc": {
"date":"2020/07/23 16:23:01"
}
}
还可执行Groovy 脚本
{
"script": {
"source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }",
"lang": "painless",
"params": {
"tag": "blue"
}
}
}
参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
5:删除文档
DELETE http://localhost:9200/my_index/logType/docId