Filter search by ids with BM25 score in Python (Elastic 8.7)

Hi folks, I'm working with Python client and would like to know if it is possible to do a search by target ids with BM25.

I have a 500k index and a list of ids that I would like to filter and at the same time obtain BM25 score.

doc_ids = ["1", "2", "3"]

# specify the query to retrieve the BM25 score
bm25_query = {"bool": {"filter": [{"terms": {"_id": doc_ids}}]}}

bm25_results = es.search(
        index=idx_name,
        size=topk,
        request_timeout=30,
        query=bm25_query,
        explain=True
    )

How do I get the score without getting 0.0 or 1.0?

for hit in bm25_results["hits"]["hits"]:
    print(f"Document BM25 Score: {hit['_score']}")

If you want a relevancy score you need to provide a query relevancy can be evaluated against. If you just filter IDs documents are included or excluded and there is no ranking with respect to relevancy.

In your example, which documents would be more relevant and why?

1 Like

i have a variable called "query" that I want to find relevant documents for.

I suppose the solution would have to be something like:

query = "random_text"

 bm25_query = {
        "bool": {
            "must": [
                { "match": { "content": { "query": query } } },
            ],
        "filter": {
            "terms": {
                "_id": doc_ids,
            }
        }
        }
    }

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