Elasticsearch 1.1 更多搜索技巧
2016-12-27 14:54:10    107    0    0
admin

这里详细介绍Elasticsearch 的query

一些小技巧

设置timeout 为1分钟

  1. curl localhost:9200/index/type/id?timeout=1m

pretty 使显示的json 更整齐,可读

  1. curl localhost:9200/index/type/id?pretty

human 使一些信息更可读,(单位上的更可读,如ls -lh )

  1. curl localhost:9200/index/type/id?human

显示java的详细报错

  1. curl localhost:9200/index/type/id?error_trace

定制输出的内容(filter)

只显示_nodes.total,cluster_name,**.successful,nodes.*.timestamp

  1. curl 'localhost:9200/_nodes/stats?pretty&filter_path=_nodes.total,cluster_name,**.successful,nodes.*.timestamp'
  2. #其中* 可以补全、匹配任何field, ** 可以匹配任何路径(这里指xx.xx.xx.field)

只显示source

  1. curl 'localhost:9200/index/type/id/_source'

想要显示source 中哪些field

  1. curl 'localhost:9200/xx/xx/xx?pretty&_source_include=xx&_source_exclude=xx'
  2. curl 'localhost:9200/index/type/id?pretty&_source=xx,xx'

常用query

匹配所有 match_all

  1. GET /index/_search
  2. {
  3. "query": { "match_all": {} }, #匹配所有
  4. "_source": ["field1", "field2"], #只显示field1 field2
  5. "size": 100, #列出100个,默认10
  6. "from": 10, #从第10个开始显示,默认是0,用于分页
  7. "sort": { "field": "desc" } } #对某个field 来排序
  8. }

匹配某些字符串,会分词 match

  1. GET /index/_search
  2. {
  3. "query": { "match": { "field": "word1 word2" } } #搜索field 中包含word1 或者word2 的文档
  4. }
  1. GET /index/_search
  2. {
  3. "query": { "match": { "field": 1 } } #搜索field = 1 的文档
  4. }

匹配某些字符串,不会分词 match_phrase

  1. GET /index/_search
  2. {
  3. "query": { "match_phrase": { "field": "word1 word2" } } #搜索field 中包含"word1 word2"的文档
  4. }

复合查询 bool

  1. GET /index/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must/should": [ #must 表示&&,要都满足才匹配;should 表示||,有一个满足就可以了
  6. { "match": { "field1": "word1" } },
  7. { "match": { "field2": "word2" } }
  8. ],
  9. "must_not": [ #必须都不满足
  10. { "match": { "field3": "word3" } },
  11. { "match": { "field4": "word4" } }
  12. ],
  13. "filter": {
  14. "range": { #拿 10 <= field5 <= 30 的
  15. "field5": {
  16. "gte": 10,
  17. "lte": 30
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }

上一篇: Elasticsearch 1.0 简单的增删改查

下一篇: Elasticsearch 1.2 多库同时搜索

文档导航