1
votes

Below search query result provides data based in an order when the search keywords are more than one.

{
"query": {
    "query_string" : {
        "query" : "(Sony Music) OR (Sony Music*) OR (*Sony Music) OR (*Sony Music*)",
        "fields" : ["MDMGlobalData.Name1"]
    }
}

}

  1. Exact Matches first.
  2. Then, show those that start with search term.
  3. Then, show those that end with search term.
  4. Then, show the remainder.

But if its just one word, say sony in query data. The order is messed up.

Someone please let me why this is happening? and what's the best approach to have above ordered results using query-string search.

2
What is the mapping of the field mentioned and add some sample values also - Gibbs
@Iniamudhan its been a long time .Did you get a chance to go through my answer, looking forward to get feedback from you and if it's helpful, please don't forget to upvote and accept :) - ESCoder
@BhavyaGupta your answer didn't work as expected for a large set of data. so we used custom function score to account our logic - Iniamudhan
Ohh okay @Iniamudhan, thanks for your reply :) - ESCoder

2 Answers

0
votes

When you only query sony, it should have the lowest score. Is that not what you expect? By default, the query string does seem to take into consideration the order of the OR clauses so I'd say yours is already pretty optimized.

Have you tried tinkering w/ the default_operator option?

Also, what do you mean by sony "being in the query data"? The query string itself or a document whose field MDMGlobalData.Name1 is sony?

0
votes

But if its just one word, say sony in query data. The order is messed up.

Based on your above statement and the comment which you mentioned in the above answer

Adding Working example with sample docs, and search query

Index Sample Data:

{
    "MDMGlobalData":{
        "name":"Sony Music"
    }
}
{
    "MDMGlobalData":{
        "name":"Sony Music Corp"
    }
}
{
    "MDMGlobalData":{
        "name":"All Sony Music Corp"
    }
}
{
    "MDMGlobalData":{
        "name":"Sony"
    }
}

Search Query:

{
  "query": {
    "query_string": {
      "query": "(Sony) OR (Sony*) OR (*Sony) OR (*Sony*)",
      "fields": [
        "MDMGlobalData.name"
      ]
    }
  }
}

Search Result:

"hits": [
        {
            "_index": "foo1",
            "_type": "_doc",
            "_id": "4",
            "_score": 3.1396344,
            "_source": {
                "MDMGlobalData": {
                    "name": "Sony"
                }
            }
        },
        {
            "_index": "foo1",
            "_type": "_doc",
            "_id": "1",
            "_score": 3.114749,
            "_source": {
                "MDMGlobalData": {
                    "name": "Sony Music"
                }
            }
        },
        {
            "_index": "foo1",
            "_type": "_doc",
            "_id": "2",
            "_score": 3.097392,
            "_source": {
                "MDMGlobalData": {
                    "name": "Sony Music Corp"
                }
            }
        },
        {
            "_index": "foo1",
            "_type": "_doc",
            "_id": "3",
            "_score": 3.084596,
            "_source": {
                "MDMGlobalData": {
                    "name": "All Sony Music Corp"
                }
            }
        }
    ]

As you can see the order is still maintained, Sony is having maximum score (as it should be according to the query taken) and then further scoring is done on the basis of the order of the OR clauses.