Query DSL - 查询和过滤上下文

Query and filter context

查询和过滤上下文

Relevance scores

相关性得分

By default, Elasticsearch sorts matching search results by relevance score, which measures how well each document matches a query.

默认情况下,Elasticsearch按相关性得分对匹配的搜索结果进行排序,该得分衡量每个文档与查询的匹配程度。

The relevance score is a positive floating point number, returned in the _score meta-field of the search API. The higher the _score, the more relevant the document. While each query type can calculate relevance scores differently, score calculation also depends on whether the query clause is run in a query or filter context.

相关性分数是一个正浮点数,在搜索 API 的meta-field(元字段)_score中返回 。_score越高,文档越相关。每种查询类型计算相关性得分可以不同,分数计算还取决于查询子句是运行在查询上下文中还是在过滤器上下文。

Query context

查询上下文

In the query context, a query clause answers the question “How well does this document match this query clause?” Besides deciding whether or not the document matches, the query clause also calculates a relevance score in the _score meta-field.

在查询上下文中,查询子句回答这样的问题“ 此文档与该查询子句的匹配程度如何?”除了确定文档是否匹配外,查询子句还计算meta-field(元字段)_score的相关性得分 。

Query context is in effect whenever a query clause is passed to a query parameter, such as the query parameter in the search API.

查询上下文在查询子句中通过query 参数设置,例如:

GET /_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

Filter context

过滤器上下文

In a filter context, a query clause answers the question “Does this document match this query clause?” The answer is a simple Yes or No — no scores are calculated. Filter context is mostly used for filtering structured data, e.g.

在过滤器上下文中,查询子句回答这样的问题“ *此文档是否与此查询子句匹配?*答案是简单的是或否-不计算分数。过滤器上下文主要用于过滤结构化数据,例如

  • Does this timestamp fall into the range 2015 to 2016?

  • Is the status field set to "published"?

  • timestamp是否属于2015年至2016年的范围?

  • *status 字段设置为"published"*吗?

Frequently used filters will be cached automatically by Elasticsearch, to speed up performance.

常用过滤器将由Elasticsearch自动缓存,以提高性能。

Filter context is in effect whenever a query clause is passed to a filter parameter, such as the filter or must_not parameters in the bool query, the filter parameter in the constant_score query, or the filter aggregation.

过滤器上下文在查询子句中通过filter 参数设置(例如 bool查询中的filter参数和must_not参数, constant_score中的filter参数,或者filter聚合)。

Example of query and filter contexts

查询和过滤器上下文示例

Below is an example of query clauses being used in query and filter context in the search API. This query will match documents where all of the following conditions are met:

以下是在searchAPI中使用查询上下文和过滤器上下文查询子句的示例。此查询将匹配满足以下所有条件的文档:

  • The title field contains the word search.

  • The content field contains the word elasticsearch.

  • The status field contains the exact word published.

  • The publish_date field contains a date from 1 Jan 2015 onwards.

  • title字段包含单词search

  • content字段包含单词elasticsearch

  • status字段包含确切的单词published

  • publish_date字段包含从2015年1月1日开始的日期。

GET /_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }},
        { "match": { "content": "Elasticsearch" }}
      ],
      "filter": [ 
        { "term":  { "status": "published" }},
        { "range": { "publish_date": { "gte": "2015-01-01" }}}
      ]
    }
  }
}

Copy as cURLView in Console

The query parameter indicates query context.
The bool and two match clauses are used in query context, which means that they are used to score how well each document matches.
The filter parameter indicates filter context. Its term and range clauses are used in filter context. They will filter out documents which do not match, but they will not affect the score for matching documents.
query参数表示查询上下文。
must下的两个match子句用于查询上下文,它们被用来表示每个文档匹配程度的评分。
filter下的参数表示过滤器上下文。其termand range子句用于过滤器上下文。它们将过滤出不匹配的文档,但不会影响匹配文档的分数。

Scores calculated for queries in query context are represented as single precision floating point numbers; they have only 24 bits for significand’s precision. Score calculations that exceed the significand’s precision will be converted to floats with loss of precision.

在查询上下文中为查询计算的分数被表示为单精度浮点数;他们只有24位bits的有效数字的精度。超过有效位数精度的计算分数将转换为失去精度的浮点数。

Use query clauses in query context for conditions which should affect the score of matching documents (i.e. how well does the document match), and use all other query clauses in filter context.

