Why adding new document with irrelevant content destroy search score and search relevant documents order?

Viewed 25

I'm trying to figure out why adding new document with irrelevant content destroy search score results.

I wrote this simle code:

from elasticsearch import Elasticsearch


def add_document(es, index_name, content, place, id):
    body = {}
    body["content"] = content
    body["place"] = place
    es.index(index=index_name, body=body, id=id)
    es.indices.refresh(index=index_name)



def get_results(es, index_name, query, place):

    search_query = {
        "query": {
            "bool": {

                "must":
                    [{
                        "query_string": {
                            "default_field": "content",
                            "query":  query
                            }
                    }]
            }
        }
    }

    if place is not None:
        should_query = {
                       "terms": {
                            "place":  [place]
                       }}
        search_query["query"]["bool"]["should"] = should_query

    res = es.search(index=index_name, body=search_query)
    print(f"\nTotal number of results: {len(res['hits']['hits'])}, For query: {query}, Place: {place}")
    for i in range(len(res['hits']['hits'])):
        print(f"Doc id: {res['hits']['hits'][i]['_id']} "
              f" ,Score: {res['hits']['hits'][i]['_score']}, "
              f" ,Place: {res['hits']['hits'][i]['_source']['place']}"
              f" ,Content: {res['hits']['hits'][i]['_source']['content']}")


def main():

    es         = Elasticsearch()
    index_name = "demo_idx"

    # delete old content
    if es.indices.exists(index=index_name) is True:
        es.indices.delete(index=index_name)

    add_document(es=es,
                 index_name=index_name,
                 content="Distributed nature, simple REST APIs, speed, and scalability",
                 place="html",
                 id=1)
    add_document(es=es,
                 index_name=index_name,
                 content="Distributed nature, simple APIs, speed, and scalability",
                 place="html",
                 id=2)
    add_document(es=es,
                 index_name=index_name,
                 content="Known for its simple REST APIs, distributed nature, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, a set of open source tools for data ingestion, enrichment, storage, analysis, and visualization.",
                 place="javascript",
                 id=3)

    get_results(es=es, index_name=index_name, query="simple rest apis distributed nature", place=None)
    get_results(es=es, index_name=index_name, query="simple rest apis distributed nature", place="javascript")

    add_document(es=es,
                 index_name=index_name,
                 content="national geographic",
                 place="javascript",
                 id=4)
    
    get_results(es=es, index_name=index_name, query="simple rest apis distributed nature", place=None)
    get_results(es=es, index_name=index_name, query="simple rest apis distributed nature", place="javascript")


if __name__ == "__main__":
    main()

And I got the following results:

Total number of results: 3, For query: simple rest apis distributed nature, Place: None
Doc id: 1  ,Score: 1.2689934,  ,Place: html ,Content: Distributed nature, simple REST APIs, speed, and scalability
Doc id: 2  ,Score: 0.6970792,  ,Place: html ,Content: Distributed nature, simple APIs, speed, and scalability
Doc id: 3  ,Score: 0.69611007,  ,Place: javascript ,Content: Known for its simple REST APIs, distributed nature, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, a set of open source tools for data ingestion, enrichment, storage, analysis, and visualization.

Total number of results: 3, For query: simple rest apis distributed nature, Place: javascript
Doc id: 3  ,Score: 1.69611,  ,Place: javascript ,Content: Known for its simple REST APIs, distributed nature, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, a set of open source tools for data ingestion, enrichment, storage, analysis, and visualization.
Doc id: 1  ,Score: 1.2689934,  ,Place: html ,Content: Distributed nature, simple REST APIs, speed, and scalability
Doc id: 2  ,Score: 0.6970792,  ,Place: html ,Content: Distributed nature, simple APIs, speed, and scalability

Total number of results: 3, For query: simple rest apis distributed nature, Place: None
Doc id: 1  ,Score: 2.5010183,  ,Place: html ,Content: Distributed nature, simple REST APIs, speed, and scalability
Doc id: 2  ,Score: 1.7494614,  ,Place: html ,Content: Distributed nature, simple APIs, speed, and scalability
Doc id: 3  ,Score: 1.2604496,  ,Place: javascript ,Content: Known for its simple REST APIs, distributed nature, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, a set of open source tools for data ingestion, enrichment, storage, analysis, and visualization.

Total number of results: 3, For query: simple rest apis distributed nature, Place: javascript
Doc id: 1  ,Score: 2.5010183,  ,Place: html ,Content: Distributed nature, simple REST APIs, speed, and scalability
Doc id: 3  ,Score: 2.2604496,  ,Place: javascript ,Content: Known for its simple REST APIs, distributed nature, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, a set of open source tools for data ingestion, enrichment, storage, analysis, and visualization.
Doc id: 2  ,Score: 1.7494614,  ,Place: html ,Content: Distributed nature, simple APIs, speed, and scalability

As you can see, before adding the new document (id=4) I'm getting that document with id=3 has the highest score results when searching with place = javascript.

But after adding new document (id=4, with irrelevant content and same place field (javascript), searching with same query and same place gives that document id=3 dosn't have highest score.

  • I'm using elasticsearch:7.6.2
  • What is the explanation for the results I received?
  • Why are they right?
0 Answers
Related