ES 查询DSL

查询 DSL 代表领域特定语言。在 elasticsearch 中,使用基于 JSON 的搜索查询执行搜索。 Elasticsearch 提供了有助于定义查询的完整查询 DSL。在elasticsearch中有两个子句进行查询,它们是:

1、叶查询子句:

叶查询子句是那些在特定字段中搜索特定值的子句,例如 term、ma​​tch、或范围查询。这些查询是自己使用的。

2.复合查询子句:

复合查询子句是通过组合叶查询子句和其他复合查询创建的包装子句。它有助于提取所需的信息。

查询以查询关键字开始。它包含 JSON 对象形式的条件和过滤器。下面描述了几个带有示例的查询列表:

匹配所有查询

这是一个基本查询,它返回指定索引中存在的所有文档。它返回文档的所有数据,每个对象的ma​​x_score 1.0。请参阅下面给出的示例:

POST http://localhost:9200/book/_doc/
_search
{
   "query": {
      "match_all": { }
   }
}

Response

上述查询将获取 book 索引中的所有文档并返回用户。请参阅下面的回复:

{ 
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
   "successful": 1,
   "skipped": 0,
   "failed": 0,
},
"hits": {
                "total": {
        "value": 2,
        "relation": "eq"
},
"max_score": 1,
"hits": [
{
    "index": "books",
     "_type": "_docs",
     "_id": "01",
     "score": 1,
     "_source": {
   "book_name": "C++",
   "author_name": "Scott Meyers",
   "publisher": "Addision Wesley Professional",
   "publish_date": "November 1987",
   "price": "837",
      }
},
{
    "index": "books",
     "_type": "_docs",
     "_id": "02",
     "score": 1,
     "_source": {
   "book_name": "Java Black Book",
   "author_name": "Steven Holzner",
   "publisher": "E. Acoma Drive Suite 7 Scottsdale, AZ",
   "publish_date": "June 2001",
   "price": "685",
      }
 }
         ]
    }
}

截图

在浏览器中看下面的截图:

Elasticsearch Query DSL

全文查询

全文查询是高级查询。这些负责在全文字段上运行全文查询并了解如何分析被查询的字段。根据与特定文档或索引关联的分析器进行全文查询工作。我们将讨论不同数量的全文查询。

查询 说明
匹配 此查询执行全文查询搜索。
multi_match 此查询允许用户通过在多个字段中匹配文本或短语来搜索文档。
ma​​tch_phrase 它负责匹配精确的短语匹配。
ma​​tch_phrase_prefix 此查询有助于对最终单词执行通配符搜索。
comman_terms 这个查询对不常见的词给予更高的偏好。
query_string query_string 允许我们通过指定 AND|OR|NOT 条件在单个查询中执行多字段搜索。
simple_query_string simple_query_string 是 query_string 的强大版本。

让我们一一讨论每个全文查询:

匹配查询

此查询有助于通过匹配文本来获取文档一个或多个字段的值。

在下面的示例中,我们将执行查询以获取 state 字段中包含 Hauston 状态的文档。 注意,它将在所有索引名称末尾包含student 词的索引中搜索这些文档。例如-学生、学生 1、学生 2 等

POST http://localhost:9200/student*/
_search
{
  "query": {
    "match": {
       "state":"Hauston"
     }
  } 
}

Response

通过执行上面的查询,索引student和student1返回了两个文档。请参阅下面给出的输出:

{ 
"took": 2507,
"timed_out": false,
"_shards": {
"total": 2,
   "successful": 2,
   "skipped": 0,
   "failed": 0,
},
"hits": {
                "total": {
        "value": 2,
        "relation": "eq"
},
"max_score": 0.53899646,
"hits": [
{
    "index": "student",
     "_type": "_doc",
     "_id": "03",
     "score": 0.53899646,
     "_source": {
   "name": "Ammy Jonson",
   "dob": "09/Feb/1996",
   "course": "BAMS",
   "Addmission year": "2017",
   "email": "amyjon@gmail.com",
   "street": "874 Monroe Street",
   "state": "Hauston",
   "country": "United State",
   "zip": "76608",
   "fees": "54900",
      }
},
{
    "index": "student1",
     "_type": "_doc",
     "_id": "02",
     "score": 0.53899646,
     "_source": {
   "name": "Jimmy Hong",
   "dob": "24/Aug/1995",
   "course": "Bsc (Economics)",
   "Addmission year": "2018",
   "email": "jimhong@gmail.com",
   "street": "847 Monroe Street",
   "state": "Hauston",
   "country": "United State",
   "zip": "76608",
   "fees": "28900",
      }
 }
         ]
    }
}

屏幕截图

