ElasticSearch学习九 文档查询

打印 上一主题 下一主题

主题 858|帖子 858|积分 2574

九、文档的查询

Elasticsearch提供了基于JSON的DSL(Domain Specific Language)来定义查询。常见的查询类型包括。

  • 查询所有:查询出所有数据,一般测试用。例如:match_all
  • 全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。例如:
    match_query
    multi_match_query
  • 精确查询:根据精确词条值查找数据,一般是查找keyword、数值、日期、boolean等类型字段。例如:
    ids
    range
    term
  • 地理(geo)查询:根据经纬度查询。例如:
    geo_distance
    geo_bounding_box
  • 复合(compound)查询:复合查询可以将上述各种查询条件组合起来,合并查询条件。例如:
    bool
    function_score
9.1、term查询

Elasticsearch默认采用的是standard,这个分词器是一个字一个词,例如我们在person这个索引里面有以下几条数据
  1. {
  2.   "took" : 49,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 4,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : 0.105360515,
  16.     "hits" : [
  17.       {
  18.         "_index" : "person",
  19.         "_type" : "_doc",
  20.         "_id" : "1",
  21.         "_score" : 0.105360515,
  22.         "_source" : {
  23.           "name" : "张三",
  24.           "age" : 30,
  25.           "address" : "杭州市滨江区"
  26.         }
  27.       },
  28.       {
  29.         "_index" : "person",
  30.         "_type" : "_doc",
  31.         "_id" : "e0DxjYABtRuVRBuRfJYL",
  32.         "_score" : 0.105360515,
  33.         "_source" : {
  34.           "name" : "李四",
  35.           "age" : 30,
  36.           "address" : "杭州市滨江区"
  37.         }
  38.       },
  39.       {
  40.         "_index" : "person",
  41.         "_type" : "_doc",
  42.         "_id" : "fEDxjYABtRuVRBuRg5aL",
  43.         "_score" : 0.105360515,
  44.         "_source" : {
  45.           "name" : "李四",
  46.           "age" : 30,
  47.           "address" : "杭州市滨江区"
  48.         }
  49.       },
  50.       {
  51.         "_index" : "person",
  52.         "_type" : "_doc",
  53.         "_id" : "4",
  54.         "_score" : 0.105360515,
  55.         "_source" : {
  56.           "name" : "李四",
  57.           "age" : 30,
  58.           "address" : "杭州市滨江区"
  59.         }
  60.       }
  61.     ]
  62.   }
  63. }
复制代码
 
采用term进行查询
  1. GET person/_search
  2. {
  3.   "query": {
  4.     "term": {
  5.       "address": {
  6.         "value": "杭州"
  7.       }
  8.     }
  9.   }
  10. }
复制代码
 
返回的结果
  1. {
  2.   "took" : 47,
  3.   "timed_out" : false,
  4.   "_shards" : {
  5.     "total" : 1,
  6.     "successful" : 1,
  7.     "skipped" : 0,
  8.     "failed" : 0
  9.   },
  10.   "hits" : {
  11.     "total" : {
  12.       "value" : 0,
  13.       "relation" : "eq"
  14.     },
  15.     "max_score" : null,
  16.     "hits" : [ ]
  17.   }
  18. }
复制代码
 
任何词条都没有查询到。
 
9.2、创建指定分词器的索引

创建索引
  1. put http://192.168.2.128:9200/person
  2. //入参
  3. {
  4.     "mappings":{
  5.         "properties":{
  6.             "name":{
  7.                 "type":"keyword"
  8.             },
  9.             "address":{
  10.                 "type":"text",
  11.                 "analyzer":"ik_max_word"
  12.             }
  13.         }
  14.     }
  15. }
  16. //结果
  17. {
  18.     "acknowledged": true,
  19.     "shards_acknowledged": true,
  20.     "index": "person"
  21. }
复制代码
 
查询索引

查询指定分词器的索引
  1. get http://192.168.2.128:9200/person
  2. // 运行结果
  3. {
  4.     "person": {
  5.         "aliases": {},
  6.         "mappings": {
  7.             "properties": {
  8.                 "address": {
  9.                     "type": "text",
  10.                     "analyzer": "ik_max_word" //address字段使用ik分词器进行分词
  11.                 },
  12.                 "name": {
  13.                     "type": "keyword"
  14.                 }
  15.             }
  16.         },
  17.         "settings": {
  18.             "index": {
  19.                 "creation_date": "1651665740384",
  20.                 "number_of_shards": "1",
  21.                 "number_of_replicas": "1",
  22.                 "uuid": "nYelz6sgQMWpcNEt-hrGUA",
  23.                 "version": {
  24.                     "created": "7040299"
  25.                 },
  26.                 "provided_name": "person"
  27.             }
  28.         }
  29.     }
  30. }
复制代码
 
添加文档
  1. POST http://192.168.2.128:9200/person/_doc/1
  2. {
  3. "name" : "李四",
  4.   "address" : "杭州市滨江区"
  5. }
  6. POST http://192.168.2.128:9200/person/_doc/2
  7. {
  8. "name" : "李四",
  9.   "address" : "北京市朝阳区"
  10. }   
复制代码
 
分词查询
  1. GET http://192.168.2.128:9200/person/_search
  2. //入参
  3. {
  4.   "query": {
  5.     "term": {
  6.       "address": {
  7.         "value": "北京"
  8.       }
  9.     }
  10.   }
  11. }
  12. //结果
  13. {
  14.   "took" : 758,
  15.   "timed_out" : false,
  16.   "_shards" : {
  17.     "total" : 1,
  18.     "successful" : 1,
  19.     "skipped" : 0,
  20.     "failed" : 0
  21.   },
  22.   "hits" : {
  23.     "total" : {
  24.       "value" : 1,
  25.       "relation" : "eq"
  26.     },
  27.     "max_score" : 0.6931472,
  28.     "hits" : [
  29.       {
  30.         "_index" : "person",
  31.         "_type" : "_doc",
  32.         "_id" : "2",
  33.         "_score" : 0.6931472,
  34.         "_source" : {
  35.           "name" : "李四",
  36.           "address" : "北京市朝阳区"
  37.         }
  38.       }
  39.     ]
  40.   }
  41. }
复制代码
 
9.3、match查询

match会对查询条件进行分词,然后匹配的结果的并集返回。
  1. GET person/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "address": "北京杭州"
  6.     }
  7.   }
  8. }
  9. //查询结果
  10. {
  11.   "took" : 736,
  12.   "timed_out" : false,
  13.   "_shards" : {
  14.     "total" : 1,
  15.     "successful" : 1,
  16.     "skipped" : 0,
  17.     "failed" : 0
  18.   },
  19.   "hits" : {
  20.     "total" : {
  21.       "value" : 2,
  22.       "relation" : "eq"
  23.     },
  24.     "max_score" : 0.6931472,
  25.     "hits" : [
  26.       {
  27.         "_index" : "person",
  28.         "_type" : "_doc",
  29.         "_id" : "1",
  30.         "_score" : 0.6931472,
  31.         "_source" : {
  32.           "name" : "李四",
  33.           "address" : "杭州市滨江区"
  34.         }
  35.       },
  36.       {
  37.         "_index" : "person",
  38.         "_type" : "_doc",
  39.         "_id" : "2",
  40.         "_score" : 0.6931472,
  41.         "_source" : {
  42.           "name" : "李四",
  43.           "address" : "北京市朝阳区"
  44.         }
  45.       }
  46.     ]
  47.   }
  48. }
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

耶耶耶耶耶

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表