SolrQueryUtils { public static String createOrBooleanQuery(SchemaField field, Collection<String> values) { return values.stream().anyMatch(StringUtils::isNotBlank) ? String.format( STANDARD_QUERY_PARSER_FIELD_QUERY_TEMPLATE, field.name(), values.stream() .filter(StringUtils::isNotBlank) .map(SolrQueryUtils::normalize) .distinct() .collect(joining(" OR "))) : ""; } protected  SolrQueryUtils(); static String createOrBooleanQuery(SchemaField field, Collection<String> values); static String createLowerBoundRangeQuery(SchemaField field, double min); static String createUpperBoundRangeQuery(SchemaField field, double max); static String createDoubleBoundRangeQuery(SchemaField field, double min, double max);  }
@Test @DisplayName("Blank field values return an empty clause") void testEmptyValuesInBooleanQuery() { assertThat(SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of())) .isEqualTo(SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of(""))) .isEqualTo(SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of(" "))) .isEqualTo(SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of(" ", "\t"))) .isEqualTo(SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of(" ", "\t", "\n"))) .isEqualTo(SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of(" ", "\t", "\n", "\r"))) .isEqualTo(SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of(" \t\n "))) .isEmpty(); }
@Test @DisplayName("Field values are deduped") void testDuplicatedValuesInBooleanQuery() { assertThat( SolrQueryUtils.createOrBooleanQuery( FIELD1, ImmutableSet.of("value1", "value1", "value2", "value2"))) .isEqualTo(SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of("value1", "value2"))) .containsOnlyOnce("value1") .containsOnlyOnce("value2"); }
@Test @DisplayName("Field values are normalized") void testEscapeBooleanQueryCharacters() { String value1 = " value1 "; String value2 = ") value2+\""; assertThat(SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of(value1, value2))) .containsOnlyOnce("\"" + escapeQueryChars(value1.trim()) + "\"") .containsOnlyOnce("\"" + escapeQueryChars(value2.trim()) + "\""); }
@Test @DisplayName("Boolean query is formatted properly") void testBooleanQueryFormat() { String value1 = "value1"; String value2 = "value2"; String[] splittedOrQuery = SolrQueryUtils.createOrBooleanQuery(FIELD1, ImmutableSet.of(value1, value2)).split(":"); assertThat(splittedOrQuery) .hasSize(2) .containsExactly( FIELD1.name(), "(" + Stream.of("\"" + value1 + "\"", "\"" + value2 + "\"").collect(joining(" OR ")) + ")"); }