Elasticsearch Query DSL

多匹配查询

此查询允许我们通过匹配多个字段中的文本或短语来搜索文档。

在下面的示例中,我们将搜索在街道、州或两者中包含 Rodney 的文档。

POST http://localhost:9200/student*/_doc/
_search
{
   "query": {
      "multi_match": {
         "query": "Rodney",
         "fields": [ "street", "state" ]
      }
   }
}

Response

通过执行上述查询,我​​们得到三个文档,两个来自student index,另一个来自一个来自student1 索引。请参阅下面给出的输出:

{ 
"took": 1874,
"timed_out": false,
"_shards": {
"total": 2,
   "successful": 2,
   "skipped": 0,
   "failed": 0,
},
"hits": {
                "total": {
        "value": 3,
        "relation": "eq"
},
"max_score": 1.6739764,
"hits": [
{
    "index": "student",
     "_type": "_doc",
     "_id": "02",
     "score": 1.6739764,
     "_source": {
   "name": "Jass Fernandiz",
   "dob": "05/April/1995",
   "course": "Bcom (H)",
   "Addmission year": "2018",
   "email": "jassf@gmail.com",
   "street": "4225 Ersel Street",
   "state": "Rodney",
   "country": "United State",
   "zip": "75003",
   "fees": "22900",
      }
},
{
    "index": "student1",
     "_type": "_doc",
     "_id": "01",
     "score": 0.9808291,
     "_source": {
   "name": "Denial Parygen",
   "dob": "07/Aug/1998",
   "course": "Mass Communication",
   "Addmission year": "2018",
   "email": "denial@gmail.com",
   "street": "3511 Rodney Street",
   "state": "Missouri",
   "country": "United State",
   "zip": "62208",
   "fees": "24800",
      }
 },
{
    "index": "student",
     "_type": "_doc",
     "_id": "01",
     "score": 0.4.384826,
     "_source": {
   "name": "Kelvin Lewis",
   "dob": "19/July/1996",
   "course": "Msc (Music)",
   "Addmission year": "2018",
   "email": "kelvinlewis@gmail.com",
   "street": "Rodney Valley",
   "state": "Minnesota",
   "country": "United State",
   "zip": "55427",
   "fees": "24900",
      }
 }
         ]
    }
}

屏幕截图

Elasticsearch Query DSL

查询字符串查询

此查询允许我们获取其任何字段包含传递给查询字符串的文本的文档。

在下面的示例中,我们将通过一个字符串"骑马",用于搜索学生*索引中的文档并显示所有匹配的文档。

POST http://localhost:9200/student*/_doc/
_search
{
   "query": {
      "query_string": {
         "query": "Horse Riding"
      }
   }
}

Response

通过执行上述查询,我​​们得到两个文档,一个来自 student 索引,另一个来自 student1 索引。请参阅下面给出的输出:

{ 
"took": 249,
"timed_out": false,
"_shards": {
"total": 2,
   "successful": 2,
   "skipped": 0,
   "failed": 0,
},
"hits": {
                "total": {
        "value": 2,
        "relation": "eq"
},
"max_score": 1.7563686,
"hits": [
{
    "index": "student1",
     "_type": "_doc",
     "_id": "02",
     "score": 1.7563686,
     "_source": {
   "name": "Jimmy Hong",
   "dob": "24/Aug/1995",
   "course": "Bsc (Economics)",
   "Addmission year": "2018",
   "fees": "22900",
   "email": "jimmhong@gmail.com",
   "hobbies": "Horse Riding, dancing",
   "street": "847 Monroe Street",
   "state": "Hauston",
   "country": "United State",
   "zip": "76608"
      }
},
{
    "index": "student",
     "_type": "_doc",
     "_id": "03",
     "score": 1.7262595,
     "_source": {
   "name": "Ammy Jonson",
   "dob": "09/Feb/1996",
   "course": "BAMS",
   "Addmission year": "2017",
   "fees": "84900",
   "email": "amyjon@gmail.com",
   "hobbies": "Horse Riding, Singing, Drawing",
   "street": "847 Monroe Street",
   "state": "NewYork",
   "country": "United State",
   "zip": "76608"
      }
 }
{
    "index": "student",
     "_type": "_doc",
     "_id": "01",
     "score": 0.4.384826,
     "_source": {
   "name": "Kelvin Lewis",
   "dob": "19/July/1996",
   "course": "Msc (Music)",
   "Addmission year": "2018",
   "email": "kelvinlewis@gmail.com",
   "street": "Rodney Valley",
   "state": "Minnesota",
   "country": "United State",
   "zip": "55427",
   "fees": "24900",
      }
 }
         ]
    }
}

截图

