7

I am retrieving documents by filtering and using a term query to apply a score. The query should match all animals having a specified color - the more colors are matched, the higher the score of a doc. Strange thing is, term and terms query result in a different scoring.

{
    "query": {
        "bool": {
            "should": [
                {"terms": {"color": ["brown","darkbrown"] } },
            ]
        }
    }
}

should be the same like using

{"term": {"color": {"value": "brown"} } },
{"term": {"color": {"value": "darkbrown"} } }

Query no. 1 gives me the exact same score for a document whether 1 or 2 terms are matched. The latter of course returns a higher score, if more colors are matched.

As stated by the coordination factor the returned score should be higher if more terms are matched. Therefore these two queries should result in the same score - or is because term queries do not analyze the search term?

My field is indexed as text. Strings are indexed as an "array" of strings, e.g. "brown","darkbrown"

Amit
  • 30,756
  • 6
  • 57
  • 88
nonNumericalFloat
  • 1,348
  • 2
  • 15
  • 32

1 Answers1

3

Difference between term vs terms query:

  • Term query return documents that contain one or more exact term in a provided field.
  • The terms query is the same as the term query, except you can search for multiple values.
  • Warning: Avoid using the term query for text fields.

As far your this part is concerned

or is because term queries do not analyze the search term?

Yes, It is because the search term does not analyze the term searched. It just matches the exact search term.

Bablu Ahmed
  • 4,412
  • 5
  • 49
  • 64
Umar Hayat
  • 4,300
  • 1
  • 12
  • 27
  • So I will have to use multiple term queries if I want my score to be higher, when there are several colors matching? – nonNumericalFloat Feb 22 '20 at 18:07
  • if there are sever colors to be matched then best practice is to use terms as it is for matching with multiple search terms. – Umar Hayat Feb 22 '20 at 18:20
  • Yes, that is my point. More colors matched -> higher score. But this apparently does not work for `terms` query - no matter how many colors are matched, score is always 1\0. It works though if I use a `term` query – nonNumericalFloat Feb 23 '20 at 13:10
  • "Yes, It is because the search term do not analyze the term searched. It just matches the exact search term." - could you please clarify this – Vasily802 Jun 30 '21 at 19:33