elasticsearch - 在Elasticsearch中 `should`之后 `filter`如何工作?

标签 elasticsearch

我正在使用Elasticsearch 6.8。我的索引中有一个文档,如下所示:

$ curl "http://localhost:9200/users/_search"|jq
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "users",
        "_type": "1",
        "_id": "WzLeNG4BVtGh82ZM-BBl",
        "_score": 1,
        "_source": {
          "name": "joey",
          "description": "xpxpxp all xpxpxp"
        }
      }
    ]
  }
}

如果我在下面的查询中使用它,确实会向我返回文档。
{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "name": "joey"
                }   
            },
            "should": [
                {
                    "constant_score": {
                        "filter": {
                            "match_phrase": {
                                "description": "xpxpxp dd All xpxpxp"
                            }
                        }
                    }
                }
            ]
        }
    }
}

但是如果我使用下面的查询,它什么也不会返回:
{
    "query": {
        "bool": {
            "should": [
                {
                    "constant_score": {
                        "filter": {
                            "match_phrase": {
                                "description": "xpxpxp dd All xpxpxp"
                            }
                        }
                    }
                }
            ]
        }
    }
}

这两个查询请求之间的区别在于,第一个查询请求在filter之前具有should。我不知道为什么这会更改should行为?

最佳答案

您的should子句不匹配,因为match_phrase字段上的description无法匹配xpxpxp dd All xpxpxp,因为该字段包含xpxpxp all xpxpxp
第一个查询返回文档,因为name字段匹配joey

关于elasticsearch - 在Elasticsearch中 `should`之后 `filter`如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58687739/

相关文章:

node.js - 如何在 Elasticsearch 中索引 Mongoose 时间戳

algorithm - 使用 ElasticSearch 进行模糊重复搜索

c# - 默认情况下,查询字段提升权重的最小值和最大值是多少?

elasticsearch - Kibana可视化分隔我要分组的X轴值

elasticsearch - Elasticsearch主要错误RollingFileManager(C:\Program Files\Elastic\Elasticsearch\6.4.0\logs\elasticsearch.log)

elasticsearch - 在Solr 5.3.1中替换ApostropheFilterFactory

ruby - 将Rails应用程序连接到Elasticsearch服务器

elasticsearch - Elasticsearch 模糊查询意外结果

ruby-on-rails - 带有轮胎 : edgeNgram with multiple words 的 Elasticsearch

ruby-on-rails - Searchkick,仅搜索特定ID