# Problem with Dis Max Query

**URL:** <https://discuss.elastic.co/t/problem-with-dis-max-query/41572>\
**Category:** Elasticsearch\
**Created:** [February 12, 2016, 3:56am UTC](https://discuss.elastic.co/t/problem-with-dis-max-query/41572 "2016-02-12T03:56:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FisherKing](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@FisherKing](https://discuss.elastic.co/u/FisherKing)\
**Post date:** [February 12, 2016, 3:56am UTC](https://discuss.elastic.co/t/problem-with-dis-max-query/41572/1 "2016-02-12T03:56:29Z")

</div>

I have this documents:

{  
"firstfield" : "House"  
}

{  
"firstfield" : "Red House"  
"secondfield" : "Blue House"  
}

I want to query every field of a document. Only the field with the maximum score should set the final score for the document.

For example when I search for 'House', the first document should have the highest score.

I tried to achieve this with a dis max query. But it seems that there is always a boost if both fields contain the word.

My query looks like this:

"dis\_max" : {  
"tie\_breaker" : 0.0,  
"queries" : [ {  
"match" : {  
"firstfield" : {  
"query" : "House",  
"operator" : "AND"  
}  
}  
}, {  
"match" : {  
"secondfield" : {  
"query" : "House",  
"operator" : "AND"  
}  
}  
} ]  
}

Anyone has a solution for my problem? I am using Elasticsearch 2.2.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [February 12, 2016, 11:02pm UTC](https://discuss.elastic.co/t/problem-with-dis-max-query/41572/2 "2016-02-12T23:02:11Z")

</div>

Why should the first document score higher? Can you post the explanation of  
the query?

If the second document scores higher, then perhaps the term 'house' has a  
higher IDF value in the 'secondfield' field. Hard to tell without data.

Ivan

---

<div class="post-metadata">

**Author:** ![FisherKing](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@FisherKing](https://discuss.elastic.co/u/FisherKing)\
**Post date:** [February 13, 2016, 4:42pm UTC](https://discuss.elastic.co/t/problem-with-dis-max-query/41572/3 "2016-02-13T16:42:26Z")

</div>

I gave you all the data I test with ( only 2 documents ).

This query will have this scores:

"dis\_max" : {  
"tie\_breaker" : 0.0,  
"queries" : [ {  
"match" : {  
"firstfield" : {  
"query" : "House",  
"type" : "boolean",  
"operator" : "AND"  
}  
}  
}, {  
"match" : {  
"secondfield" : {  
"query" : "House",  
"type" : "boolean",  
"operator" : "AND"  
}  
}  
} ]  
}

"Red House" , "Blue House" =\> score 0.19178301

"House" =\> score 0.09415865

If I change the query to only 1 field ( which doesn't make much sense) I get different results:

"dis\_max" : {  
"tie\_breaker" : 0.0,  
"queries" : [ {  
"match" : {  
"firstfield" : {  
"query" : "House",  
"type" : "boolean",  
"operator" : "AND"  
}  
}  
} ]  
}

"House" =\> score 0.30685282

"Red House" , "Blue House" =\> score 0.19178301

If it would really be a pure disjunction max query it should return a score of 0.30685282 for House in both queries.

This must be either a bug or the Dis Max Query is totally useless ☹

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [February 13, 2016, 5:39pm UTC](https://discuss.elastic.co/t/problem-with-dis-max-query/41572/4 "2016-02-13T17:39:39Z")

</div>

By data I mean the explanation of the query:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-explain.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-explain.html)

Is your environment sharded? If your documents are in different shards, the  
same terms might have different IDF values. If you have just a few test  
documents, scores will be more consistent in a single shard environment.

Also, in your last query, the first document probably has a higher value  
because of length normalization. By default, a shorter field will be more  
relevant than a longer field, even if the term frequency is the same:

[https://www.elastic.co/guide/en/elasticsearch/guide/master/scoring-theory.html#field-norm](https://www.elastic.co/guide/en/elasticsearch/guide/master/scoring-theory.html#field-norm)

Reading Lucene explanations takes practice, but all the relevant details  
are in there.

Ivan

---

<div class="post-metadata">

**Author:** ![FisherKing](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@FisherKing](https://discuss.elastic.co/u/FisherKing)\
**Post date:** [February 14, 2016, 5:57am UTC](https://discuss.elastic.co/t/problem-with-dis-max-query/41572/5 "2016-02-14T05:57:33Z")

</div>

Thank you for your help. I found out that IDF is the reason for this strange results. I think I was using only 1 shard the whole time. It's possible that the same term will get different IDF values even if only 1 shard is used?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [February 14, 2016, 5:10pm UTC](https://discuss.elastic.co/t/problem-with-dis-max-query/41572/6 "2016-02-14T17:10:12Z")

</div>

The IDF should be the same for each document in the shard. The IDF  
scores/weighs the search term, not the document. It sounds like documents  
are in different shards. You can view the number of shards in the GET  
settings API:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html)

Elasticsearch has a default of five shards. If you read the explanation, it  
will tell you which shard the document hit came from.

You can also change the search type to do a distributed query then fetch.  
There is a performance hit since it needs to do another network roundtrip  
between the coordinating node and the data nodes, but I think it is slight.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html#dfs-query-then-fetch](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html#dfs-query-then-fetch)

The issue is minimized as you add more documents to an index. You normally  
see the issue in test indices with a handful of documents.

Ivan

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:16pm UTC](https://discuss.elastic.co/t/problem-with-dis-max-query/41572/7 "2017-07-05T23:16:31Z")

</div>


