diff --git a/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/elastic/ElasticStream.java b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/elastic/ElasticStream.java index dee40aeeb4..74e6c92b23 100644 --- a/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/elastic/ElasticStream.java +++ b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/elastic/ElasticStream.java @@ -153,7 +153,9 @@ private SearchRequest compileRequest(Object[] searchAfterParameters, int batchSi searchSourceBuilder.size(batchSize); if (!comparators.isEmpty()) { - comparators.stream().map(IndigoComparator::toSortBuilder).forEach(searchSourceBuilder::sort); + comparators.stream() + .flatMap(comparator -> comparator.toSortBuilders().stream()) + .forEach(searchSourceBuilder::sort); searchSourceBuilder.sort(new FieldSortBuilder("_doc").order(SortOrder.ASC)); } @@ -216,7 +218,8 @@ public ElasticStream sorted(Comparator comparator) { if (!(comparator instanceof IndigoComparator)) { throw new IllegalArgumentException("Comparator used isn't an IndigoComparator"); } - comparators.add((IndigoComparator) comparator); + this.comparators.clear(); + this.comparators.add((IndigoComparator) comparator); return this; } diff --git a/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/CombinedComparator.java b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/CombinedComparator.java new file mode 100644 index 0000000000..aac484d947 --- /dev/null +++ b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/CombinedComparator.java @@ -0,0 +1,48 @@ +package com.epam.indigo.sort; + +import com.epam.indigo.model.IndigoRecord; +import org.elasticsearch.search.sort.SortBuilder; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.stream.Collectors; + +public class CombinedComparator extends IndigoComparator { + + protected Collection> comparators; + + @Override + public Collection> toSortBuilders() { + return comparators.stream() + .map(IndigoComparator::toSortBuilders) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + } + + public static Builder builder() { + return new Builder<>(); + } + + public static class Builder { + private Collection> comparators; + + public Builder withComparators(Collection> comparators) { + this.comparators = comparators; + return this; + } + + public Builder withComparator(IndigoComparator comparator) { + if (this.comparators == null) + this.comparators = new ArrayList<>(); + this.comparators.add(comparator); + return this; + } + + public CombinedComparator build() { + CombinedComparator combinedComparator = new CombinedComparator<>(); + combinedComparator.comparators = comparators; + return combinedComparator; + } + } + +} diff --git a/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/FieldComparator.java b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/FieldComparator.java index 6edc4118f8..e423c6f33f 100644 --- a/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/FieldComparator.java +++ b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/FieldComparator.java @@ -1,27 +1,59 @@ package com.epam.indigo.sort; import com.epam.indigo.model.IndigoRecord; -import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortBuilder; +import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; +import java.util.Collection; +import java.util.Collections; + public class FieldComparator extends IndigoComparator { protected String fieldName; + protected SortOrder sortOrder; public FieldComparator(final String fieldName, final SortOrder sortOrder) { - super(sortOrder); this.fieldName = fieldName; + this.sortOrder = sortOrder; } - @Override - public int compare(final T o1, final T o2) { - // does not expect to be called - return 0; + public String getFieldName() { + return fieldName; + } + + public SortOrder getSortOrder() { + return sortOrder; } @Override - public SortBuilder toSortBuilder() { - return new FieldSortBuilder(this.fieldName).order(this.sortOrder); + public Collection> toSortBuilders() { + return Collections.singletonList( + SortBuilders.fieldSort(fieldName) + .order(sortOrder) + ); + } + + public static Builder builder() { + return new Builder<>(); + } + + public static class Builder { + private String fieldName; + private SortOrder sortOrder; + + public Builder withFieldName(String fieldName) { + this.fieldName = fieldName; + return this; + } + + public Builder withSortOrder(SortOrder sortOrder) { + this.sortOrder = sortOrder; + return this; + } + + public FieldComparator build() { + return new FieldComparator<>(fieldName, sortOrder); + } } } diff --git a/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/IndigoComparator.java b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/IndigoComparator.java index c0ee0fd106..9fab9456f9 100644 --- a/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/IndigoComparator.java +++ b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/IndigoComparator.java @@ -2,18 +2,19 @@ import com.epam.indigo.model.IndigoRecord; import org.elasticsearch.search.sort.SortBuilder; -import org.elasticsearch.search.sort.SortOrder; +import java.util.Collection; import java.util.Comparator; public abstract class IndigoComparator implements Comparator { - protected SortOrder sortOrder; - public IndigoComparator(SortOrder sortOrder) { - this.sortOrder = sortOrder; - } + public abstract Collection> toSortBuilders(); - public abstract SortBuilder toSortBuilder(); + @Override + public int compare(final T o1, final T o2) { + // does not expect to be called + return 0; + } } diff --git a/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/ScoreComparator.java b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/ScoreComparator.java index aa315264b7..dd30726b1c 100644 --- a/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/ScoreComparator.java +++ b/bingo/bingo-elastic/java/src/main/java/com/epam/indigo/sort/ScoreComparator.java @@ -1,28 +1,49 @@ package com.epam.indigo.sort; import com.epam.indigo.model.IndigoRecord; -import org.elasticsearch.search.sort.ScoreSortBuilder; import org.elasticsearch.search.sort.SortBuilder; +import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; +import java.util.Collection; +import java.util.Collections; + public class ScoreComparator extends IndigoComparator { + protected SortOrder sortOrder; + public ScoreComparator() { - super(SortOrder.DESC); + this(SortOrder.DESC); } public ScoreComparator(SortOrder sortOrder) { - super(sortOrder); + this.sortOrder = sortOrder; } - @Override - public SortBuilder toSortBuilder() { - return new ScoreSortBuilder().order(sortOrder); + public SortOrder getSortOrder() { + return sortOrder; } @Override - public int compare(final T o1, final T o2) { - // does not expect to be called - return 0; + public Collection> toSortBuilders() { + return Collections.singletonList(SortBuilders.scoreSort().order(sortOrder)); + } + + public static Builder builder() { + return new Builder<>(); } + + public static class Builder { + private SortOrder sortOrder; + + public Builder withSortOrder(SortOrder sortOrder) { + this.sortOrder = sortOrder; + return this; + } + + public ScoreComparator build() { + return new ScoreComparator<>(sortOrder); + } + } + } diff --git a/bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/CombinedComparatorTest.java b/bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/CombinedComparatorTest.java new file mode 100644 index 0000000000..78f62e805d --- /dev/null +++ b/bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/CombinedComparatorTest.java @@ -0,0 +1,57 @@ +package com.epam.indigo.sort; + +import com.epam.indigo.model.IndigoRecord; +import org.elasticsearch.search.sort.FieldSortBuilder; +import org.elasticsearch.search.sort.ScoreSortBuilder; +import org.elasticsearch.search.sort.SortBuilder; +import org.elasticsearch.search.sort.SortOrder; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class CombinedComparatorTest { + + @Test + public void testToSortBuilders() { + FieldComparator fieldComparator = new FieldComparator<>("testField", SortOrder.ASC); + ScoreComparator scoreComparator = new ScoreComparator<>(SortOrder.DESC); + + CombinedComparator combinedComparator = CombinedComparator.builder() + .withComparator(fieldComparator) + .withComparator(scoreComparator) + .build(); + + Collection> sortBuilders = combinedComparator.toSortBuilders(); + assertNotNull(sortBuilders); + assertEquals(2, sortBuilders.size()); + + List> sortBuilderList = (List>) sortBuilders; + FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilderList.get(0); + assertEquals("testField", fieldSortBuilder.getFieldName()); + assertEquals(SortOrder.ASC, fieldSortBuilder.order()); + + assertInstanceOf(ScoreSortBuilder.class, sortBuilderList.get(1)); + } + + @Test + public void testBuilderWithComparators() { + FieldComparator fieldComparator = new FieldComparator<>("testField1", SortOrder.ASC); + ScoreComparator scoreComparator = new ScoreComparator<>(SortOrder.DESC); + + List> comparators = Arrays.asList(fieldComparator, scoreComparator); + CombinedComparator combinedComparator = CombinedComparator.builder() + .withComparators(comparators) + .build(); + + Collection> sortBuilders = combinedComparator.toSortBuilders(); + assertNotNull(sortBuilders); + assertEquals(2, sortBuilders.size()); + } +} + diff --git a/bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/FieldComparatorTest.java b/bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/FieldComparatorTest.java new file mode 100644 index 0000000000..016e029197 --- /dev/null +++ b/bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/FieldComparatorTest.java @@ -0,0 +1,92 @@ +package com.epam.indigo.sort; + +import com.epam.indigo.model.IndigoRecord; +import org.elasticsearch.search.sort.FieldSortBuilder; +import org.elasticsearch.search.sort.SortBuilder; +import org.elasticsearch.search.sort.SortOrder; +import org.junit.jupiter.api.Test; + +import java.util.Collection; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; + + +public class FieldComparatorTest { + + @Test + public void testConstructor() { + String fieldName = "testField"; + SortOrder sortOrder = SortOrder.ASC; + FieldComparator comparator = new FieldComparator<>(fieldName, sortOrder); + + assertEquals(fieldName, comparator.getFieldName()); + assertEquals(sortOrder, comparator.getSortOrder()); + } + + @Test + public void testToSortBuilders() { + String fieldName = "testField"; + SortOrder sortOrder = SortOrder.ASC; + FieldComparator comparator = new FieldComparator<>(fieldName, sortOrder); + + Collection> sortBuilders = comparator.toSortBuilders(); + assertNotNull(sortBuilders); + assertEquals(1, sortBuilders.size()); + + SortBuilder sortBuilder = sortBuilders.iterator().next(); + assertNotNull(sortBuilder); + assertInstanceOf(FieldSortBuilder.class, sortBuilder); + + FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilder; + assertEquals(fieldName, fieldSortBuilder.getFieldName()); + assertEquals(sortOrder, fieldSortBuilder.order()); + } + + @Test + public void testBuilderWithFieldName() { + String fieldName = "testField"; + FieldComparator.Builder builder = FieldComparator.builder(); + builder.withFieldName(fieldName); + FieldComparator comparator = builder.build(); + + assertEquals(fieldName, comparator.getFieldName()); + } + + @Test + public void testBuilderWithSortOrder() { + SortOrder sortOrder = SortOrder.ASC; + FieldComparator.Builder builder = FieldComparator.builder(); + builder.withSortOrder(sortOrder); + FieldComparator comparator = builder.build(); + + assertEquals(sortOrder, comparator.getSortOrder()); + } + + @Test + public void testBuilderCompleteFlow() { + String fieldName = "testField"; + SortOrder sortOrder = SortOrder.DESC; + FieldComparator comparator = FieldComparator.builder() + .withFieldName(fieldName) + .withSortOrder(sortOrder) + .build(); + + assertNotNull(comparator); + assertEquals(fieldName, comparator.getFieldName()); + assertEquals(sortOrder, comparator.getSortOrder()); + + Collection> sortBuilders = comparator.toSortBuilders(); + assertNotNull(sortBuilders); + assertEquals(1, sortBuilders.size()); + + SortBuilder sortBuilder = sortBuilders.iterator().next(); + assertNotNull(sortBuilder); + assertInstanceOf(FieldSortBuilder.class, sortBuilder); + + FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilder; + assertEquals(fieldName, fieldSortBuilder.getFieldName()); + assertEquals(sortOrder, fieldSortBuilder.order()); + } +} diff --git a/bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/ScoreComparatorTest.java b/bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/ScoreComparatorTest.java new file mode 100644 index 0000000000..48655b2325 --- /dev/null +++ b/bingo/bingo-elastic/java/src/test/java/com/epam/indigo/sort/ScoreComparatorTest.java @@ -0,0 +1,80 @@ +package com.epam.indigo.sort; + +import com.epam.indigo.model.IndigoRecord; +import org.elasticsearch.search.sort.ScoreSortBuilder; +import org.elasticsearch.search.sort.SortBuilder; +import org.elasticsearch.search.sort.SortOrder; +import org.junit.jupiter.api.Test; + +import java.util.Collection; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class ScoreComparatorTest { + + @Test + public void testDefaultConstructor() { + ScoreComparator comparator = new ScoreComparator<>(); + + assertEquals(SortOrder.DESC, comparator.getSortOrder()); + } + + @Test + public void testParameterizedConstructor() { + SortOrder sortOrder = SortOrder.ASC; + ScoreComparator comparator = new ScoreComparator<>(sortOrder); + assertEquals(sortOrder, comparator.getSortOrder()); + } + + @Test + public void testToSortBuilders() { + SortOrder sortOrder = SortOrder.DESC; + ScoreComparator comparator = new ScoreComparator<>(sortOrder); + + Collection> sortBuilders = comparator.toSortBuilders(); + assertNotNull(sortBuilders); + assertEquals(1, sortBuilders.size()); + + SortBuilder sortBuilder = sortBuilders.iterator().next(); + assertNotNull(sortBuilder); + assertInstanceOf(ScoreSortBuilder.class, sortBuilder); + + ScoreSortBuilder scoreSortBuilder = (ScoreSortBuilder) sortBuilder; + assertEquals(sortOrder, scoreSortBuilder.order()); + } + + @Test + public void testBuilderWithSortOrder() { + SortOrder sortOrder = SortOrder.ASC; + ScoreComparator.Builder builder = ScoreComparator.builder(); + builder.withSortOrder(sortOrder); + ScoreComparator comparator = builder.build(); + + assertEquals(sortOrder, comparator.getSortOrder()); + } + + @Test + public void testBuilderCompleteFlow() { + SortOrder sortOrder = SortOrder.ASC; + ScoreComparator comparator = ScoreComparator.builder() + .withSortOrder(sortOrder) + .build(); + + assertNotNull(comparator); + assertEquals(sortOrder, comparator.getSortOrder()); + + Collection> sortBuilders = comparator.toSortBuilders(); + assertNotNull(sortBuilders); + assertEquals(1, sortBuilders.size()); + + SortBuilder sortBuilder = sortBuilders.iterator().next(); + assertNotNull(sortBuilder); + assertInstanceOf(ScoreSortBuilder.class, sortBuilder); + + ScoreSortBuilder scoreSortBuilder = (ScoreSortBuilder) sortBuilder; + assertEquals(sortOrder, scoreSortBuilder.order()); + } +} +