在查询上下文中使用查询子句以应对可能影响匹配文档得分(即文档匹配程度)的条件,并在过滤器上下文中使用所有其他查询子句。

ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。Kibana 也是一个开源免费的工具,Kibana可以为 Logstash ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析搜索重要数据日志。该课程从ELK组件中的两大组件常用的应用日志收集进行剖析讲解。(1)ElasticSearch:从单台的安装到集群的搭建、从基础的入门使用到高级的搜索聚合都进行了较好的阐述。(2) Logstash:通过收集apache日志,使用grok插件geoip插件先让学习人员对整体框架做到了了解于胸。然后再深入的对LogStash重要的知识点进行了剖析。(3)应用日志收集:对常见的Apache、Nginx、MySQL、Syslog进行日志收集分析。虽说没有讲解太多,但是我想应该能做到让大家对应用日志收集了解的更加深入。
<think>好的,用户想了解如何在Elasticsearch DSL查询以特定值开头的内容。首先,我需要回忆一下Elasticsearch查询类型。前缀查询应该是prefix查询,不过用户提到了“match specific prefix”,可能需要确认是否是指通配符查询或者prefix查询。 根据引用中的信息,用户已经了解了一些基本的查询类型,比如Match QueryMatch Phrase Query。现在需要找关于前缀查询的部分。Elasticsearch中的前缀查询通常使用prefix查询或者wildcard查询。不过prefix查询是用于精确字段的前缀匹配,而wildcard允许使用通配符,比如“text*”。 另外,用户提到DSL语句,可能需要给出具体的示例。比如使用prefix查询的结构。需要注意的是,prefix查询在文本字段上可能效率不高,因为需要遍历倒排索引中的术语。如果字段是keyword类型的话,效果会更好。 还要考虑是否涉及过滤上下文查询上下文。根据引用4,查询上下文会影响相关性评分,而过滤上下文仅关心是否匹配。如果用户只是需要过滤出以特定前缀开头的文档,使用filter context会更高效,因为它会被缓存且不计分。 可能需要比较prefix查询wildcard查询的区别,指出prefix查询适合精确前缀匹配,而wildcard更灵活但性能较差。同时,建议用户如果处理的是全文搜索,可能需要结合其他查询类型,或者使用keyword子字段来进行精确匹配。 另外,用户的问题中提到了“match specific prefix”,可能存在误解,因为match查询通常用于分词后的匹配,而prefix查询则是精确匹配开头。需要明确说明两者区别,避免混淆。 最后,给出示例代码,展示如何使用prefix查询,并说明适用场景注意事项,比如避免长前缀导致的性能问题,推荐使用keyword类型字段等。</think>在Elasticsearch DSL中,查询以特定值开头的内容可以通过以下两种主要方式实现: ### 1. 使用`prefix`查询 适用于对`keyword`类型字段进行精确前缀匹配,属于**过滤上下文**,不计算相关性分数,性能更高。 示例: ```json GET /my-index/_search { "query": { "prefix": { "title.keyword": { "value": "elastic" } } } } ``` 这会匹配`title.keyword`字段值以"elastic"开头的文档(如"elasticsearch")[^4]。 ### 2. 使用`wildcard`通配符查询 支持更灵活的模式匹配,但性能较低,建议谨慎使用: ```json GET /my-index/_search { "query": { "wildcard": { "title": { "value": "elastic*" } } } } ``` ### 关键区别说明 | 特性 | prefix查询 | wildcard查询 | |-----------|-----------------------|----------------------| | 匹配方式 | 仅前缀匹配 | 支持`*``?`通配符 | | 适用字段类型 | keyword类型字段最佳 | text/keyword类型均可 | | 性能 | 较高(使用倒排索引直接定位) | 较低(需要遍历更多术语) | | 是否支持分词 | 不支持 | 不支持 | | 是否计算相关性分数 | 否(建议放在filter上下文) | 是(默认在query上下文)[^4] | ### 最佳实践建议 1. 对`text`类型字段使用`.keyword`子字段进行前缀匹配 2. 长前缀(3个字符以上)可以提高查询效率 3. 高频查询建议结合`bool`过滤上下文: ```json GET /my-index/_search { "query": { "bool": { "filter": [ {"prefix": {"title.keyword": "elas"}} ] } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值