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

#1826 - CombinedComparator is created. Added builders for comparators. #1880

Merged
merged 1 commit into from
Apr 8, 2024
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 @@ -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));
}

Expand Down Expand Up @@ -216,7 +218,8 @@ public ElasticStream<T> sorted(Comparator<? super T> 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<? super T>) comparator);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<T extends IndigoRecord> extends IndigoComparator<T> {

protected Collection<IndigoComparator<T>> comparators;

@Override
public Collection<SortBuilder<?>> toSortBuilders() {
return comparators.stream()
.map(IndigoComparator::toSortBuilders)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

public static <T extends IndigoRecord> Builder<T> builder() {
return new Builder<>();
}

public static class Builder<T extends IndigoRecord> {
private Collection<IndigoComparator<T>> comparators;

public Builder<T> withComparators(Collection<IndigoComparator<T>> comparators) {
this.comparators = comparators;
return this;
}

public Builder<T> withComparator(IndigoComparator<T> comparator) {
if (this.comparators == null)
this.comparators = new ArrayList<>();
this.comparators.add(comparator);
return this;
}

public CombinedComparator<T> build() {
CombinedComparator<T> combinedComparator = new CombinedComparator<>();
combinedComparator.comparators = comparators;
return combinedComparator;
}
}

}
Original file line number Diff line number Diff line change
@@ -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<T extends IndigoRecord> extends IndigoComparator<T> {

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<FieldSortBuilder> toSortBuilder() {
return new FieldSortBuilder(this.fieldName).order(this.sortOrder);
public Collection<SortBuilder<?>> toSortBuilders() {
return Collections.singletonList(
SortBuilders.fieldSort(fieldName)
.order(sortOrder)
);
}

public static <T extends IndigoRecord> Builder<T> builder() {
return new Builder<>();
}

public static class Builder<T extends IndigoRecord> {
private String fieldName;
private SortOrder sortOrder;

public Builder<T> withFieldName(String fieldName) {
this.fieldName = fieldName;
return this;
}

public Builder<T> withSortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
return this;
}

public FieldComparator<T> build() {
return new FieldComparator<>(fieldName, sortOrder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends IndigoRecord> implements Comparator<T> {
protected SortOrder sortOrder;

public IndigoComparator(SortOrder sortOrder) {
this.sortOrder = sortOrder;
}
public abstract Collection<SortBuilder<?>> toSortBuilders();

public abstract SortBuilder toSortBuilder();
@Override
public int compare(final T o1, final T o2) {
// does not expect to be called
return 0;
}

}
Original file line number Diff line number Diff line change
@@ -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<T extends IndigoRecord> extends IndigoComparator<T> {

protected SortOrder sortOrder;

public ScoreComparator() {
super(SortOrder.DESC);
this(SortOrder.DESC);
}

public ScoreComparator(SortOrder sortOrder) {
super(sortOrder);
this.sortOrder = sortOrder;
}

@Override
public SortBuilder<ScoreSortBuilder> 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<SortBuilder<?>> toSortBuilders() {
return Collections.singletonList(SortBuilders.scoreSort().order(sortOrder));
}

public static <T extends IndigoRecord> Builder<T> builder() {
return new Builder<>();
}

public static class Builder<T extends IndigoRecord> {
private SortOrder sortOrder;

public Builder<T> withSortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
return this;
}

public ScoreComparator<T> build() {
return new ScoreComparator<>(sortOrder);
}
}

}
Original file line number Diff line number Diff line change
@@ -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<IndigoRecord> fieldComparator = new FieldComparator<>("testField", SortOrder.ASC);
ScoreComparator<IndigoRecord> scoreComparator = new ScoreComparator<>(SortOrder.DESC);

CombinedComparator<IndigoRecord> combinedComparator = CombinedComparator.builder()
.withComparator(fieldComparator)
.withComparator(scoreComparator)
.build();

Collection<SortBuilder<?>> sortBuilders = combinedComparator.toSortBuilders();
assertNotNull(sortBuilders);
assertEquals(2, sortBuilders.size());

List<SortBuilder<?>> sortBuilderList = (List<SortBuilder<?>>) 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<IndigoRecord> fieldComparator = new FieldComparator<>("testField1", SortOrder.ASC);
ScoreComparator<IndigoRecord> scoreComparator = new ScoreComparator<>(SortOrder.DESC);

List<IndigoComparator<IndigoRecord>> comparators = Arrays.asList(fieldComparator, scoreComparator);
CombinedComparator<IndigoRecord> combinedComparator = CombinedComparator.builder()
.withComparators(comparators)
.build();

Collection<SortBuilder<?>> sortBuilders = combinedComparator.toSortBuilders();
assertNotNull(sortBuilders);
assertEquals(2, sortBuilders.size());
}
}

Original file line number Diff line number Diff line change
@@ -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<IndigoRecord> 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<IndigoRecord> comparator = new FieldComparator<>(fieldName, sortOrder);

Collection<SortBuilder<?>> 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<IndigoRecord> builder = FieldComparator.builder();
builder.withFieldName(fieldName);
FieldComparator<IndigoRecord> comparator = builder.build();

assertEquals(fieldName, comparator.getFieldName());
}

@Test
public void testBuilderWithSortOrder() {
SortOrder sortOrder = SortOrder.ASC;
FieldComparator.Builder<IndigoRecord> builder = FieldComparator.builder();
builder.withSortOrder(sortOrder);
FieldComparator<IndigoRecord> comparator = builder.build();

assertEquals(sortOrder, comparator.getSortOrder());
}

@Test
public void testBuilderCompleteFlow() {
String fieldName = "testField";
SortOrder sortOrder = SortOrder.DESC;
FieldComparator<IndigoRecord> comparator = FieldComparator.builder()
.withFieldName(fieldName)
.withSortOrder(sortOrder)
.build();

assertNotNull(comparator);
assertEquals(fieldName, comparator.getFieldName());
assertEquals(sortOrder, comparator.getSortOrder());

Collection<SortBuilder<?>> 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());
}
}
Loading
Loading