# Exact matches with match query on analyzed field not showing on top result

**URL:** <https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399>\
**Category:** Elasticsearch\
**Created:** [May 29, 2017, 6:30am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399 "2017-05-29T06:30:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![shashankgutha](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@shashankgutha](https://discuss.elastic.co/u/shashankgutha)\
**Post date:** [May 29, 2017, 6:30am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/1 "2017-05-29T06:30:12Z")

</div>

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
{
    "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. Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 29, 2017, 7:09am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/2 "2017-05-29T07:09:07Z")

</div>

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](https://gist.github.com/dadoonet/5179ee72ecbf08f12f53d4bda1b76bab)

Should give something like:

```auto
GET oss/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "name": {
              "query" : "david",
              "boost": 8.0
            }
          }
        },
        {
          "match": {
            "name": {
              "query": "david"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![shashankgutha](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@shashankgutha](https://discuss.elastic.co/u/shashankgutha)\
**Post date:** [May 29, 2017, 7:22am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/3 "2017-05-29T07:22:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 29, 2017, 7:38am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/4 "2017-05-29T07:38:58Z")

</div>

I don't understand. You said that you want your search to be case insensitive?

But this is the case here:

```auto
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1.7260926,
    "hits": [
      {
        "_index": "demotest",
        "_type": "demotest",
        "_id": "9",
        "_score": 1.7260926,
        "_source": {
          "name": "Dulugunti Ram Gopal Reddy "
        }
      },
      {
        "_index": "demotest",
        "_type": "demotest",
        "_id": "2",
        "_score": 1.7260926,
        "_source": {
          "name": "Duvvuri ram gopal reddy"
        }
      },
      {
        "_index": "demotest",
        "_type": "demotest",
        "_id": "1",
        "_score": 1.5189614,
        "_source": {
          "name": "ram gopal reddy"
        }
      },
      {
        "_index": "demotest",
        "_type": "demotest",
        "_id": "3",
        "_score": 0.7594807,
        "_source": {
          "name": "reddy ram gopal"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![shashankgutha](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@shashankgutha](https://discuss.elastic.co/u/shashankgutha)\
**Post date:** [May 29, 2017, 7:41am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/5 "2017-05-29T07:41:17Z")

</div>

i want **ram gopal reddy** record should come on top even if i search for Ram gopal reddy (R in caps).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 29, 2017, 7:43am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/6 "2017-05-29T07:43:34Z")

</div>

Then add a term query as a new should clause with some boost. That should work.

---

<div class="post-metadata">

**Author:** ![shashankgutha](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@shashankgutha](https://discuss.elastic.co/u/shashankgutha)\
**Post date:** [May 29, 2017, 7:45am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/7 "2017-05-29T07:45:39Z")

</div>

I have tried with that too, but did'nt got desired output.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 29, 2017, 7:53am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/9 "2017-05-29T07:53:27Z")

</div>

May be this then:

```auto
DELETE demotest
PUT demotest
{
  "mappings": {
    "demotest": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "text",
              "analyzer": "keyword"
            }
          }
        }
      }
    }
  }
}
POST /demotest/demotest/2
{
  "name": "Duvvuri ram gopal reddy"
}
POST /demotest/demotest/1
{
  "name": "ram gopal reddy"
}
POST /demotest/demotest/3
{
  "name": "reddy ram gopal"
}
POST /demotest/demotest/9
{
  "name": "Dulugunti Ram Gopal Reddy "
}

#search

GET demotest/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "name.keyword": {
              "value": "ram gopal reddy"
            }
          }
        },
        {
          "match_phrase": {
            "name": {
              "query" : "ram gopal reddy"
            }
          }
        },
        {
          "match": {
            "name": {
              "query": "ram gopal reddy"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![shashankgutha](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@shashankgutha](https://discuss.elastic.co/u/shashankgutha)\
**Post date:** [May 29, 2017, 7:57am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/10 "2017-05-29T07:57:31Z")

</div>

Working with query as ram gopal reddy.  
But if i change query to Ram gopal reddy.  
I am getting the same old results, not exact one on top.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 29, 2017, 8:20am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/11 "2017-05-29T08:20:16Z")

</div>

Sure. It's a combination of things. You basically need to find the right analyzers for your case, apply them on different sub-fields then apply the right searches that you need within a bool query in should clauses as I shown you.

Now I believe you have all the information and you need to try to find how to combine all that to achieve exactly your use case.  
Also you can play a bit with boost factors if needed.

---

<div class="post-metadata">

**Author:** ![shashankgutha](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@shashankgutha](https://discuss.elastic.co/u/shashankgutha)\
**Post date:** [May 29, 2017, 8:32am UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/12 "2017-05-29T08:32:17Z")

</div>

I tried adjusting boost values for match phrase, match and wildcard by keeping all three in bool query with should clause. but not getting desired output. I have been trying from past 6 days.

---

<div class="post-metadata">

**Author:** ![Tetrapack](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Tetrapack](https://discuss.elastic.co/u/Tetrapack)\
**Post date:** [May 29, 2017, 1:09pm UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/13 "2017-05-29T13:09:47Z")

</div>

Concerning the case insensitive thing, and for my use case,

I add a custom lowercase analyser on field "name" and then, when building the request, I lowercase the searched value before execution

for your use case, you could also split the searched value in order to have a request like that:  
(ugly but it works..)

```
GET testindex/type1/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "name": {
              "value": "ram gopal reddy"
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "wildcard": {
                  "name": "*ram*"
                }
              },
              {
                "wildcard": {
                  "name": "*gopal*"
                }
              },
              {
                "wildcard": {
                  "name": "*reddy*"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

you can test that with a not\_alanysed field, if it works for you, replace the not\_analysed with a custom analyser for lowercase..

```
PUT testindex
{
  "mappings": {
    "type1": {
      "properties": {
        "name": {
          "type": "string",
          "index": "not_analyzed"
          }
        }
      }
    }
  }
}
```

---

<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:** [June 26, 2017, 1:10pm UTC](https://discuss.elastic.co/t/exact-matches-with-match-query-on-analyzed-field-not-showing-on-top-result/87399/14 "2017-06-26T13:10:05Z")

</div>

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