Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : added support for multiple values for CONTAIN, START_WITH and END_WITH operators #11068

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -555,25 +555,97 @@ private static QueryBuilder getQueryBuilderFromCriterionForSingleField(
aspectRetriever)
.queryName(queryName != null ? queryName : fieldName);
} else if (condition == Condition.CONTAIN) {
return QueryBuilders.wildcardQuery(
toKeywordField(criterion.getField(), isTimeseries, aspectRetriever),
"*" + ESUtils.escapeReservedCharacters(criterion.getValue().trim()) + "*")
.queryName(queryName != null ? queryName : fieldName);
return buildContainsConditionFromCriterion(
fieldName, criterion, queryName, isTimeseries, aspectRetriever);
} else if (condition == Condition.START_WITH) {
return QueryBuilders.wildcardQuery(
toKeywordField(criterion.getField(), isTimeseries, aspectRetriever),
ESUtils.escapeReservedCharacters(criterion.getValue().trim()) + "*")
.queryName(queryName != null ? queryName : fieldName);
return buildStartsWithConditionFromCriterion(
fieldName, criterion, queryName, isTimeseries, aspectRetriever);
} else if (condition == Condition.END_WITH) {
return QueryBuilders.wildcardQuery(
toKeywordField(criterion.getField(), isTimeseries, aspectRetriever),
"*" + ESUtils.escapeReservedCharacters(criterion.getValue().trim()))
.queryName(queryName != null ? queryName : fieldName);
return buildEndsWithConditionFromCriterion(
fieldName, criterion, queryName, isTimeseries, aspectRetriever);
}
}
throw new UnsupportedOperationException("Unsupported condition: " + condition);
}

private static QueryBuilder buildWildcardQueryWithMultipleValues(
@Nonnull final String fieldName,
@Nonnull final Criterion criterion,
final boolean isTimeseries,
@Nullable String queryName,
@Nonnull AspectRetriever aspectRetriever,
String wildcardPattern) {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

for (String value : criterion.getValues()) {
boolQuery.should(
QueryBuilders.wildcardQuery(
toKeywordField(criterion.getField(), isTimeseries, aspectRetriever),
String.format(wildcardPattern, ESUtils.escapeReservedCharacters(value.trim())))
.queryName(queryName != null ? queryName : fieldName));
}
return boolQuery;
}

private static QueryBuilder buildWildcardQueryWithSingleValue(
@Nonnull final String fieldName,
@Nonnull final Criterion criterion,
final boolean isTimeseries,
@Nullable String queryName,
@Nonnull AspectRetriever aspectRetriever,
String wildcardPattern) {
return QueryBuilders.wildcardQuery(
toKeywordField(criterion.getField(), isTimeseries, aspectRetriever),
String.format(
wildcardPattern, ESUtils.escapeReservedCharacters(criterion.getValue().trim())))
.queryName(queryName != null ? queryName : fieldName);
}

private static QueryBuilder buildContainsConditionFromCriterion(
@Nonnull final String fieldName,
@Nonnull final Criterion criterion,
@Nullable String queryName,
final boolean isTimeseries,
@Nonnull AspectRetriever aspectRetriever) {

if (!criterion.getValues().isEmpty()) {
return buildWildcardQueryWithMultipleValues(
fieldName, criterion, isTimeseries, queryName, aspectRetriever, "*%s*");
}
return buildWildcardQueryWithSingleValue(
fieldName, criterion, isTimeseries, queryName, aspectRetriever, "*%s*");
}

private static QueryBuilder buildStartsWithConditionFromCriterion(
@Nonnull final String fieldName,
@Nonnull final Criterion criterion,
@Nullable String queryName,
final boolean isTimeseries,
@Nonnull AspectRetriever aspectRetriever) {

if (!criterion.getValues().isEmpty()) {
return buildWildcardQueryWithMultipleValues(
fieldName, criterion, isTimeseries, queryName, aspectRetriever, "%s*");
}
return buildWildcardQueryWithSingleValue(
fieldName, criterion, isTimeseries, queryName, aspectRetriever, "%s*");
}

private static QueryBuilder buildEndsWithConditionFromCriterion(
@Nonnull final String fieldName,
@Nonnull final Criterion criterion,
@Nullable String queryName,
final boolean isTimeseries,
@Nonnull AspectRetriever aspectRetriever) {

if (!criterion.getValues().isEmpty()) {
return buildWildcardQueryWithMultipleValues(
fieldName, criterion, isTimeseries, queryName, aspectRetriever, "*%s");
}
return buildWildcardQueryWithSingleValue(
fieldName, criterion, isTimeseries, queryName, aspectRetriever, "*%s");
}

