HibernateSimpleQueryUtils { static List<Pair<Integer, Integer>> getReplaceableTermsOfQuery(String simpleQuery) { var termIndexPairs = new ArrayList<Pair<Integer, Integer>>(); var queryCharCount = simpleQuery.length(); var index = 0; while(index >= 0 && index < queryCharCount) { var currentChar = simpleQuery.charAt(index); if(TERM_DELIMITER.contains(currentChar)) { index++; continue; } if(currentChar == '"') { index = getIndexAfterPhraseTerm(index, simpleQuery); continue; } var nextSpecialCharIndex = getIndexOfFirstAppearanceOfAny(index, SPECIAL_CHARACTERS, simpleQuery); var nextTermDelimiterIndex = getIndexOfFirstAppearanceOfAny(index, TERM_DELIMITER, simpleQuery); if(nextTermDelimiterIndex < 0) { nextTermDelimiterIndex = queryCharCount; } var termEndIndex = nextTermDelimiterIndex - 1; var isSimpleTerm = nextSpecialCharIndex < 0 || nextTermDelimiterIndex < nextSpecialCharIndex; if(isSimpleTerm) { termIndexPairs.add(Pair.of(index, termEndIndex)); index = nextTermDelimiterIndex + 1; } else { index = getIndexAfterFirstAppearanceOfAny(nextSpecialCharIndex, TERM_DELIMITER, simpleQuery); } } return termIndexPairs; }  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 getReplaceableTermsOfQueryReturnsExpectedPairsForComplexQuery() { var simpleQuery = "(test | java) & -jenkins junnit~2 | \"continuous delivery\"~10 (apache wicket) \"kotlin\" (((typescript | spring) & testing & junit*))"; var expectedPairs = List.of( Pair.of(1, 4), Pair.of(8, 11), Pair.of(17, 23), Pair.of(62, 67), Pair.of(69, 74), Pair.of(89, 98), Pair.of(102, 107), Pair.of(112, 118) ); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactlyInAnyOrderElementsOf(expectedPairs); }
@Test public void getReplaceableTermsOfQueryReturnsExpectedPairOfSingleWordQuery() { var simpleQuery = "test"; var expectedPair = Pair.of(0, simpleQuery.length() - 1); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactly(expectedPair); }
@Test public void getReplaceableTermsOfQueryReturnsExpectedPairOfNotQueryWithSingleTerm() { var simpleQuery = "-test"; var expectedPair = Pair.of(1, simpleQuery.length() - 1); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactly(expectedPair); }
@Test public void getReplaceableTermsOfQueryReturnsExpectedPairOfNotQueryWithMultipleTerms() { var simpleQuery = "-(test spring)"; var expectedPairs = List.of( Pair.of(2, 5), Pair.of(7, 12) ); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactlyInAnyOrderElementsOf(expectedPairs); }
@Test public void getReplaceableTermsOfQueryReturnsExpectedPairsOfDisjunctionQueryWithWhitespace() { var simpleQuery = "test | java"; var expectedPairs = List.of( Pair.of(0, 3), Pair.of(7, simpleQuery.length() - 1) ); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactlyInAnyOrderElementsOf(expectedPairs); }
@Test public void getReplaceableTermsOfQueryReturnsExpectedPairsOfDisjunctionQueryWithoutWhitespace() { var simpleQuery = "test|java"; var expectedPairs = List.of( Pair.of(0, 3), Pair.of(5, simpleQuery.length() - 1) ); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactlyInAnyOrderElementsOf(expectedPairs); }
@Test public void getReplaceableTermsOfQueryReturnsExpectedPairsOfConjunctionQueryWithWhitespace() { var simpleQuery = "test & java"; var expectedPairs = List.of( Pair.of(0, 3), Pair.of(7, simpleQuery.length() - 1) ); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactlyInAnyOrderElementsOf(expectedPairs); }
@Test public void getReplaceableTermsOfQueryReturnsExpectedPairsOfConjunctionQueryWithoutWhitespace() { var simpleQuery = "test&java"; var expectedPairs = List.of( Pair.of(0, 3), Pair.of(5, simpleQuery.length() - 1) ); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactlyInAnyOrderElementsOf(expectedPairs); }
@Test public void getReplaceableTermsOfQueryReturnsExpectedPairsOfTermsSeparatedByWhitespace() { var simpleQuery = "test java"; var expectedPairs = List.of( Pair.of(0, 3), Pair.of(5, simpleQuery.length() - 1) ); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactlyInAnyOrderElementsOf(expectedPairs); }
@Test public void getReplaceableTermsOfQueryDoesNotIncludeParentheses() { var simpleQuery = "((test))"; var expectedPair = Pair.of(2, 5); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactly(expectedPair); }
@Test public void getReplaceableTermsOfQueryIgnoresFuzzyTerms() { var simpleQuery = "test~2"; var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).isEmpty(); }
@Test public void getReplaceableTermsOfQueryIgnoresPrefixTerms() { var simpleQuery = "test*"; var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).isEmpty(); }
@Test public void getReplaceableTermsOfQueryIgnoresPhraseTerms() { var simpleQuery = "\"test\""; var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).isEmpty(); }
@Test public void getReplaceableTermsOfQueryIgnoresNearOperatorOfPhraseTerms() { var simpleQuery = "\"test\"~10"; var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).isEmpty(); }
@Test public void getReplaceableTermsOfQueryIgnoresParentheses() { var simpleQuery = "(test)"; var expectedPair = Pair.of(1, 4); var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).containsExactly(expectedPair); }
@Test public void getReplaceableTermsOfQueryIgnoresSinglePrefixOperator() { var simpleQuery = "*"; var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).isEmpty(); }
@Test public void getReplaceableTermsOfQueryIgnoresSingleFuzzyOperator() { var simpleQuery = "~2"; var actualPairs = HibernateSimpleQueryUtils.getReplaceableTermsOfQuery(simpleQuery); assertThat(actualPairs).isEmpty(); }