码迷,mamicode.com
首页 > 其他好文 > 详细

Elasticsearch的DSL之query and filter

时间:2016-04-22 19:32:35      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

在Elasticsearch的DSL中, 有两个概念需要搞清楚, query 和 filter, 对ES的检索效率是很有影响的。

下面就来搞清楚这两个关键字的具体函数。


query context: 回答的是这个文档在多大程度上匹配查询语句(How well does this document match this query clause?),会计算出一个分数_score。


filter context: 回答的是这个文档与查询语句是否匹配,是 或者 不是(Does this document match this query clause?),不会计算分数。


除了需要匹配程度的查询(有_score的情况)使用query, 其余的查询都应该使用filter。(As a general rule, use query clauses for full-text search or for any condition that should affect the relevance score, and use filters for everything else.


filter的结果是会被ES缓存的, 以此来提高效率。


另外, filter由于不计算分数及排序, 所以, 速度较 query要快。


下面的例子来自https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html

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

技术分享

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.

技术分享 技术分享

The 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.


Elasticsearch的DSL之query and filter

标签:

原文地址:http://blog.csdn.net/smithallenyu/article/details/51209359

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!