Home > Blockchain >  Why score is everytime is 1.0 in elasticsearch
Why score is everytime is 1.0 in elasticsearch

Time:07-12

My json is below

  • I have to match first from sports
  • In that i need to extract search string
  • The issue is i am getting score 1.0 for every document
[{'id':1, 'name': 'christiano ronaldo', 'description': '[email protected]', 'type': 'football', 'var':'sports'},
{'id':2, 'name': 'lionel messi', 'description': '[email protected]','type': 'soccer','var':'sports'},
{'id':3, 'name': 'sachin', 'description': 'was', 'type': 'cricket', 'var':'sports'}]

My dsl query is below

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "var.keyword": [
              "sports"
            ]
          }
        }
      ],
      "filter": {
        "query_string": {
          "query": "sachin* OR messi",
          "fields": [
            "name^1024",
            "description^32"
          ]
        }
      }
    }
  }
}

My out

{'took': 911,
 'timed_out': False,
 '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
 'hits': {'total': {'value': 2, 'relation': 'eq'},
  'max_score': 1.0,
  'hits': [{'_index': 'newtestplayer',
    '_type': '_doc',
    '_id': '2',
    '_score': 1.0,
    '_source': {'id': 2,
     'name': 'lionel messi',
     'description': '[email protected]',
     'type': 'soccer',
     'var': 'sports'}},
   {'_index': 'newtestplayer',
    '_type': '_doc',
    '_id': '3',
    '_score': 1.0,
    '_source': {'id': 3,
     'name': 'sachin',
     'description': 'was',
     'type': 'cricket',
     'var': 'sports'}}]}}

You can see my score is '_score':1.0 for every where. How to change that it will be according to Fields which i mentioned

CodePudding user response:

it is because you are using your query under the filter context which just filters the documents based on the search query, and don't compute the score of search queries.

Refer query and filter context for more info,

In order to fix that, you need to use the query context, proper query should be like

{
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "var.keyword": [
                            "sports"
                        ]
                    }
                },
                {
                    "query_string": {
                        "query": "sachin* OR messi",
                        "fields": [
                            "name^1024",
                            "description^32"
                        ]
                    }
                }
            ]
        }
    }
}

As it returns search results with proper scoring and executed in query context.

  • Related