pythonelasticsearchelasticsearch-dslelasticsearch-dsl-py

Elasticsearch DSL on Python fails to generate Score


I have an Elasticsearch database with several fields that can include name information and am attempting to search it like so:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

client = Elasticsearch()
s = Search(using=client, index="names")
query = 'smith'
fields = ['name1', 'name2']

results = s.query("multi_match", query=query, fields=fields, fuzziness='AUTO')

for hit in results.scan():
    print(hit.meta.score)

The results are:

None
None
None
...

However, if I structure it manually:

results = client.search(index="names",
    body={"size": 100, "query":{
        "multi_match": {
            "query": query, "fields": fields, "fuzziness": 'AUTO'
        }
    }
})

My results are:

{'_index': 'names', '_type': 'Name1', '_id': '1MtYSW4BXryTHXwQ1xBS', '_score': 14.226202, '_source': {...}
{'_index': 'names', '_type': 'Name1', '_id': 'N8tZSW4BXryTHXwQHBfw', '_score': 14.226202, '_source': {...}
{'_index': 'names', '_type': 'Name1', '_id': '8MtZSW4BXryTHXwQeR-i', '_score': 14.226202, '_source': {...}

I would prefer to use elasticsearch-dsl if possible, but I need the score information.


Solution

  • The first version of the code is not equivalent to the second version of the code. Instead of executing a query, the first version uses Scroll API (elasticsearch.helpers.scan).

    Search.query() method build or extend a search object, not send a query to elasticsearch. So the following line of the code is misleading:

    results = s.query("multi_match", query=query, fields=fields, fuzziness='AUTO')
    

    It should be like this:

    # execute() added at the end
    results = s.query("multi_match", query=query, fields=fields, fuzziness='AUTO').execute()
    # scan() removed 
    for hit in results:
        print(hit.meta.score)