开发手册 欢迎您!
软件开发者资料库

Elasticsearch - 查询DSL

Elasticsearch查询DSL - 从基本概念,安装,填充弹性搜索,版本之间的迁移,API约定,文档API,搜索API,聚合,索引API,群集API,查询DSL,映射,分析,模块开始,从简单而简单的步骤学习Elasticsearch ,测试。

在Elasticsearch中,使用基于JSON的查询进行搜索.查询由两个子句组成 :

  • 叶子查询条款 : 这些子句是匹配,术语或范围,用于在特定字段中查找特定值.

  • 复合查询子句 : 这些查询是叶子查询子句和其他复合查询的组合,用于提取所需信息.

Elasticsearch支持大量查询.查询以查询关键字开头,然后以JSON对象的形式包含条件和过滤器.下面已经描述了不同类型的查询 :

匹配所有查询

这是最基本的查询;它返回所有内容,每个对象的得分为1.0.例如,

POST http://localhost:9200/schools */_ search

请求正文

{   "query":{      "match_all":{}   }}

回复

{   "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},   "hits":{      "total":5, "max_score":1.0, "hits":[         {            "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,            "_source":{               "name":"Saint Paul School", "description":"ICSE Affiliation",               "street":"Dawarka", "city":"Delhi", "state":"Delhi",                "zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000,                "tags":["Good Faculty", "Great Sports"], "rating":"4.5"            }         },         {            "_index":"schools_gov", "_type":"school", "_id":"2", "_score":1.0,            "_source":{               "name":"Government School", "description":"State Board Affiliation",               "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",               "location":[18.599752, 73.6821995], "fees":500, "tags":["Great Sports"],               "rating":"4"            }         },         {            "_index":"schools", "_type":"school", "_id":"1", "_score":1.0,            "_source":{               "name":"Central School", "description":"CBSE Affiliation",               "street":"Nagan", "city":"paprola", "state":"HP",                "zip":"176115", "location":[31.8955385, 76.8380405],                "fees":2200, "tags":["Senior Secondary", "beautiful campus"],                "rating":"3.3"            }         },         {            "_index":"schools_gov", "_type":"school", "_id":"1", "_score":1.0,            "_source":{               "name":"Model School", "description":"CBSE Affiliation",               "street":"silk city", "city":"Hyderabad", "state":"AP",                "zip":"500030", "location":[17.3903703, 78.4752129], "fees":700,                "tags":["Senior Secondary", "beautiful campus"], "rating":"3"            }         },         {            "_index":"schools", "_type":"school", "_id":"3", "_score":1.0,            "_source":{               "name":"Crescent School", "description":"State Board Affiliation",               "street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114",               "location":[26.8535922, 75.7923988], "fees":2500,                "tags":["Well equipped labs"], "rating":"4.5"            }         }      ]   }}

全Te xt查询

这些查询用于搜索整篇文章,如章节或新闻文章.此查询根据与该特定索引或文档关联的分析器工作.在本节中,我们将讨论不同类型的全文查询.

匹配查询

此查询将文本或短语与值匹配一个或多个字段.例如,

POST http://localhost:9200/schools*/_search

Request Body

{   "query":{      "match" : {         "city":"pune"      }   }}

响应

{   "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},   "hits":{      "total":1, "max_score":0.30685282, "hits":[{         "_index":"schools_gov", "_type":"school", "_id":"2", "_score":0.30685282,          "_source":{            "name":"Government School", "description":"State Board Afiliation",            "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",            "location":[18.599752, 73.6821995], "fees":500,             "tags":["Great Sports"], "rating":"4"         }      }]   }}

multi_match query

此查询匹配具有多个字段的文本或短语.例如,

POST http://localhost:9200/schools */_ search

请求正文

{   "query":{      "multi_match" : {         "query": "hyderabad",         "fields": [ "city", "state" ]      }   }}

回复

{   "took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},   "hits":{      "total":1, "max_score":0.09415865, "hits":[{         "_index":"schools_gov", "_type":"school", "_id":"1", "_score":0.09415865,         "_source":{            "name":"Model School", " description":"CBSE Affiliation",             "street":"silk city", "city":"Hyderabad", "state":"AP",             "zip":"500030", "location":[17.3903703, 78.4752129], "fees":700,             "tags":["Senior Secondary", "beautiful campus"], "rating":"3"         }      }]   }}

查询字符串查询

此查询使用查询解析器和query_string关键字.例如,

POST http://localhost:9200/schools/_search

请求正文

{   "query":{      "query_string":{         "query":"good faculty"      }   }}

回复

{   "took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},    "hits":{      "total":1, "max_score":0.09492774, "hits":[{         "_index":"schools", "_type":"school", "_id":"2", "_score":0.09492774,          "_source":{            "name":"Saint Paul School", "description":"ICSE Affiliation",            "street":"Dawarka", "city":"Delhi", "state":"Delhi",            "zip":"110075", "location":[28.5733056, 77.0122136],            "fees":5000, "tags":["Good Faculty", "Great Sports"],            "rating":"4.5"          }      }]   }}

术语等级查询

这些查询主要处理结构化数据,如数字,日期和emuns.例如,

POST http://localhost:9200/schools/_search

请求正文

{   "query":{      "term":{"zip":"176115"}   }}

回复

{   "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},   "hits":{      "total":1, "max_score":0.30685282, "hits":[{         "_index":"schools", "_type":"school", "_id":"1", "_score":0.30685282,         "_source":{            "name":"Central School", "description":"CBSE Affiliation",            "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",            "location":[31.8955385, 76.8380405], "fees":2200,             "tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"         }      }]   }}

范围查询

此查询用于查找值在值范围之间的对象.为此,我们需要使用运算符,如 :

  • gte : 大于等于

  • gt : 大于

  • lte : 小于等于

  • lt : 小于

例如,

POST http://localhost:9200/schools */_ search

Request Body

{   "query":{      "range":{         "rating":{            "gte":3.5         }      }   }}

回复

{   "took":31, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},   "hits":{      "total":3, "max_score":1.0, "hits":[         {            "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,            "_source":{               "name":"Saint Paul School", "description":"ICSE Affiliation",               "street":"Dawarka", "city":"Delhi", "state":"Delhi",                "zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000,                "tags":["Good Faculty", "Great Sports"], "rating":"4.5"            }         },          {            "_index":"schools_gov", "_type":"school", "_id":"2", "_score":1.0,             "_source":{               "name":"Government School", "description":"State Board Affiliation",               "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",               "location":[18.599752, 73.6821995] "fees":500,                "tags":["Great Sports"], "rating":"4"            }         },         {            "_index":"schools", "_type":"school", "_id":"3", "_score":1.0,            "_source":{               "name":"Crescent School", "description":"State Board Affiliation",               "street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114",                "location":[26.8535922, 75.7923988], "fees":2500,               "tags":["Well equipped labs"], "rating":"4.5"            }         }      ]   }}

其他术语级别查询的类型是 :

  • 存在查询 : 如果某个字段的值为空值.

  • 缺少查询 : 这与存在查询完全相反,此查询搜索没有特定字段或具有空值的字段的对象.

  • 通配符或正则表达式查询 : 此查询使用正则表达式查找对象中的模式.

键入查询 : 具有特定类型的文件.例如,

POST http://localhost:9200/schools */_ search

Request Body

{   "query":{      "type" : {         "value" : "school"      }   }}

回复

指定索引中存在的所有学校JSON对象.

复合查询

这些查询是彼此合并的不同查询的集合通过使用和,或,或不使用布尔运算符或用于不同的索引或具有函数调用等.例如,

POST http://localhost:9200/schools*/_search

请求正文

{   "query":{      "filtered":{         "query":{            "match":{               "state":"UP"            }         },         "filter":{            "range":{               "rating":{                  "gte":4.0               }            }         }      }   }}

响应

{   "took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},   "hits":{"total":0, "max_score":null, "hits":[]}}

加入查询

这些查询用于包含多个映射或文档的位置.有两种类型的加入查询 :

嵌套查询

这些查询处理嵌套映射(您将在下一章中阅读更多相关信息) ).

has_child和has_parent查询

这些查询用于检索文档的子级或父级,这些查询在查询中匹配.例如,

POST http://localhost:9200/tutorials/_search

请求正文

{   "query":   {      "has_child" : {         "type" : "article", "query" : {            "match" : {               "Text" : "This is article 1 of chapter 1"            }         }      }   }}

回复

{   "took":21, "timed_out":false, "_shards":{"total":5, "successful":5, "failed":0},   "hits":{      "total":1, "max_score":1.0, "hits":[{         "_index":"tutorials", "_type":"chapter", "_id":"1", "_score":1.0,         "_source":{            "Text":"this is chapter one"         }      }]   }}

地理查询

这些查询处理地理位置和地理位置.这些查询有助于找到任何地点附近的学校或任何其他地理对象.您需要使用地理点数据类型.例如,

POST http://localhost:9200/schools */_ search

Request Body

{   "query":{      "filtered":{         "filter":{            "geo_distance":{               "distance":"100km",               "location":[32.052098, 76.649294]            }         }      }   }}

回复

{   "took":6, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},   "hits":{      "total":2, "max_score":1.0, "hits":[         {            "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,            "_source":{               "name":"Saint Paul School", "description":"ICSE Affiliation",               "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075",               "location":[28.5733056, 77.0122136], "fees":5000,               "tags":["Good Faculty", "Great Sports"], "rating":"4.5"            }         },         {            "_index":"schools", "_type":"school", "_id":"1", "_score":1.0,            "_source":{               "name":"Central School", "description":"CBSE Affiliation",               "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",               "location":[31.8955385, 76.8380405], "fees":2000,               "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"            }         }      ]   }}

注意 : 如果在执行上述示例时遇到异常,请将以下映射添加到索引中.

{   "mappings":{      "school":{         "_all":{            "enabled":true         },         "properties":{            "location":{               "type":"geo_point"            }         }      }   }}