mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-21 03:22:11 +00:00
parent
9bc4bee86f
commit
23ac6d77cf
@ -158,5 +158,13 @@ public final class SearchHitSupport {
|
||||
public SearchHits<T> getSearchHits() {
|
||||
return searchHits;
|
||||
}
|
||||
|
||||
/*
|
||||
* return the same instance as in getSearchHits().getSearchHits()
|
||||
*/
|
||||
@Override
|
||||
public List<SearchHit<T>> getContent() {
|
||||
return searchHits.getSearchHits();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,11 +19,12 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.search.aggregations.Aggregations;
|
||||
import org.springframework.data.util.Lazy;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Basic implementation of {@link SearchScrollHits}
|
||||
* Basic implementation of {@link SearchScrollHits}
|
||||
*
|
||||
* @param <T> the result data class.
|
||||
* @author Peter-Josef Meisch
|
||||
@ -35,9 +36,10 @@ public class SearchHitsImpl<T> implements SearchScrollHits<T> {
|
||||
private final long totalHits;
|
||||
private final TotalHitsRelation totalHitsRelation;
|
||||
private final float maxScore;
|
||||
private final String scrollId;
|
||||
@Nullable private final String scrollId;
|
||||
private final List<? extends SearchHit<T>> searchHits;
|
||||
private final Aggregations aggregations;
|
||||
private final Lazy<List<SearchHit<T>>> unmodifiableSearchHits;
|
||||
@Nullable private final Aggregations aggregations;
|
||||
|
||||
/**
|
||||
* @param totalHits the number of total hits for the search
|
||||
@ -58,6 +60,7 @@ public class SearchHitsImpl<T> implements SearchScrollHits<T> {
|
||||
this.scrollId = scrollId;
|
||||
this.searchHits = searchHits;
|
||||
this.aggregations = aggregations;
|
||||
this.unmodifiableSearchHits = Lazy.of(() -> Collections.unmodifiableList(searchHits));
|
||||
}
|
||||
|
||||
// region getter
|
||||
@ -84,7 +87,7 @@ public class SearchHitsImpl<T> implements SearchScrollHits<T> {
|
||||
|
||||
@Override
|
||||
public List<SearchHit<T>> getSearchHits() {
|
||||
return Collections.unmodifiableList(searchHits);
|
||||
return unmodifiableSearchHits.get();
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -15,12 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* This interface is used to expose the current {@code scrollId} from the underlying scroll context.
|
||||
* <p>
|
||||
* Internal use only.
|
||||
*
|
||||
* @author Sascha Woo
|
||||
* @author Peter-Josef Meisch
|
||||
* @param <T>
|
||||
* @since 4.0
|
||||
*/
|
||||
@ -29,6 +32,7 @@ public interface SearchScrollHits<T> extends SearchHits<T> {
|
||||
/**
|
||||
* @return the scroll id
|
||||
*/
|
||||
@Nullable
|
||||
String getScrollId();
|
||||
|
||||
}
|
||||
|
@ -19,17 +19,23 @@ import static java.util.Collections.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.search.aggregations.Aggregations;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.util.CloseableIterator;
|
||||
|
||||
/**
|
||||
* @author Roman Puchkovskiy
|
||||
* @author Peter-Josef Meisch
|
||||
*/
|
||||
class SearchHitSupportTest {
|
||||
|
||||
@Test // DATAES-772
|
||||
void unwrapsSearchHitsIteratorToCloseableIteratorOfEntities() {
|
||||
TestStringSearchHitsIterator searchHitsIterator = new TestStringSearchHitsIterator();
|
||||
@ -38,6 +44,7 @@ class SearchHitSupportTest {
|
||||
CloseableIterator<String> unwrappedIterator = (CloseableIterator<String>) SearchHitSupport
|
||||
.unwrapSearchHits(searchHitsIterator);
|
||||
|
||||
// noinspection ConstantConditions
|
||||
assertThat(unwrappedIterator.next()).isEqualTo("one");
|
||||
assertThat(unwrappedIterator.next()).isEqualTo("two");
|
||||
assertThat(unwrappedIterator.hasNext()).isFalse();
|
||||
@ -47,6 +54,27 @@ class SearchHitSupportTest {
|
||||
assertThat(searchHitsIterator.closed).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATAES-952
|
||||
@DisplayName("should return the same list instance in SearchHits and getContent")
|
||||
void shouldReturnTheSameListInstanceInSearchHitsAndGetContent() {
|
||||
|
||||
List<SearchHit<String>> hits = new ArrayList<>();
|
||||
hits.add(new SearchHit<>(null, null, 0, null, null, "one"));
|
||||
hits.add(new SearchHit<>(null, null, 0, null, null, "two"));
|
||||
hits.add(new SearchHit<>(null, null, 0, null, null, "three"));
|
||||
hits.add(new SearchHit<>(null, null, 0, null, null, "four"));
|
||||
hits.add(new SearchHit<>(null, null, 0, null, null, "five"));
|
||||
|
||||
SearchHits<String> originalSearchHits = new SearchHitsImpl<>(hits.size(), TotalHitsRelation.EQUAL_TO, 0, "scroll",
|
||||
hits, null);
|
||||
|
||||
SearchPage<String> searchPage = SearchHitSupport.searchPageFor(originalSearchHits, PageRequest.of(0, 3));
|
||||
SearchHits<String> searchHits = searchPage.getSearchHits();
|
||||
|
||||
assertThat(searchHits).isEqualTo(originalSearchHits);
|
||||
assertThat(searchHits.getSearchHits()).isSameAs(searchPage.getContent());
|
||||
}
|
||||
|
||||
private static class TestStringSearchHitsIterator implements SearchHitsIterator<String> {
|
||||
private final Iterator<String> iterator = Arrays.asList("one", "two").iterator();
|
||||
private boolean closed = false;
|
||||
@ -87,4 +115,5 @@ class SearchHitSupportTest {
|
||||
return new SearchHit<>("index", "id", 1.0f, new Object[0], emptyMap(), nextString);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user