# Cannot do the fuzzy match with Java RestHighLevelClient

**URL:** <https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854>\
**Category:** Elasticsearch\
**Created:** [March 12, 2019, 3:54am UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854 "2019-03-12T03:54:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)\
**Post date:** [March 12, 2019, 3:54am UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/1 "2019-03-12T03:54:39Z")

</div>

We are trying to use elasticsearch to do some inverted index (e.g. name -\> id index). We are using Amazon ElasticSearch. And from the client side, I used Java Rest High Level API and build SearchRequest as follows:

```auto
SearchRequest buildSearchRequest(ResolutionRequest request) {
    SearchSourceBuilder searchSourceBuilder =
        new SearchSourceBuilder()
            .query(
                QueryBuilders.queryStringQuery(request.getInput())
                    .fuzziness(Fuzziness.TWO)
                    .fuzzyPrefixLength(3)
                    .fuzzyMaxExpansions(10));
    request.getSizeOptional().map(val -> searchSourceBuilder.size(val.intValue()));
    request.getOffsetOptional().map(val -> searchSourceBuilder.from(val.intValue()));
    return new SearchRequest()
        .source(searchSourceBuilder)
        .searchType(SearchType.DFS_QUERY_THEN_FETCH);

```

But it turns out it cannot do the fuzzy match even I set the Fuziness to TWO (which is the highest level that can be set). For example, if the index has "cabin", when user input "cabins", there's no results returned.

Am I forming fuzzy query in a wrong way or did I miss anything?

To follow up with David's request, here is the index settings and things I plan to do:

```
DELETE index
PUT index/_doc/type_name
{
   "id": "id1"
   "name": "cabin"
   "indexed_at": "2019-03-01"
}
GET index/_search
{
  "query": {
      "query_string": {
            "query": "cabins"
            "fuzziness": "TWO"
            "fuzzy_prefix_length": 3
            "fuzzy_max_expansions": 10
     }
  }
}

```

Thank you!

---

<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:** [March 12, 2019, 10:52am UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/2 "2019-03-12T10:52:32Z")

</div>

Please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

This is the icon to use if you are not using markdown format:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7e6e239431ec2d71cbf1beef741f2e93e7cc762c.jpg)

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.  
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.

I updated your post but please do it next time. Thanks!

Could you 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. Please, try to keep the example as simple as possible and don't do it in Java but just with Kibana dev console (as shown in the example).

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)\
**Post date:** [March 12, 2019, 5:10pm UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/3 "2019-03-12T17:10:29Z")

</div>

Thanks for the modification, David! That's help the readability a lot!

Regarding the recreation script, I think it might related to Java client question, therefore I pasted the Java code. (not sure how this java code is translated to recreation script :()

But I did refine the question and put more details. Thanks so much for the tips

---

<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:** [March 12, 2019, 7:39pm UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/4 "2019-03-12T19:39:49Z")

</div>

> [@flash](#):
>
> Regarding the recreation script, I think it might related to Java client question

I don't think so. That's what I'd like to figure out.

So please provide a full example, index settings, mappings, documents, and search that we can use to reproduce and fix your problem or explain why the behavior is what you see.

---

<div class="post-metadata">

**Author:** ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)\
**Post date:** [March 12, 2019, 8:38pm UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/5 "2019-03-12T20:38:34Z")

</div>

Gotcha! I have added some recreation script. Let me know if it makes sense or if I need to add more information.

---

<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:** [March 13, 2019, 2:48am UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/6 "2019-03-13T02:48:09Z")

</div>

A match query works well.

```auto
DELETE index
PUT index/_doc/type_name
{
  "name": "cabin"
}
GET index/_search
{
  "query": {
    "match": {
      "name": {
        "query": "cabins",
        "fuzziness": 2
      }
    }
  }
}

```

If you want to use the query string query or the simple query string query, you need to use `~` to indicate that you want to apply fuzzy on a given term:

```auto
DELETE index
PUT index/_doc/type_name
{
  "name": "cabin"
}
GET index/_search
{
  "query": {
    "query_string": {
      "query": "cabins~"
    }
  }
}

```

I hope this helps.

---

<div class="post-metadata">

**Author:** ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)\
**Post date:** [March 13, 2019, 3:13am UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/7 "2019-03-13T03:13:02Z")

</div>

Wow, that's really helpful!

Thank you so much!

Btw, can I know what's the difference behind this, and also since the query\_string also have a fuzziness configurations in the api.

---

<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:** [March 13, 2019, 6:41pm UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/8 "2019-03-13T18:41:52Z")

</div>

I don't know. @jimczi?

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [March 14, 2019, 12:02am UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/9 "2019-03-14T00:02:35Z")

</div>

The fuzziness option in the `query_string` is about changing the default value when you don't provide a numeric value after the `~` operator. The default is `AUTO` (see [https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness))  
but you can change the default or provide an explicit value (e.g. `cabins~1`).  
Regarding the `match` query, since it doesn't accept operators the fuzziness option is applied to each term automatically.

---

<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:** [March 14, 2019, 1:11am UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/10 "2019-03-14T01:11:37Z")

</div>

I thought that `foo~` meant `fuzziness: AUTO` and `foo~1` meant `fuzziness: 1`. Good to know. Thanks @jimczi.

---

<div class="post-metadata">

**Author:** ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)\
**Post date:** [March 14, 2019, 9:09pm UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/11 "2019-03-14T21:09:55Z")

</div>

Good to know! thanks @jimczi and @dadoonet !!

---

<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:** [April 11, 2019, 9:09pm UTC](https://discuss.elastic.co/t/cannot-do-the-fuzzy-match-with-java-resthighlevelclient/171854/12 "2019-04-11T21:09:57Z")

</div>

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