0

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

{'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

sim
  • 524
  • 3
  • 14

1 Answers1

1

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.

Amit
  • 30,756
  • 6
  • 57
  • 88
  • Even better is to put the terms query on `var.keyword` in the filter section as it brings nothing to scoring – Val Jul 11 '22 at 10:58
  • @Val can you look into this https://stackoverflow.com/questions/72964121/how-to-add-suggestion-inside-term-query-in-dsl – sim Jul 13 '22 at 10:57