-1

I have a data set like below

"hits": [
         {
            "_index": "demotest",
            "_type": "demotest",
            "_id": "2",
            "_score": 1,
            "_source": {
               "name": "Duvvuri ram gopal reddy"
            }
         },
         {
            "_index": "demotest",
            "_type": "demotest",
            "_id": "1",
            "_score": 1,
            "_source": {
               "name": "ram gopal reddy"
            }
         },
         {
            "_index": "demotest",
            "_type": "demotest",
            "_id": "3",
            "_score": 1,
            "_source": {
               "name": "reddy ram gopal"
            }
         }
      ]

When i try to perform a match query with value as ram gopal reddy exact matched record is not showing on top. Query:

GET demotest/_search
{
    "explain": true, 
    "query": {
        "match": {
           "name": "ram gopal reddy"
        }
    }
}

Result after execution of above query:

"hits": [
         {
            "_index": "demotest",
            "_type": "demotest",
            "_id": "2",
            "_score": 0.8630463,
            "_source": {
               "name": "Duvvuri ram gopal reddy"
            }
         },
         {
            "_index": "demotest",
            "_type": "demotest",
            "_id": "1",
            "_score": 0.7594807,
            "_source": {
               "name": "ram gopal reddy"
            }
         },
         {
            "_index": "demotest",
            "_type": "demotest",
            "_id": "3",
            "_score": 0.7594807,
            "_source": {
               "name": "reddy ram gopal"
            }
         }
      ]

How to get exact matched record on top in search results. Also, I posted my complete test here and I want case insensitive search and not phrase search. Thanks

Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
Shashank Gutha
  • 145
  • 2
  • 12
  • What Elasticsearch version is this? – Andrei Stefan May 26 '17 at 08:53
  • I am using elastic 5.1.2 version – Shashank Gutha May 27 '17 at 18:16
  • Strange, even if the 3 words "reddy ram gopal" are in all documents (I mean with a low score according TF/IDF), shorter field should have a best score than long field (according TF/IDF doc here https://www.elastic.co/guide/en/elasticsearch/guide/current/scoring-theory.html), can you post ``_explain`` details please? – Thomas Decaux May 29 '17 at 07:25
  • please find explain details [here](https://gist.github.com/shashankgutha/8757565a6be5f2c57d64e6ff189c2358) – Shashank Gutha May 29 '17 at 07:30

1 Answers1

0

I'd use a bool query with multiple should clauses. One would be set with a phrase query and the other one as a match query.

I wrote a full (but complex example) here: https://gist.github.com/dadoonet/5179ee72ecbf08f12f53d4bda1b76bab

Should give something like:

GET oss/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "name": {
              "query" : "ram gopal reddy",
              "boost": 8.0
            }
          }
        },
        {
          "match": {
            "name": {
              "query": "ram gopal reddy"
            }
          }
        }
      ]
    }
  }
}
dadoonet
  • 14,109
  • 3
  • 42
  • 49
  • I did not get desired result with above answer. I posted my complete test [here](https://gist.github.com/shashankgutha/0b75774d273091590dd0765ee63276af) and I want case insensitive search and not phrase search. – Shashank Gutha May 29 '17 at 07:34