Adriaan Bouman
Adriaan Bouman

Reputation: 41

Elasticsearch hits.total different with OR

When I use the following search (/posts/_search) my hits.total is 1400:

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

When I use the following search (/posts/_search) my hits.total is 500:

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

When I use an OR in my search, the hits.total is 1400, where I expected it to be 1900.

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

Why is my hits.total number different when I am using an "OR"? I am using the hits.total as a counter to display and the number should be the same, right?

I am pretty new with ElasticSearch and hopefully, someone could point me in the right direction. Thanks!

Upvotes: 0

Views: 405

Answers (1)

lrathod
lrathod

Reputation: 1124

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

Upvotes: 1

Related Questions