emraldinho
emraldinho

Reputation: 497

ElasticSearch multi-word query, matching multiple words is more valuable than matching few but many times

I am writing a multi-word search query in ElasticSearch, matching multiple words is more valuable than matching 1 but many many times.

1 query across a few fields:

{
      "bool" : {
        "must" : [
          {
            "simple_query_string" : {
              "query" : "effective date ",
              "fields" : [
                "field1^1.0",
                "field2^5.0",
                "field3^10.0",
              ],
              "flags" : -1,
              "default_operator" : "or",
              "analyze_wildcard" : false,
              "auto_generate_synonyms_phrase_query" : true,
              "fuzzy_prefix_length" : 0,
              "fuzzy_max_expansions" : 50,
              "fuzzy_transpositions" : true,
              "boost" : 1.0
            }
          }
        ],
        "adjust_pure_negative" : true,
        "boost" : 1.0
      }
    }

When I search "effective OR date"

For example:

"This is an example date for effective calculation of the problems"

should score higher than :

"date date date is what he said to the children"

how could I fine tune elasticsearch for this?

Thanks!

Upvotes: 1

Views: 2378

Answers (1)

Bhavya
Bhavya

Reputation: 16192

Since, you have not mentioned in the question about how many fields you have indexed, I have taken only one field i.e title

Indexed Documents:

{
    "title":"This is an example date for effective calculation of the problems"

}
{
    "title":"date date date is what he said to the children"

}

Search Query :

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "effective date",
            "operator": "or",
            "fields": [
             "title"                    --> If you have more fields, you can 
                                            add them here
            ]
          }
        }
      ]
    }
  }
}

Search Result:

"hits": [
        {
            "_index": "my_index",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.85874003,
            "_source": {
                "title": "This is an example date for effective calculation of the problems"
            }
        },
        {
            "_index": "my_index",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.289459,
            "_source": {
                "title": "date date date is what he said to the children"
            }
        }
    ]

To get detailed explaination about Multi-Match query, you can refer this offical documentation

UPDATE 1:

Using query_string

    {
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "effective OR date"
    }
  }
}

To get detailed explaination of query_string you can refer this

UPDATE 2:

Using simple_query_string

{
  "query": {
    "simple_query_string" : {
        "query": "effective date",
        "fields": ["title"],
        "default_operator": "or"
    }
  }
}

By using all the above three search queries, you get the same search result, and there is no difference in the _score

Upvotes: 2

Related Questions