Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why score is everytime is 1.0 in elasticsearch

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': 'football@football.com', 'type': 'football', 'var':'sports'},
{'id':2, 'name': 'lionel messi', 'description': 'messi@fifa.com','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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'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': 'messi@fifa.com',
     '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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading