5.2 Query DSL 详解


文档摘要

5.2 Query DSL 详解 Elasticsearch Query DSL 详解 Query DSL 的基本结构 一个 Query DSL 查询通常包含以下结构: : 最外层,表示这是一个查询语句。 : 指定查询类型,例如 、 、 等。 : 要查询的字段。 : 要搜索的值。 : 可选参数,用于提升查询的权重。 常用查询类型详解 以下是一些常用的 Query DSL 查询类型,并附带代码示例: 2.1 Match Query 查询是全文搜索的基础,它会分析查询字符串,并根据分析结果进行匹配。 解释: 这个查询会在 字段中搜索包含 "Elasticsearch" 和 "Tutorial" 的文档。Elasticsearch 会对查询字符串进行分词,然后匹配包含任意一个或所有分词的文档。

5.2 Query DSL 详解

Elasticsearch Query DSL 详解

1. Query DSL 的基本结构

一个 Query DSL 查询通常包含以下结构:

{ "query": { "query_type": { "field": "value", "boost": 1.0, // 其他参数 } } }
  • query: 最外层,表示这是一个查询语句。

  • query_type: 指定查询类型,例如 matchtermrange 等。

  • field: 要查询的字段。

  • value: 要搜索的值。

  • boost: 可选参数,用于提升查询的权重。

2. 常用查询类型详解

以下是一些常用的 Query DSL 查询类型,并附带代码示例:

2.1 Match Query

match 查询是全文搜索的基础,它会分析查询字符串,并根据分析结果进行匹配。

{ "query": { "match": { "title": "Elasticsearch Tutorial" } } }

解释:

这个查询会在 title 字段中搜索包含 "Elasticsearch" 和 "Tutorial" 的文档。Elasticsearch 会对查询字符串进行分词,然后匹配包含任意一个或所有分词的文档。

代码示例 (Python):

from elasticsearch import Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) query = { "query": { "match": { "title": "Elasticsearch Tutorial" } } } response = es.search(index="my_index", body=query) print(response)

2.2 Term Query

term 查询用于精确匹配某个字段的值,不会进行分词。

{ "query": { "term": { "status": "published" } } }

解释:

这个查询会精确匹配 status 字段值为 "published" 的文档。

代码示例 (Python):

from elasticsearch import Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) query = { "query": { "term": { "status": "published" } } } response = es.search(index="my_index", body=query) print(response)

2.3 Range Query

range 查询用于查找字段值在指定范围内的文档。

{ "query": { "range": { "age": { "gte": 20, "lte": 30 } } } }

解释:

这个查询会查找 age 字段值在 20 到 30 之间的文档(包含 20 和 30)。

  • gte: 大于等于 (greater than or equal to)

  • lte: 小于等于 (less than or equal to)

  • gt: 大于 (greater than)

  • lt: 小于 (less than)

代码示例 (Python):

from elasticsearch import Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) query = { "query": { "range": { "age": { "gte": 20, "lte": 30 } } } } response = es.search(index="my_index", body=query) print(response)

2.4 Boolean Query (Bool Query)

bool 查询允许你组合多个查询条件,使用 mustshouldmust_notfilter 子句。

{ "query": { "bool": { "must": [ { "match": { "title": "Elasticsearch" } } ], "should": [ { "match": { "content": "search" } } ], "must_not": [ { "term": { "status": "draft" } } ], "filter": [ { "range": { "age": { "gte": 18 } } } ] } } }

解释:

  • must: 文档必须匹配这些查询条件(AND)。

  • should: 文档应该匹配这些查询条件,但不强制(OR)。should 子句可以指定 minimum_should_match 参数,控制至少需要匹配多少个 should 子句。

  • must_not: 文档不能匹配这些查询条件。

  • filter: 文档必须匹配这些查询条件,但不参与评分。filter 子句通常用于过滤数据,性能比 must 子句更好。

代码示例 (Python):

from elasticsearch import Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) query = { "query": { "bool": { "must": [ { "match": { "title": "Elasticsearch" } } ], "should": [ { "match": { "content": "search" } } ], "must_not": [ { "term": { "status": "draft" } } ], "filter": [ { "range": { "age": { "gte": 18 } } } ] } } } response = es.search(index="my_index", body=query) print(response)

2.5 Multi Match Query

multi_match 查询允许你在多个字段中搜索同一个值。

{ "query": { "multi_match": { "query": "Elasticsearch", "fields": ["title", "content"] } } }

解释:

这个查询会在 titlecontent 字段中搜索包含 "Elasticsearch" 的文档。

代码示例 (Python):

from elasticsearch import Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) query = { "query": { "multi_match": { "query": "Elasticsearch", "fields": ["title", "content"] } } } response = es.search(index="my_index", body=query) print(response)

2.6 exists Query

exists 查询用于查找包含特定字段的文档。

{ "query": { "exists": { "field": "tags" } } }

解释:

这个查询会查找所有包含 tags 字段的文档。

代码示例 (Python):

from elasticsearch import Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) query = { "query": { "exists": { "field": "tags" } } } response = es.search(index="my_index", body=query) print(response)

2.7 prefix Query

prefix 查询用于查找以指定前缀开头的字段值的文档。

{ "query": { "prefix": { "name": { "value": "Elas" } } } }

解释:

这个查询会查找 name 字段值以 "Elas" 开头的文档。

代码示例 (Python):

from elasticsearch import Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) query = { "query": { "prefix": { "name": { "value": "Elas" } } } } response = es.search(index="my_index", body=query) print(response)

3. Query DSL 的组合使用

Query DSL 的强大之处在于可以将各种查询类型组合起来,构建复杂的搜索逻辑。例如,可以使用 bool 查询组合 matchrange 查询,实现更精确的搜索。

4. Graph Visualization (Mermaid)

解释:

这个 Mermaid 图展示了 Query DSL 的整体结构,以及它包含的各种查询类型,以及Bool Query包含的子句。

5. 总结

Elasticsearch 的 Query DSL 提供了丰富的查询类型,可以满足各种搜索需求。掌握 Query DSL 的基本结构和常用查询类型,可以帮助你构建更精确、更高效的搜索应用。通过组合不同的查询类型,可以实现复杂的搜索逻辑,满足各种业务场景的需求。 记住,在实际应用中,需要根据数据结构和业务需求选择合适的查询类型,并进行性能优化。


作者与出处
原作者: 灏天文库
来源:灏天文库
整理: 灏天文库整理
由灏天文库平台收录,内容或由平台用户上传,仅供学习交流
发布者: 作者: 灏天文库 转发
评论区 (0)
U