看下面的截图,从Hobbies中包含Horse Riding字符串的数据库中提取了两个文档 字段。

Elasticsearch Query DSL

术语级别查询

术语级别查询处理结构化数据而不是全文字段搜索。结构数据,如数字、日期和枚举等。参见术语级别查询的给定示例:

Copy Code

POST http://localhost:9200/student*/_doc/
_search
{
   "query": {
      "term": {
         "zip":"76011"
      }
   }
}

Response

通过执行上面的查询,我们将得到所有邮编为 76011 的文档。

{ 
"took": 4185,
"timed_out": false,
"_shards": {
"total": 2,
   "successful": 2,
   "skipped": 0,
   "failed": 0,
},
"hits": {
                "total": {
        "value": 1,
        "relation": "eq"
},
"max_score": 0.9808291,
"hits": [
{
    "index": "student1",
     "_type": "_doc",
     "_id": "03",
     "score": 0.9808291,
     "_source": {
   "name": "Maria Lee",
   "dob": "12/Dec/1997",
   "course": "BFA",
   "Addmission year": "2015",
   "fees": "18900",
   "email": "marial@gmail.com",
   "hobbies": "Dancing, Photography",
   "street": "4225 Ersel Street",
   "state": "Texas",
   "country": "United State",
   "zip": "76001"
      }
 }
         ]
    }
}

屏幕截图

Elasticsearch Query DSL

范围查询

范围查询允许我们搜索值在指定范围内的文档。要执行范围查询,我们需要使用一些运算符,例如:

  • lt-小于
  • lte-小于等于
  • gt-大于
  • gte-大于等于

通过放置任何一个条件,我们可以在该条件下对给定范围的数据进行分类。请参阅下面给出的示例:

POST http://localhost:9200/student*/_doc/
_search
{
   "query": {
      "range": {
         "fees": {
            "gt":50000
         }
      }
   }
}

Response

通过执行上述查询,从学生索引中提取了两个文档,其中我们使用的费用大于 50000 gt状况。请参阅下面给出的响应:

{ 
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
   "successful": 1,
   "skipped": 0,
   "failed": 0,
},
"hits": {
                "total": {
        "value": 2,
        "relation": "eq"
},
"max_score": 1,
"hits": [
{
    "index": "student",
     "_type": "_doc",
     "_id": "01",
     "score": 1,
     "_source": {
   "name": "Kelvin Lewis",
   "dob": "19/July/1996",
   "course": "Msc (Music)",
   "Addmission year": "2018",
   "fees": "54900",
   "email": "kelvinlewis@gmail.com",
   "hobbies": "Travelling, Cooking, Painting",
   "street": "Rodney Valley",
   "state": "Minnesota",
   "country": "United State",
   "zip": "55427"
      }
 },
{
    "index": "student",
     "_type": "_doc",
     "_id": "03",
     "score": 1,
     "_source": {
   "name": "Ammy Jonson",
   "dob": "09/Feb/1996",
   "course": "Msc (Music)",
   "Addmission year": "2017",
   "fees": "84900",
   "email": "amyjon@gmail.com",
   "hobbies": "Horse Riding, Singing, Drawing",
   "street": "847 Monroe Street",
   "state": "NewYork",
   "country": "United State",
   "zip": "76608"
      }
 }
         ]
    }
}

同理,我们可以根据需要使用其他条件gte、lt或lte对文档进行分类和获取。

截图

Elasticsearch Query DSL

其他类型的术语级别查询

低于某个术语级别查询如下:

查询 描述
期限 术语查询有助于搜索包含查询中指定的确切术语的文档。
条款 此查询与术语查询略有不同。它有助于查找包含查询中指定的任何确切术语的文档。
范围 它有助于搜索包含查询中指定的特定字段的给定范围的文档。
存在 exits 查询有助于查找查询指定的字段具有任何非空值的文档。
缺少查询 缺失查询与存在查询正好相反。与exists查询不同,它查找没有特定字段或具有空值的字段的文档。
前缀 它搜索包含以查询中指定的确切前缀开头的术语的文档。
通配符 有助于搜索包含与查询指定的模式匹配的术语的文档。
正则表达式 它在查询中使用正则表达式来查找对象中的某种模式。
模糊 它允许我们找到与指定术语模糊相似的术语的文档。
类型 类型查询有助于找到指定类型的文档。
ID 此查询还负责查找指定类型和 ID 的文档。

类型查询

类型查询我们允许查找用户在查询中指定的特定类型(默认为_doc)的文档。在elasticsearch中,默认的文档ment 类型是 _doc,但文档类型也可以是用户定义的。

如果存在查询中指定的特定类型的文档,则返回这些文档列表,否则返回空值。

