Search code examples
elasticsearchfuzzy-searchelasticsearch-java-api

Fuzzify existing ElasticSearch Java API query


I have an existing ElasticSearch query that uses the Java API:

BoolQueryBuilder queryBuilder = 
     boolQuery().should(queryStringQuery(theUsersQueryString)); 
SearchResponse response = client.prepareSearch(...).setQuery(queryBuilder);

Now I want to add fuzziness to this, to allow minor misspellings to still return something to the user. My guess was that adding fuzziness parameters to the QueryBuilders object would be fruitful:

boolQuery().should(queryStringQuery(theUsersQueryString)
              .fuzziness(Fuzziness.ONE)
              .fuzzyMaxExpansions(4)
              .fuzzyPrefixLength(2));

Unfortunately this doesn't seem to work and I have so far been unable to find good documentation for this. For example, I have the string John Deere in my database. If I use the query string deere I get a match, but not if I use query strings Deeree or Deeer.

My question is: how should I correctly fuzzify my query?


Solution

  • I opted to create a new query rather than modifying my existing one.

    MultiMatchQueryBuilder fuzzyMmQueryBuilder = multiMatchQuery(
                    theUsersQueryString, "field1", "field2", ... , "fieldn").fuzziness("AUTO");
    BoolQueryBuilder b = boolQuery().should(fuzzyMmQueryBuilder); 
    SearchRequestBuilder srb = client.prepareSearch(...).setQuery(b)... 
    SearchResponse res = srb.execute().actionGet();
    

    This query exhibits fuzzy behaviour.