总结一下ElasticSearch中的常用查询
查询
curl 'http://localhost:9200/indexName/typeName/docID?pretty
用于查询指定ID的文档内容
curl 'http://localhost:9200/indexName/typeName/_search?pretty&size=10
用于查询指定索引,指定类型下的文档,使用size参数指定返回文档数量,默认返回10条
curl 'http://localhost:9200/indexName/typeName/_search?pretty&size=10' -d '{"query":{"match":{"fieldName":"value"}}}'
用于查询文档中fieldName指定的字段匹配指定value的文档,注意这里的value是可能被分词的
合并查询条件
{"query":{"constant_score":{"filter":{"bool":{"must":[{"term":{"account_id":524055}},{"term":{"action":"download file"}}]}}}}}
{"query":{"constant_score":{"filter":{"bool":{"must":[{"term":{"user_id":1365565}},{"term":{"nsid":1064549}},{"term":{"neid":914594193}}]}}}}}
{"query":{"bool":{"must_not":{"exists":{"field":"path"}}}}}
{"query": { "bool": { "must": { "match": { "tweet": "elasticsearch" }}, "must_not": { "match": { "name": "mary" }}, "should": { "match": { "tweet": "full text" }}, "filter": { "range": { "age" : { "gt" : 30 }} } } }}
{"query":{
"bool": {
"must": { "match": { "email": "business opportunity" }},
"should": [
{ "match": { "starred": true }},
{ "bool": {
"must": { "match": { "folder": "inbox" }},
"must_not": { "match": { "spam": true }}
}}],
"minimum_should_match": 1
}
}}
前缀查询,不会分析查询字符串 '{"query":{"prefix":{"postcode": "W1"}}}'
排序 '{"sort":{"age":{"order":"desc"}}}'
聚集 '{"size":0,"aggs":{"total_count":{"terms":{"field":"account_id"}}}}'
查看索引列表
curl 'http://localhost:9200/_cat/indices'
重建索引
{"size":5000,"conflicts":"proceed", "source":{"index":"log"}, "dest":{"index":"log_v2", "op_type":"create","routing":"keep"}}
curl -XPOST 'http://localhost:9200/_reindex?pretty' -d
'{"size":5000,"conflicts":"proceed", "source":{"index":"log"}, "dest":{"index":"log_v2", "op_type":"create","routing":"keep"}}'
curl -XPOST 'http://localhost:9200/_reindex?pretty' -d
'{"conflicts":"proceed", "source":{"index":"log","size":5000}, "dest":{"index":"log_v2", "op_type":"create","routing":"keep"}}'
创建别名
curl -XPUT 'http://localhost:9200/log/_alias/log_v2'
删除别名
curl -XDELETE 'http://localhost:9200/log/_alias/log_v2'
查看别名
方式一:curl 'http://localhost:9200/*/_alias/log'
方式二:curl 'http://localhost:9200/log/_alias/*'
查看es当前执行的任务列表
curl 'http://localhost:9200/_tasks?pretty'
查看某一个任务执行详细信息
curl 'http://localhost:9200/_tasks/k0xbXNGRT0uTNQAlJJixqw:1243709676?pretty'
需求某一账户,所有子账户,最后登录时间?
先按条件过滤,然后按 user id 分组,最后求每组最大值
curl 'http://localhost:9200/log/_search?pretty' -d
'{"size":0, "query":{"bool":{"must":[{"term":{"account_id":123456}}, {"term":{"action":"login"}}]}},
"aggs":{"users":{"terms":{"field":"user_id", "size":20}, "aggs":{"time":{"max":{"field":"@timestamp"}}}}}}'
更新
curl 'http://localhost:9200/indexName/type/docID/_update?pretty' -d '{"doc": {"addNewFieldName": "value"}}'
用于更新ID为docID的文档,新增字段;