例如:

POST http://localhost:9200/
_search
{
   "query": {
      "type": {
         "value": "_doc"
     }
   }
}

Response

通过执行上面的查询,从多个索引如-book, student 、new_student 和 student1(我们创建的)已经返回。请参阅下面的输出:

. . . . . . . . . . . . . . . . . . . . . . . . . . .
"hits": {
                "total": {
        "value": 13,
        "relation": "eq"
},
"max_score": 1,
"hits": [
{
    "index": "book",
     "_type": "_doc",
     "_id": "01",
     "score": 1,
     "_source": {
   "book_name": "C++",
   "author_name": "Scott Meyers",
   "publisher": "Addision Wesley Professional",
   "publish_date": "November 1987",
   "price": "837",
      }
 },
{
    "index": "new_student",
     "_type": "_doc",
     "_id": "02",
     "score": 1,
     "_source": {
   "name": "Jass Fernandiz",
   "dob": "07/Aug/1996",
   "course": "Bcom (H)",
   "Addmission year": "2019",
   "fees": "24900",
   "email": "jassf@gmail.com",
   "hobbies": "Horse Riding, Singing, Crafting",
   "street": "4225 Ersel Street",
   "state": "Texas",
   "country": "United State",
   "zip": "76011"
      }
 },
. . . . . . . . . . . . . . . . . . . . . . . . . .

屏幕截图

在下面的屏幕截图中,您可以看到从 book 和 student 索引返回的文档是 _doc 类型,如查询中提到的那样。

p> Elasticsearch Query DSL

现在,转向我们之前简要讨论过的复合查询。在这里,我们将通过示例详细阐述复合查询。

复合查询

顾名思义,复合查询是不同查询的集合。这些查询使用布尔运算符(例如 AND、OR、NOT)合并在一起,或者用于多个索引或具有函数调用。

它负责将叶查询和其他查询包装在一起。基本上,这些查询用于组合其结果、更改其行为或从查询切换到过滤器上下文。让我们举个例子来理解下面给出的复合查询:

Copy Code

POST http://localhost:9200/student/_doc/
_search
{
   "query": {
      "bool": {
         "must": {
            "term": {
               "state": "NewYork"
            }
         },   
         "filter": {
            "term": {
               "fees": "24900"
            },
            "minimum_should_match": 1,
            "boost": 1.0
        }
     }
  }
}

Response

上述查询返回了 0 个文档,max_socre 为空,总值为零。请参阅下面给出的响应:

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

屏幕截图

Elasticsearch Query DSL

地理查询

地理查询处理地理点和地理位置。它有助于找到学校或任何位置附近的任何地理对象。为此,我们需要使用地理点数据类型,例如地点的位置。请记住,elasticsearch 支持两种类型的地理数据(geo_point 和 geo_shape),它们是:

geo_point- geo_points 是支持经纬度对的字段。

geo_shape- 和geo_points类似,geo_shape也是支持点、线、圆、多边形、多边形等的字段。

看例子下面给出:

执行以下查询以创建名为 geo_query_example 的索引带有地图和位置。

PUT http://localhost:9200/geo_query_example/
{
   "mapping": {
       "properties": {
          "location": {
             "type": "geo_shape"
         }
      }
   }
}

响应:

通过执行上面的代码,创建了一个带有 geo_query_example 名称的索引。请参阅下面给出的响应:

{ 
"acknowledged": true,
"shard_acknowledged": false,
"index": "geo_query_example"
       }
}

屏幕截图

Elasticsearch 查询 DSL

将数据添加到索引

现在,将以下数据添加到上面创建的geo_query_example 索引中。

POST http://localhost:9200/geo_query_example/
_doc?refresh
{
   "name": "NewYork, Paris, London",
   "location": {
      "type": "point",
      "coordinates": [10.650477, 47.901276]
   }
}

响应:

通过执行上面的代码,在geo_query_example索引中添加数据成功。请参阅下面给出的响应:

{ 
"index": "geo_query_example",
"_type": "_doc",
"_id": "UEcpoHMBPRkyw7wOJtn-",
"_version": 1,
"result": "created",
"forced_refresh": true,
"_shard": {
     "total": 2,
     "successful": 1,
     "failed": 0
},
"_seq_no": 1,
"_primary_term": 1
       }
}

屏幕截图

Elasticsearch 查询 DSL

下一章:ES 分析

分析是将文本转换为标记或术语的过程,例如转换任何电子邮件的正文。这些被添加到倒排索引中以供进一步搜索。因此,无论何时在搜索操作期间处理查询,分析模块都会分析任何索引中的可用数据。此分析模块包括分析器、分词器、字符过滤器和标记过滤 ...