Skip to content

Commit

Permalink
Merge pull request #211 from skni-kod/issue-210
Browse files Browse the repository at this point in the history
fix: #210 change filtering in Material Search
  • Loading branch information
marcinbator authored Dec 6, 2024
2 parents 9c474e5 + 34c47ef commit 10cfd4b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ private SearchCriteria createSearchCriteria(@NonNull MaterialControllerDefinitio
));
var categoryIds = filterSearchParams.getCategoryIds();
if (Objects.nonNull(categoryIds) && !categoryIds.isEmpty()) {
categoryIds.forEach(categoryId -> {
criteria.addPhraseField(new SearchCriteria.PhraseField(
"categoryId", String.valueOf(categoryId), false, false
));
});
criteria.addArrayField(new SearchCriteria.ArrayField(
"categoryId", categoryIds.stream().map(String::valueOf).toList()));
}
if (Objects.nonNull(filterSearchParams.getMinAvgGrade()) || Objects.nonNull(filterSearchParams.getMaxAvgGrade()))
criteria.addRangeField(new SearchCriteria.RangeField<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class SearchCriteria {
String anyPhrase;
List<PhraseField> phraseFields = new ArrayList<>();
List<RangeField<?>> rangeFields = new ArrayList<>();
List<ArrayField> arrayFields = new ArrayList<>();
Pageable pageable;

public SearchCriteria(@NonNull String anyPhrase, @NonNull Pageable pageable) {
Expand All @@ -30,6 +31,10 @@ public void addRangeField(RangeField<?> field) {
rangeFields.add(field);
}

public void addArrayField(ArrayField field) {
arrayFields.add(field);
}

@Getter
private abstract static class Field {
private final String name;
Expand Down Expand Up @@ -65,6 +70,16 @@ public PhraseField(String name, String value, boolean wildcard, boolean mustNot)
}
}

@Getter
public static class ArrayField extends Field {
private final List<String> values;

public ArrayField(String name, List<String> values) {
super(name);
this.values = values;
}
}

@Getter
public static class ContentField {
private final String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import io.jsonwebtoken.lang.Assert;
import io.jsonwebtoken.lang.Strings;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch._types.SortOptions;
import org.opensearch.client.opensearch._types.SortOrder;
import org.opensearch.client.opensearch._types.query_dsl.MatchPhraseQuery;
import org.opensearch.client.opensearch._types.query_dsl.Query;
import org.opensearch.client.opensearch._types.query_dsl.RangeQuery;
import org.opensearch.client.opensearch._types.query_dsl.TermsQuery;
import org.opensearch.client.opensearch._types.query_dsl.WildcardQuery;
import org.opensearch.client.opensearch.core.SearchRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -42,6 +44,7 @@ public SearchRequestBuilder(String indexName, SearchCriteria criteria) {
any(criteria.getAnyPhrase());
criteria.getPhraseFields().forEach(this::append);
criteria.getRangeFields().forEach(this::append);
criteria.getArrayFields().forEach(this::append);
}

private void with(Pageable pageable) {
Expand Down Expand Up @@ -74,7 +77,7 @@ private void append(SearchCriteria.PhraseField field) {
var query = field.isWildcard()
? WildcardQuery.of(w -> w.field(field.getName()).value(field.getValue())).toQuery()
: MatchPhraseQuery.of(m -> m.field(field.getName()).query(field.getValue())).toQuery();
(field.isMustNot() ? mustNotQueries : shouldQueries).add(query);
(field.isMustNot() ? mustNotQueries : mustQueries).add(query);
}

private void append(SearchCriteria.RangeField<?> field) {
Expand All @@ -87,6 +90,17 @@ private void append(SearchCriteria.RangeField<?> field) {
mustQueries.add(rangeQueryBuilder.build().toQuery());
}

private void append(SearchCriteria.ArrayField field) {
if (field == null || field.getValues().isEmpty()) {
return;
}
var query = TermsQuery.of(t -> t.field(field.getName())
.terms(terms -> terms.value(field.getValues().stream().map(FieldValue::of).toList()))
).toQuery();
mustQueries.add(query);
}


public SearchRequest build() {
return new SearchRequest.Builder()
.index(indexName)
Expand Down

0 comments on commit 10cfd4b

Please sign in to comment.