# Strict and fuzzy searching in ES

**URL:** https://discuss.elastic.co/t/strict-and-fuzzy-searching-in-es/117482
**Category:** Elasticsearch
**Created:** [January 29, 2018, 3:06pm UTC](https://discuss.elastic.co/t/strict-and-fuzzy-searching-in-es/117482 "2018-01-29T15:06:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Nikita\_Krasnov](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Nikita\_Krasnov](https://discuss.elastic.co/u/Nikita_Krasnov)
#### Post date: [January 29, 2018, 3:06pm UTC](https://discuss.elastic.co/t/strict-and-fuzzy-searching-in-es/117482/1 "2018-01-29T15:06:52Z")

</div>

Here muy json:

```
{
	
	"office": "office 52",
    "header": "some test"
}

```

I have such Java code:

```
  BoolQueryBuilder bqb = QueryBuilders.boolQuery();
   if (esLogRequest.getHeader() != null) {
            bqb.filter(QueryBuilders.fuzzyQuery("header", esLogRequest.getHeader())
                    .fuzziness(Fuzziness.AUTO));
        }
            if (esLogRequest.getOffice() != null) {
                bqb.must(QueryBuilders.termQuery("office", esLogRequest.getOffice()));
            }

```

I want such result:

1. Searching of header should work with only one part of header or full header. For example: `"header"` in ES is `"some test"` and I want search with part of it - `"test`" or full name `"some test"`
2. I want to search due to strick meaning. For example:`"office"` in ES is `"office 52"` and I want to search only for that meaning `"office 52"`  
In my case first example works when I search for part of text but not full text.  
In second case, it works when in ES is record `"52"` or "office" and I search for`"52"` or `"office"` - it works fine. But when in ES is record `"office 52"` - I can't find such record (maybe because of space).  
What should I do in such cases?

---

<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: [January 29, 2018, 3:21pm UTC](https://discuss.elastic.co/t/strict-and-fuzzy-searching-in-es/117482/2 "2018-01-29T15:21:49Z")

</div>

If you want strict matching, then you need to use a `keyword` type instead of a `text` type for your field.

---

<div class="post-metadata">

### Author: ![Nikita\_Krasnov](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Nikita\_Krasnov](https://discuss.elastic.co/u/Nikita_Krasnov)
#### Post date: [January 29, 2018, 3:27pm UTC](https://discuss.elastic.co/t/strict-and-fuzzy-searching-in-es/117482/3 "2018-01-29T15:27:33Z")

</div>

Ok, for strict fields I will change mapping on "keyword".  
But what should I do for not strict(fuzzy) search? I have discribed problem upper.

---

<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: [January 29, 2018, 3:42pm UTC](https://discuss.elastic.co/t/strict-and-fuzzy-searching-in-es/117482/4 "2018-01-29T15:42:12Z")

</div>

Use a Match query I guess.

Better to provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing.

---

<div class="post-metadata">

### Author: ![Nikita\_Krasnov](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Nikita\_Krasnov](https://discuss.elastic.co/u/Nikita_Krasnov)
#### Post date: [January 30, 2018, 8:34am UTC](https://discuss.elastic.co/t/strict-and-fuzzy-searching-in-es/117482/5 "2018-01-30T08:34:59Z")

</div>

this helps:

```
if (esLogRequest.getHeader() != null) {
            bqb.filter(QueryBuilders.matchPhraseQuery("header", esLogRequest.getHeader()));
        }
```

---

<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: [February 27, 2018, 8:35am UTC](https://discuss.elastic.co/t/strict-and-fuzzy-searching-in-es/117482/6 "2018-02-27T08:35:00Z")

</div>

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