Filter request and score

Hi all,

I was running some test to understand the filter notion in ES:

POST /index_test/1
{
  "firstname":"John",
  "lastname": "Doe"
  
}

GET /index_test/_search
{
  "query":{
    "term": {
        "firstname.keyword": "John"
      }
  }
}

I don't understand why there is a score in the result set if I am using a filter clause:

"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
  {
    "_index": "index_test",
    "_type": "1",
    "_id": "BfswoGYBR1LGPYDH8oD_",
    "_score": 0.2876821,
    "_source": {
      "firstname": "John",
      "lastname": "Doe"
    }
  }
]
}

The filter clause is missing in your query :

GET /index_test/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "firstname.keyword": "John"
        }
      }
    }
  }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html

Ok ! I have another question related to the scoring of filter results.

My mapping is the following:

{
  "index_test": {
    "mappings": {
      "type1": {
        "properties": {
          "age": {
            "type": "long"
          },
          "firstname": {
            "type": "keyword"
          },
          "lastname": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

How can the following request score the hits while the lastname type is keyword ?

GET /index_test/_search
{
  "query":{
    "term": {
        "firstname": "John"
      }
  }
}

It doesn't matter the field type, you are in a query context, so you will get a score for the documents returned by this query. The filter context implies a filter clause.

The scoring formula will give a score to a field even if it's a keyword. The formula based on the TF/IDF takes in account multiple parameters. (ex. : the number of documents you have on your index.)

https://www.elastic.co/guide/en/elasticsearch/guide/current/scoring-theory.html

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.