ElasticSearch 查询DSL

  • 查询DSL

    在Elasticsearch中,搜索是通过使用基于JSON的查询来进行的。查询由两个子句组成-
    • 叶子查询子句 - 这些子句是匹配项,术语或范围,它们在特定字段中寻找特定值。
    • 复合查询子句 - 这些查询是叶查询子句和其他复合查询的组合,以提取所需的信息。
    Elasticsearch支持大量查询。查询以查询关键字开头,然后以JSON对象的形式包含条件和过滤器。下面描述了不同类型的查询。
  • 匹配所有查询

    这是最基本的查询;它返回所有内容,每个对象的得分为1.0。
    
    POST /school/_search
    {
       "query":{
          "match_all":{}
       }
    }
    
    运行上面的代码,我们得到以下结果-
    
    {
       "took" : 7,
       "timed_out" : false,
       "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
       },
       "hits" : {
          "total" : {
             "value" : 2,
             "relation" : "eq"
          },
          "max_score" : 1.0,
          "hits" : [
             {
                "_index" : "school",
                "_type" : "school",
                "_id" : "5",
                "_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" : "school",
                "_type" : "school",
                "_id" : "4",
                "_score" : 1.0,
                "_source" : {
                   "name" : "City Best School",
                   "description" : "ICSE",
                   "street" : "West End",
                   "city" : "Meerut",
                   "state" : "UP",
                   "zip" : "250002",
                   "location" : [
                      28.9926174,
                      77.692485
                   ],
                   "fees" : 3500,
                   "tags" : [
                      "fully computerized"
                   ],
                   "rating" : "4.5"
                }
             }
          ]
       }
    }
    
  • 全文查询

    这些查询用于搜索全文,例如章节或新闻文章。该查询根据与该特定索引或文档相关联的分析器工作。在本节中,我们将讨论全文查询的不同类型。
    匹配查询
    此查询将文本或短语与一个或多个字段的值匹配。
    
    POST /school*/_search
    {
       "query":{
          "match" : {
             "rating":"4.5"
          }
       }
    }
    
    运行上面的代码后,我们得到如下所示的响应:
    
    {
       "took" : 44,
       "timed_out" : false,
       "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
       },
       "hits" : {
          "total" : {
             "value" : 1,
             "relation" : "eq"
          },
          "max_score" : 0.47000363,
          "hits" : [
             {
                "_index" : "school",
                "_type" : "school",
                "_id" : "4",
                "_score" : 0.47000363,
                "_source" : {
                   "name" : "City Best School",
                   "description" : "ICSE",
                   "street" : "West End",
                   "city" : "Meerut",
                   "state" : "UP",
                   "zip" : "250002",
                   "location" : [
                      28.9926174,
                      77.692485
                   ],
                   "fees" : 3500,
                   "tags" : [
                      "fully computerized"
                   ],
                   "rating" : "4.5"
                }
             }
          ]
       }
    }
    
  • 多个匹配查询

    此查询将文本或短语与多个字段匹配。
    
    POST /school*/_search
    {
       "query":{
          "multi_match" : {
             "query": "paprola",
             "fields": [ "city", "state" ]
          }
       }
    }
    
    运行上面的代码后,我们得到如下所示的响应:
    
    {
       "took" : 12,
       "timed_out" : false,
       "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
       },
       "hits" : {
          "total" : {
             "value" : 1,
             "relation" : "eq"
          },
          "max_score" : 0.9808292,
          "hits" : [
             {
                "_index" : "school",
                "_type" : "school",
                "_id" : "5",
                "_score" : 0.9808292,
                "_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"
                }
             }
          ]
       }
    }
    
  • 查询字符串查询

    该查询使用查询解析器和query_string关键字。
    
    POST /school*/_search
    {
       "query":{
          "query_string":{
             "query":"beautiful"
          }
       }
    }  
    
    运行上面的代码后,我们得到如下所示的响应:
    
    {
        "took": 16,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 1,
                "relation": "eq"
            },
            "max_score": 1.1374959,
            "hits": [
                {
                    "_index": "school",
                    "_type": "_doc",
                    "_id": "5",
                    "_score": 1.1374959,
                    "_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"
                    }
                }
            ]
        }
    }
    
  • 词级查询

    这些查询主要处理结构化数据,例如数字,日期和枚举。
    
    POST /school*/_search
    {
       "query":{
          "term":{"zip":"176115"}
       }
    }
    
    运行上面的代码后,我们得到如下所示的响应:
    
    ……………………………..
    hits" : [
       {
          "_index" : "school",
          "_type" : "school",
          "_id" : "5",
          "_score" : 0.9808292,
          "_source" : {
             "name" : "Central School",
             "description" : "CBSE Affiliation",
             "street" : "Nagan",
             "city" : "paprola",
             "state" : "HP",
             "zip" : "176115",
             "location" : [
                31.8955385,
                76.8380405
             ],
          }
       }
    ]   
    …………………………………………..
    
  • 范围查询

    该查询用于查找具有给定值范围之间的值的对象。为此,我们需要使用运算符,例如-
    • gte - 大于等于
    • gt - 大于
    • lte - 小于等于
    • lt - 小于
    例如,观察下面给出的代码-
    
    POST /school*/_search
    {
       "query":{
          "range":{
             "rating":{
                "gte":3.5
             }
          }
       }
    }
    
    运行上面的代码后,我们得到如下所示的响应:
    
    {
       "took" : 24,
       "timed_out" : false,
       "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
       },
       "hits" : {
          "total" : {
             "value" : 1,
             "relation" : "eq"
          },
          "max_score" : 1.0,
          "hits" : [
             {
                "_index" : "school",
                "_type" : "school",
                "_id" : "4",
                "_score" : 1.0,
                "_source" : {
                   "name" : "City Best School",
                   "description" : "ICSE",
                   "street" : "West End",
                   "city" : "Meerut",
                   "state" : "UP",
                   "zip" : "250002",
                   "location" : [
                      28.9926174,
                      77.692485
                   ],
                   "fees" : 3500,
                   "tags" : [
                      "fully computerized"
                   ],
                   "rating" : "4.5"
                }
             }
          ]
       }
    }
    
    还存在其他类型的术语级别查询,例如-
    • 存在查询 - 如果某个字段具有非null值。
    • 缺少查询 - 这与存在查询完全相反,该查询搜索没有特定字段或字段值为空的对象。
    • 通配符或正则表达式查询 - 此查询使用正则表达式在对象中查找模式。
  • 复合查询

    这些查询是不同查询的集合,这些查询通过使用布尔运算符(例如and/or,或not)或针对不同的索引或具有函数调用等彼此合并。
    
    POST /school/_search
    {
       "query": {
          "bool" : {
             "must" : {
                "term" : { "state" : "Delhi" }
             },
             "filter": {
                "term" : { "fees" : 2200 }
             },
             "minimum_should_match" : 1,
             "boost" : 1.0
          }
       }
    }
    
    运行上面的代码后,我们得到如下所示的响应:
    
    {
       "took" : 6,
       "timed_out" : false,
       "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
       },
       "hits" : {
          "total" : {
             "value" : 0,
             "relation" : "eq"
          },
          "max_score" : null,
          "hits" : [ ]
       }
    }
    
  • 地理查询

    这些查询处理地理位置和地理位置。这些查询有助于找出学校或任何其他地理位置附近的地理对象。您需要使用地理位置数据类型。
    
    PUT /geo_example
    {
       "mappings": {
          "properties": {
             "location": {
                "type": "geo_shape"
             }
          }
       }
    }
    
    运行上面的代码后,我们得到如下所示的响应:
    
    {  "acknowledged" : true,
       "shards_acknowledged" : true,
       "index" : "geo_example"
    }
    
    现在,我们将数据发布到上面创建的索引中。
    
    POST /geo_example/_doc?refresh
    {
       "name": "Chapter One, London, UK",
       "location": {
          "type": "point",
          "coordinates": [11.660544, 57.800286]
       }
    }
    
    运行上面的代码后,我们得到如下所示的响应:
    
    {
       "took" : 1,
       "timed_out" : false,
       "_shards" : {
          "total" : 1,
          "successful" : 1,
          "skipped" : 0,
          "failed" : 0
       },
       "hits" : {
          "total" : {
             "value" : 2,
             "relation" : "eq"
          },
          "max_score" : 1.0,
          "hits" : [
             "_index" : "geo_example",
             "_type" : "_doc",
             "_id" : "hASWZ2oBbkdGzVfiXHKD",
             "_score" : 1.0,
             "_source" : {
                "name" : "Chapter One, London, UK",
                "location" : {
                   "type" : "point",
                   "coordinates" : [
                      11.660544,
                      57.800286
                   ]
                }
             }
          }
       }