# How to match exact value in elasticsearch?

**URL:** <https://discuss.elastic.co/t/how-to-match-exact-value-in-elasticsearch/21180>\
**Category:** Elasticsearch\
**Created:** [December 10, 2014, 8:18am UTC](https://discuss.elastic.co/t/how-to-match-exact-value-in-elasticsearch/21180 "2014-12-10T08:18:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashish](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashish/32/65891_2.png) [@ashish](https://discuss.elastic.co/u/ashish)\
**Post date:** [December 10, 2014, 8:18am UTC](https://discuss.elastic.co/t/how-to-match-exact-value-in-elasticsearch/21180/1 "2014-12-10T08:18:06Z")

</div>

I have indexed the meta-data of three files and they are of "text/plain" mime-Types.

But when I am trying to match other `mime-types` with "text/plain", following are getting matched!

Here is the list of `mime-types` that are matching with "text/plain", with `hits` & `score`:

```
***********************************
1. Mime-Type text/vnd.motorola.reflex
2. Total Hits 3
3. Max Score 0.07154637
***********************************
1. Mime-Type text/vnd.ms-mediapackage
2. Total Hits 3
3. Max Score 0.034633614
***********************************
1. Mime-Type text/vnd.net2phone.commcenter.command
2. Total Hits 3
3. Max Score 0.07154637
***********************************
1. Mime-Type text/plain
2. Total Hits 3
3. Max Score 0.629606
***********************************

```

**I want that mime-type should exact match** and should consider only last one. If you notice it's giving Max-score greater then all above.

Search Code:

query = "text/plain";  
filter = "mimeType"

```
public long getHitsCount(String query, String filter, Project project) {
        try {
            /*TermQueryBuilder QueryBuilder = new TermQueryBuilder(filter, smartEscapeQuery(query));*/
           /* QueryStringQueryBuilder QueryBuilder = new QueryStringQueryBuilder(smartEscapeQuery(query)).field(filter);*/
            MatchQueryBuilder QueryBuilder = QueryBuilders.matchQuery(filter, smartEscapeQuery(query));
            QueryBuilder qb = QueryBuilders
                    .boolQuery()
                    .must(QueryBuilder);
                    
            SearchRequestBuilder requestBuilder;
                requestBuilder = client.prepareSearch()
                        .setIndices(getDomainIndexId(project))
                        .setTypes(getProjectTypeId(project))
                        .setSearchType(SEARCH_TYPE)
                        .setQuery(qb);

            SearchResponse response = requestBuilder.execute().actionGet(ES_TIMEOUT_MS);
            SearchHits hits = response.getHits();
            if (hits.getTotalHits() > 0) {
                return hits.getTotalHits();
            }else{
                return 0l;
            }
        } catch (IndexMissingException ex) {
            
        }
       return 0;
    } 

/**
     * Escape the string from bad chars for the search
     *
     * @param str the String that should be escaped
     * @return an escaped String
     */
    @SuppressWarnings({"ConstantConditions"})
    private static String smartEscapeQuery(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '\\' || c == '+' || c == '-' || c == '!' || c ==
                    '(' || c == ')' || c == ':'
                    || c == '^' || c == '[' || c == ']' || c == '\"'
                    || c == '{' || c == '}' || c == '~' || c == '/'
                    || c == '?' || c == '|' || c == '&' || c == ';'
                    || (!Character.isSpaceChar(c) &&
                    Character.isWhitespace(c))) {
                sb.append('\\');
            }
            sb.append(c);
        }
        return sb.toString();
    }

```

Match Query:

```
    {
      "bool" : {
        "must" : {
          "match" : {
            "mimeType" : {
              "query" : "text\\/plain",
              "type" : "boolean"
            }
          }
        }
      }
    }
Result: 3 Hits

```

Term Query:

```
{
  "bool" : {
    "must" : {
      "term" : {
        "mimeType" : "text\\/plain"
      }
    }
  }
}

Result: 0 Hits

```

I have tried with both `TermQuery` & `MatchQuery` but it did not work. I am using `AutoDetectParser` while indexing.

**How can I match the exact value in elasticsearch so that in above example it should only match with the "text/plain" NOT with matching ones?**

---

<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:** [July 6, 2017, 12:44am UTC](https://discuss.elastic.co/t/how-to-match-exact-value-in-elasticsearch/21180/2 "2017-07-06T00:44:34Z")

</div>


