Skip to content

Commit

Permalink
fix: add 'CONTAIN' support in query builder
Browse files Browse the repository at this point in the history
This fix to address the issue #114
  • Loading branch information
yangyangv2 committed Jul 22, 2021
1 parent 77df5d8 commit 045cfe2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public static QueryBuilder getQueryBuilderFromCriterion(@Nonnull Criterion crite
return QueryBuilders.rangeQuery(criterion.getField()).lt(criterion.getValue().trim());
} else if (condition == Condition.LESS_THAN_OR_EQUAL_TO) {
return QueryBuilders.rangeQuery(criterion.getField()).lte(criterion.getValue().trim());
} else if (condition == Condition.CONTAIN) {
return QueryBuilders.termQuery(criterion.getField(), criterion.getValue().trim());
}

throw new UnsupportedOperationException("Unsupported condition: " + condition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void testFilteredQueryUnsupportedCondition() {
int from = 0;
int size = 10;
final Filter filter2 = new Filter().setCriteria(new CriterionArray(Arrays.asList(
new Criterion().setField("field_contain").setValue("value_contain").setCondition(Condition.CONTAIN))));
new Criterion().setField("field_contain").setValue("value_contain").setCondition(Condition.START_WITH))));
SortCriterion sortCriterion = new SortCriterion().setOrder(SortOrder.ASCENDING).setField("urn");
assertThrows(UnsupportedOperationException.class,
() -> _searchDAO.getFilteredSearchQuery(filter2, sortCriterion, from, size));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public static QueryBuilder getQueryBuilderFromCriterion(@Nonnull Criterion crite
return QueryBuilders.rangeQuery(criterion.getField()).lt(criterion.getValue().trim());
} else if (condition == Condition.LESS_THAN_OR_EQUAL_TO) {
return QueryBuilders.rangeQuery(criterion.getField()).lte(criterion.getValue().trim());
} else if (condition == Condition.CONTAIN) {
return QueryBuilders.matchQuery(criterion.getField(), criterion.getValue().trim());
}

throw new UnsupportedOperationException("Unsupported condition: " + condition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public void testFilteredQueryUnsupportedCondition() {
int from = 0;
int size = 10;
final Filter filter2 = new Filter().setCriteria(new CriterionArray(Arrays.asList(
new Criterion().setField("field_contain").setValue("value_contain").setCondition(Condition.CONTAIN))));
new Criterion().setField("field_contain").setValue("value_contain").setCondition(Condition.START_WITH))));
SortCriterion sortCriterion = new SortCriterion().setOrder(SortOrder.ASCENDING).setField("urn");
assertThrows(UnsupportedOperationException.class,
() -> _searchDAO.getFilteredSearchQuery(filter2, sortCriterion, from, size));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.testng.annotations.Test;

import static com.linkedin.metadata.dao.utils.SearchUtils.*;
Expand Down Expand Up @@ -37,4 +39,19 @@ public void testGetRequestMap() {
));
assertThrows(UnsupportedOperationException.class, () -> getRequestMap(filter3));
}

@Test
public void testGetQueryBuilderFromCriterion() {

// given a 'contain' criterion
Criterion containCriterion = new Criterion();
containCriterion.setValue("match text");
containCriterion.setCondition(Condition.CONTAIN);
containCriterion.setField("text");

// verify 'contain' criterion creates a MatchQueryBuilder
QueryBuilder queryBuilder = SearchUtils.getQueryBuilderFromCriterion(containCriterion);
assertNotNull(queryBuilder);
assertTrue(queryBuilder instanceof MatchQueryBuilder);
}
}

0 comments on commit 045cfe2

Please sign in to comment.