Elasticsearch 简明教程

Elasticsearch - Query DSL

在 Elasticsearch 中,通过基于 JSON 的查询执行搜索。查询由两个子句组成 −

In Elasticsearch, searching is carried out by using query based on JSON. A query is made up of two clauses −

  1. Leaf Query Clauses − These clauses are match, term or range, which look for a specific value in specific field.

  2. Compound Query Clauses − These queries are a combination of leaf query clauses and other compound queries to extract the desired information.

Elasticsearch 支持大量查询。查询以查询关键字开头,然后以 JSON 对象的形式在其中具有条件和过滤器。不同类型的查询已在下面进行描述。

Elasticsearch supports a large number of queries. A query starts with a query key word and then has conditions and filters inside in the form of JSON object. The different types of queries have been described below.

Match All Query

这是最基本的查询;它返回所有内容,并且每个对象的得分均为 1.0。

This is the most basic query; it returns all the content and with the score of 1.0 for every object.

POST /schools/_search
{
   "query":{
      "match_all":{}
   }
}

运行以上代码时,我们得到以下结果:-

On running the above code, we get the following result −

{
   "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" : "schools",
            "_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" : "schools",
            "_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"
            }
         }
      ]
   }
}

Full Text Queries

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

These queries are used to search a full body of text like a chapter or a news article. This query works according to the analyser associated with that particular index or document. In this section, we will discuss the different types of full text queries.

Match query

此查询将文本或短语与一个或多个字段的值进行匹配。

This query matches a text or phrase with the values of one or more fields.

POST /schools*/_search
{
   "query":{
      "match" : {
         "rating":"4.5"
      }
   }
}

在运行以上代码时,我们得到响应,如下所示:-

On running the above code, we get the response as shown below −

{
   "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" : "schools",
            "_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"
            }
         }
      ]
   }
}

Multi Match Query

此查询将文本或短语与多个字段进行匹配。

This query matches a text or phrase with more than one field.

POST /schools*/_search
{
   "query":{
      "multi_match" : {
         "query": "paprola",
         "fields": [ "city", "state" ]
      }
   }
}

在运行以上代码时,我们得到响应,如下所示:-

On running the above code, we get the response as shown below −

{
   "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" : "schools",
            "_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 Query

此查询使用查询解析器和 query_string 关键字。

This query uses query parser and query_string keyword.

POST /schools*/_search
{
   "query":{
      "query_string":{
         "query":"beautiful"
      }
   }
}

在运行以上代码时,我们得到响应,如下所示:-

On running the above code, we get the response as shown below −

{
   "took" : 60,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
      "value" : 1,
      "relation" : "eq"
   },
………………………………….

Term Level Queries

这些查询主要处理结构化数据,如数字、日期和枚举。

These queries mainly deal with structured data like numbers, dates and enums.

POST /schools*/_search
{
   "query":{
      "term":{"zip":"176115"}
   }
}

在运行以上代码时,我们得到响应,如下所示:-

On running the above code, we get the response as shown below −

……………………………..
hits" : [
   {
      "_index" : "schools",
      "_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
         ],
      }
   }
]
…………………………………………..

Range Query

此查询用于查找在给定值范围内具有值的那些对象。为此,我们需要使用操作符,如 −

This query is used to find the objects having values between the ranges of values given. For this, we need to use operators such as −

  1. gte − greater than equal to

  2. gt − greater-than

  3. lte − less-than equal to

  4. lt − less-than

例如,请观察下面给出的代码 -

For example, observe the code given below −

POST /schools*/_search
{
   "query":{
      "range":{
         "rating":{
            "gte":3.5
         }
      }
   }
}

在运行以上代码时,我们得到响应,如下所示:-

On running the above code, we get the response as shown below −

{
   "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" : "schools",
            "_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"
            }
         }
      ]
   }
}

还存在其他类型的期限级别查询,例如 -

There exist other types of term level queries also such as −

  1. Exists query − If a certain field has non null value.

  2. Missing query − This is completely opposite to exists query, this query searches for objects without specific fields or fields having null value.

  3. Wildcard or regexp query − This query uses regular expressions to find patterns in the objects.

Compound Queries

这些查询是通过使用布尔运算符(如与运算符、或运算符、非运算符或不同索引的运算符或有函数调用等的运算符)合并到一起的不同查询的集合。

These queries are a collection of different queries merged with each other by using Boolean operators like and, or, not or for different indices or having function calls etc.

POST /schools/_search
{
   "query": {
      "bool" : {
         "must" : {
            "term" : { "state" : "UP" }
         },
         "filter": {
            "term" : { "fees" : "2200" }
         },
         "minimum_should_match" : 1,
         "boost" : 1.0
      }
   }
}

在运行以上代码时,我们得到响应,如下所示:-

On running the above code, we get the response as shown below −

{
   "took" : 6,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
         "value" : 0,
         "relation" : "eq"
      },
      "max_score" : null,
      "hits" : [ ]
   }
}

Geo Queries

这些查询涉及地理位置和地理点。这些查询有助于找到学校或任何其他靠近某个位置的地理对象。您需要使用地理点数据类型。

These queries deal with geo locations and geo points. These queries help to find out schools or any other geographical object near to any location. You need to use geo point data type.

PUT /geo_example
{
   "mappings": {
      "properties": {
         "location": {
            "type": "geo_shape"
         }
      }
   }
}

在运行以上代码时,我们得到响应,如下所示:-

On running the above code, we get the response as shown below −

{  "acknowledged" : true,
   "shards_acknowledged" : true,
   "index" : "geo_example"
}

现在,我们将数据发布到上面创建的索引中。

Now we post the data in the index created above.

POST /geo_example/_doc?refresh
{
   "name": "Chapter One, London, UK",
   "location": {
      "type": "point",
      "coordinates": [11.660544, 57.800286]
   }
}

在运行以上代码时,我们得到响应,如下所示:-

On running the above code, we get the response as shown below −

{
   "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
               ]
            }
         }
      }
   }