private static QueryBuilder buildEqualsConditionFromCriterion(
@Nonnull final String fieldName,
@Nonnull final Criterion criterion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,197 @@ public void testGetQueryBuilderFromCriterionEqualsValues() {
Assert.assertEquals(result.toString(), expected);
}

@Test
public void testGetQueryBuilderFromCriterionContain() {
final Criterion singleValueCriterion =
new Criterion().setField("myTestField").setCondition(Condition.CONTAIN).setValue("value1");

QueryBuilder result =
ESUtils.getQueryBuilderFromCriterion(
singleValueCriterion, false, new HashMap<>(), mock(AspectRetriever.class));

String expected =
"{\n"
+ " \"wildcard\" : {\n"
+ " \"myTestField.keyword\" : {\n"
+ " \"wildcard\" : \"*value1*\",\n"
+ " \"boost\" : 1.0,\n"
+ " \"_name\" : \"myTestField\"\n"
+ " }\n"
+ " }\n"
+ "}";

Assert.assertEquals(result.toString(), expected);

final Criterion multiValueCriterion =
new Criterion()
.setField("myTestField")
.setCondition(Condition.CONTAIN)
.setValues(new StringArray(ImmutableList.of("value1", "value2")));

result =
ESUtils.getQueryBuilderFromCriterion(
multiValueCriterion, false, new HashMap<>(), mock(AspectRetriever.class));

expected =
"{\n"
+ " \"bool\" : {\n"
+ " \"should\" : [\n"
+ " {\n"
+ " \"wildcard\" : {\n"
+ " \"myTestField.keyword\" : {\n"
+ " \"wildcard\" : \"*value1*\",\n"
+ " \"boost\" : 1.0,\n"
+ " \"_name\" : \"myTestField\"\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"wildcard\" : {\n"
+ " \"myTestField.keyword\" : {\n"
+ " \"wildcard\" : \"*value2*\",\n"
+ " \"boost\" : 1.0,\n"
+ " \"_name\" : \"myTestField\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " ],\n"
+ " \"adjust_pure_negative\" : true,\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ "}";

Assert.assertEquals(result.toString(), expected);
}

@Test
public void testWildcardQueryBuilderFromCriterionWhenStartsWith() {
final Criterion singleValueCriterion =
new Criterion()
.setField("myTestField")
.setCondition(Condition.START_WITH)
.setValue("value1");

QueryBuilder result =
ESUtils.getQueryBuilderFromCriterion(
singleValueCriterion, false, new HashMap<>(), mock(AspectRetriever.class));

String expected =
"{\n"
+ " \"wildcard\" : {\n"
+ " \"myTestField.keyword\" : {\n"
+ " \"wildcard\" : \"value1*\",\n"
+ " \"boost\" : 1.0,\n"
+ " \"_name\" : \"myTestField\"\n"
+ " }\n"
+ " }\n"
+ "}";

Assert.assertEquals(result.toString(), expected);

final Criterion multiValueCriterion =
new Criterion()
.setField("myTestField")
.setCondition(Condition.START_WITH)
.setValues(new StringArray(ImmutableList.of("value1", "value2")));

result =
ESUtils.getQueryBuilderFromCriterion(
multiValueCriterion, false, new HashMap<>(), mock(AspectRetriever.class));

expected =
"{\n"
+ " \"bool\" : {\n"
+ " \"should\" : [\n"
+ " {\n"
+ " \"wildcard\" : {\n"
+ " \"myTestField.keyword\" : {\n"
+ " \"wildcard\" : \"value1*\",\n"
+ " \"boost\" : 1.0,\n"
+ " \"_name\" : \"myTestField\"\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"wildcard\" : {\n"
+ " \"myTestField.keyword\" : {\n"
+ " \"wildcard\" : \"value2*\",\n"
+ " \"boost\" : 1.0,\n"
+ " \"_name\" : \"myTestField\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " ],\n"
+ " \"adjust_pure_negative\" : true,\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ "}";

Assert.assertEquals(result.toString(), expected);
}

@Test
public void testWildcardQueryBuilderFromCriterionWhenEndsWith() {
final Criterion singleValueCriterion =
new Criterion().setField("myTestField").setCondition(Condition.END_WITH).setValue("value1");

QueryBuilder result =
ESUtils.getQueryBuilderFromCriterion(
singleValueCriterion, false, new HashMap<>(), mock(AspectRetriever.class));

String expected =
"{\n"
+ " \"wildcard\" : {\n"
+ " \"myTestField.keyword\" : {\n"
+ " \"wildcard\" : \"*value1\",\n"
+ " \"boost\" : 1.0,\n"
+ " \"_name\" : \"myTestField\"\n"
+ " }\n"
+ " }\n"
+ "}";
Assert.assertEquals(result.toString(), expected);

final Criterion multiValueCriterion =
new Criterion()
.setField("myTestField")
.setCondition(Condition.END_WITH)
.setValues(new StringArray(ImmutableList.of("value1", "value2")));

result =
ESUtils.getQueryBuilderFromCriterion(
multiValueCriterion, false, new HashMap<>(), mock(AspectRetriever.class));

expected =
"{\n"
+ " \"bool\" : {\n"
+ " \"should\" : [\n"
+ " {\n"
+ " \"wildcard\" : {\n"
+ " \"myTestField.keyword\" : {\n"
+ " \"wildcard\" : \"*value1\",\n"
+ " \"boost\" : 1.0,\n"
+ " \"_name\" : \"myTestField\"\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"wildcard\" : {\n"
+ " \"myTestField.keyword\" : {\n"
+ " \"wildcard\" : \"*value2\",\n"
+ " \"boost\" : 1.0,\n"
+ " \"_name\" : \"myTestField\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " ],\n"
+ " \"adjust_pure_negative\" : true,\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ "}";

Assert.assertEquals(result.toString(), expected);
}

@Test
public void testGetQueryBuilderFromCriterionExists() {
final Criterion singleValueCriterion =
Expand Down
Loading