这里详细介绍Elasticsearch 的query
一些小技巧
设置timeout 为1分钟
curl localhost:9200/index/type/id?timeout=1m
pretty 使显示的json 更整齐,可读
curl localhost:9200/index/type/id?pretty
human 使一些信息更可读,(单位上的更可读,如ls -lh )
curl localhost:9200/index/type/id?human
显示java的详细报错
curl localhost:9200/index/type/id?error_trace
定制输出的内容(filter)
只显示_nodes.total,cluster_name,**.successful,nodes.*.timestamp
curl 'localhost:9200/_nodes/stats?pretty&filter_path=_nodes.total,cluster_name,**.successful,nodes.*.timestamp'
#其中* 可以补全、匹配任何field, ** 可以匹配任何路径(这里指xx.xx.xx.field)
只显示source
curl 'localhost:9200/index/type/id/_source'
想要显示source 中哪些field
curl 'localhost:9200/xx/xx/xx?pretty&_source_include=xx&_source_exclude=xx'
curl 'localhost:9200/index/type/id?pretty&_source=xx,xx'
常用query
匹配所有 match_all
GET /index/_search
{
"query": { "match_all": {} }, #匹配所有
"_source": ["field1", "field2"], #只显示field1 field2
"size": 100, #列出100个,默认10
"from": 10, #从第10个开始显示,默认是0,用于分页
"sort": { "field": "desc" } } #对某个field 来排序
}
匹配某些字符串,会分词 match
GET /index/_search
{
"query": { "match": { "field": "word1 word2" } } #搜索field 中包含word1 或者word2 的文档
}
GET /index/_search
{
"query": { "match": { "field": 1 } } #搜索field = 1 的文档
}
匹配某些字符串,不会分词 match_phrase
GET /index/_search
{
"query": { "match_phrase": { "field": "word1 word2" } } #搜索field 中包含"word1 word2"的文档
}
复合查询 bool
GET /index/_search
{
"query": {
"bool": {
"must/should": [ #must 表示&&,要都满足才匹配;should 表示||,有一个满足就可以了
{ "match": { "field1": "word1" } },
{ "match": { "field2": "word2" } }
],
"must_not": [ #必须都不满足
{ "match": { "field3": "word3" } },
{ "match": { "field4": "word4" } }
],
"filter": {
"range": { #拿 10 <= field5 <= 30 的
"field5": {
"gte": 10,
"lte": 30
}
}
}
}
}
}