elasticsearch(4)查询DSL

亦凉 2022-05-13 02:04 323阅读 0赞

全部搜索

  1. GET /index/type/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

例如:

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

分页搜索

  1. GET /index/type/_search
  2. {
  3. "query": {
  4. xxx
  5. },
  6. "from": 0,
  7. "size": 10
  8. }

例如:

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "from": 0,
  7. "size": 10
  8. }

搜索返回指定字段

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. xxx
  5. },
  6. "_source": ["key1","key2"]
  7. }

例如:

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "_source": ["name","price"]
  7. }

指定字段分词搜索

  1. GET /index/type/_search
  2. {
  3. "query": {
  4. "match": {
  5. "key1": "value1"
  6. }
  7. }
  8. }

例如:

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "牙膏"
  6. }
  7. }
  8. }

查询排序

  1. GET /index/type/_search
  2. {
  3. "query": {
  4. xxx
  5. },
  6. "sort": [
  7. {
  8. "key1": {
  9. "order": "asc|desc"
  10. }
  11. }
  12. ]
  13. }

例如:

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "price": {
  9. "order": "desc"
  10. }
  11. }
  12. ]
  13. }

查询filter

filter不涉及积分计算,因此filter的效率比普通查询的效率要高。

例如:

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "match":{
  7. "name":"牙膏"
  8. }
  9. },
  10. "filter": {
  11. "range": {
  12. "price": {
  13. "gt": 10,
  14. "lte": 30
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }

查询phrase

跟match全文检索相反,全文检索会将搜索输入分词拆解,去倒排索引中一一匹配,只要有一个分词能在倒排索引中匹配上,就可以作为结果返回。

phrase不会将搜索输入分词,必须在指定字段值中完全包含搜索输入才算搜索匹配,作为结果返回。

例如:在match全文检索模式下,高露洁可以匹配上高露洁牙膏佳洁士牙膏;在phrase模式下,可以匹配上高露洁牙膏,但匹配不上佳洁士牙膏

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "name": "高露洁"
  6. }
  7. }
  8. }

查询高亮

  1. GET /index/type/_search
  2. {
  3. "query": {
  4. xxx
  5. },
  6. "highlight": {
  7. "fields": {
  8. "key1": {}
  9. }
  10. }
  11. }

例如:

requset

  1. GET /ecommerce/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "高露洁"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "name": {}
  11. }
  12. }
  13. }

response

  1. {
  2. "took": 6,
  3. "timed_out": false,
  4. "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 },
  5. "hits": { "total": 2, "max_score": 0.8630463, "hits": [ { "_index": "ecommerce", "_type": "product", "_id": "2", "_score": 0.8630463, "_source": { "name": "高露洁牙膏", "price": 20 }, "highlight": { "name": [ "<em>高</em><em>露</em><em>洁</em>牙膏" ] } }, { "_index": "ecommerce", "_type": "product", "_id": "1", "_score": 0.2876821, "_source": { "name": "佳洁士牙膏", "price": 30 }, "highlight": { "name": [ "佳<em>洁</em>士牙膏" ] } } ] } }

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读