HibernateSimpleQueryUtils { public static String makeQueryPrefixAndFuzzy(@NonNull String simpleQuery) { var trimmedSimpleQuery = simpleQuery.trim(); var replaceableTermIndexPairs = getReplaceableTermsOfQuery(trimmedSimpleQuery); if(replaceableTermIndexPairs.isEmpty()) { return trimmedSimpleQuery; } var fuzzyAndPrefixQuery = trimmedSimpleQuery; var addedOffsetByReplacingTerms = 0; for(var pair : replaceableTermIndexPairs) { var termStartIndex = pair.getFirst() + addedOffsetByReplacingTerms; var termEndIndex = pair.getSecond() + addedOffsetByReplacingTerms; var termStartEndIndexPair = Pair.of(termStartIndex, termEndIndex); var preReplacementQueryLength = fuzzyAndPrefixQuery.length(); fuzzyAndPrefixQuery = replaceTermWithFuzzyAndPrefixDisjunction(termStartEndIndexPair, fuzzyAndPrefixQuery); var postReplacementQueryLength = fuzzyAndPrefixQuery.length(); addedOffsetByReplacingTerms += postReplacementQueryLength - preReplacementQueryLength; } return fuzzyAndPrefixQuery; }  static String makeQueryPrefixAndFuzzy(@NonNull String simpleQuery); static String createLuceneQueryString(Collection<String> values, String operator,
                                                  Function<String, String> function); static String createLuceneQueryString(Collection<String> values, String operator);  }
@Test public void makeQueryPrefixAndFuzzyReturnsExpectedQuery() { var givenSimpleQuery = "((java | junit) & mockito~2) | spring* | \"kotlin\""; var expectedSimpleQuery = "(((java | java~1 | java*) | (junit | junit~1 | junit*)) & mockito~2) | spring* | \"kotlin\""; var actualQuery = HibernateSimpleQueryUtils.makeQueryPrefixAndFuzzy(givenSimpleQuery); assertThat(actualQuery).isEqualTo(expectedSimpleQuery); }
@Test public void makeQueryPrefixAndFuzzyReplacesNonExplicitTerms() { var givenSimpleQuery = "java"; var expectedSimpleQuery = "(java | java~1 | java*)"; var actualQuery = HibernateSimpleQueryUtils.makeQueryPrefixAndFuzzy(givenSimpleQuery); assertThat(actualQuery).isEqualTo(expectedSimpleQuery); }
@Test public void makeQueryPrefixAndFuzzyDoesNotReplaceFuzzyTerm() { var expectedSimpleQuery = "java~2"; var actualQuery = HibernateSimpleQueryUtils.makeQueryPrefixAndFuzzy(expectedSimpleQuery); assertThat(actualQuery).isEqualTo(expectedSimpleQuery); }
@Test public void makeQueryPrefixAndFuzzyDoesNotReplacePrefixTerms() { var expectedSimpleQuery = "java*"; var actualQuery = HibernateSimpleQueryUtils.makeQueryPrefixAndFuzzy(expectedSimpleQuery); assertThat(actualQuery).isEqualTo(expectedSimpleQuery); }
@Test public void makeQueryPrefixAndFuzzyDoesNotReplacePhraseTerms() { var expectedSimpleQuery = "\"java\""; var actualQuery = HibernateSimpleQueryUtils.makeQueryPrefixAndFuzzy(expectedSimpleQuery); assertThat(actualQuery).isEqualTo(expectedSimpleQuery); }
@Test public void makeQueryPrefixAndFuzzyDoesNotReplacePhraseTermsWithNearOperator() { var expectedSimpleQuery = "\"java pro\"~12"; var actualQuery = HibernateSimpleQueryUtils.makeQueryPrefixAndFuzzy(expectedSimpleQuery); assertThat(actualQuery).isEqualTo(expectedSimpleQuery); }