首页 > 解决方案 > Elasticsearch hits.total 与 OR 不同

问题描述

当我使用以下搜索(/posts/_search)时,我的 hits.total 为 1400:

{"query": {"query_string": {"query": "Bitcoin"}}}

当我使用以下搜索(/posts/_search)时,我的 hits.total 为 500:

{"query": {"query_string": {"query": "Ethereum"}}}

当我在搜索中使用 OR 时,它hits.total是 1400,我预计它是 1900。

{"query": {"query_string": {"query": "(Ethereum) OR (Bitcoin)"}}}

当我使用“OR”时,为什么我的 hits.total 数字不同?我使用 hits.total 作为计数器来显示,数字应该是一样的,对吧?

我对 ElasticSearch 很陌生,希望有人能指出我正确的方向。谢谢!

标签: elasticsearch

解决方案


Most probably it Looks like there are some documents where **_all has both terms** i.e. Bitcoin and Ethereum, and hence, same documents get selected when u run the query independently, but when u run, this common documents get included only once.

May be this Venn diagram can explain better

enter image description here

A U B = (7+2+5) + (8+1+2+5) - (2+5) = 23

A + B = (7+2+5) + (8+1+2+5) = 30

If you are sure, these field which can never have multiple values then try adding "default_field" in the query and run the results. When you don't pass "default_field", if defaults to index.query.default_field index settings, which in turn defaults to _all.

{
  "query": {
    "query_string": {
      "default_field": "CRYPTOCURRENCY_TYPE",
      "query": "as"
    }
  }
}

More details you can be found here : https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-query-string-query.html


推荐阅读