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

**URL:** <https://discuss.elastic.co/t/filter-search-by-ids-with-bm25-score-in-python-elastic-8-7/330625>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 24, 2023, 9:52am UTC](https://discuss.elastic.co/t/filter-search-by-ids-with-bm25-score-in-python-elastic-8-7/330625 "2023-04-24T09:52:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco\_Rocha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/francisco_rocha/32/119936_2.png) [@Francisco\_Rocha](https://discuss.elastic.co/u/Francisco_Rocha)\
**Post date:** [April 24, 2023, 9:52am UTC](https://discuss.elastic.co/t/filter-search-by-ids-with-bm25-score-in-python-elastic-8-7/330625/1 "2023-04-24T09:52:35Z")

</div>

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.

```auto
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?

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

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 24, 2023, 10:09am UTC](https://discuss.elastic.co/t/filter-search-by-ids-with-bm25-score-in-python-elastic-8-7/330625/2 "2023-04-24T10:09:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Francisco\_Rocha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/francisco_rocha/32/119936_2.png) [@Francisco\_Rocha](https://discuss.elastic.co/u/Francisco_Rocha)\
**Post date:** [April 24, 2023, 2:32pm UTC](https://discuss.elastic.co/t/filter-search-by-ids-with-bm25-score-in-python-elastic-8-7/330625/3 "2023-04-24T14:32:46Z")

</div>

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

I suppose the solution would have to be something like:

```auto
query = "random_text"

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 22, 2023, 2:33pm UTC](https://discuss.elastic.co/t/filter-search-by-ids-with-bm25-score-in-python-elastic-8-7/330625/4 "2023-05-22T14:33:03Z")

</div>

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