diff --git a/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/MaterialSearchService.java b/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/MaterialSearchService.java index 50ccc403..d95d9947 100644 --- a/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/MaterialSearchService.java +++ b/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/MaterialSearchService.java @@ -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<>( diff --git a/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/SearchCriteria.java b/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/SearchCriteria.java index 5f43019c..cd848002 100644 --- a/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/SearchCriteria.java +++ b/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/SearchCriteria.java @@ -15,6 +15,7 @@ public class SearchCriteria { String anyPhrase; List phraseFields = new ArrayList<>(); List> rangeFields = new ArrayList<>(); + List arrayFields = new ArrayList<>(); Pageable pageable; public SearchCriteria(@NonNull String anyPhrase, @NonNull Pageable pageable) { @@ -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; @@ -65,6 +70,16 @@ public PhraseField(String name, String value, boolean wildcard, boolean mustNot) } } + @Getter + public static class ArrayField extends Field { + private final List values; + + public ArrayField(String name, List values) { + super(name); + this.values = values; + } + } + @Getter public static class ContentField { private final String value; diff --git a/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/SearchRequestBuilder.java b/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/SearchRequestBuilder.java index 5a274bf7..d6c1098b 100644 --- a/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/SearchRequestBuilder.java +++ b/kodemy-search/src/main/java/pl/sknikod/kodemysearch/infrastructure/module/material/SearchRequestBuilder.java @@ -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; @@ -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) { @@ -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) { @@ -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)