Polishing.

This commit is contained in:
Peter-Josef Meisch 2021-03-11 19:13:34 +01:00
parent 4dc8b2589a
commit eb816cce87
No known key found for this signature in database
GPG Key ID: DE108246970C7708
5 changed files with 34 additions and 27 deletions

View File

@ -30,7 +30,7 @@ import org.springframework.util.Assert;
/**
* Encapsulates the found data with additional information from the search.
*
*
* @param <T> the result data class.
* @author Peter-Josef Meisch
* @author Matt Gilene
@ -48,8 +48,13 @@ public class SearchHit<T> {
@Nullable private final NestedMetaData nestedMetaData;
@Nullable private final String routing;
@Nullable private final Explanation explanation;
@Nullable private final List<String> matchedQueries;
private final List<String> matchedQueries = new ArrayList<>();
/**
* @deprecated since 4.2 use
* {@link #SearchHit(String, String, String, float, Object[], Map, Map, NestedMetaData, Explanation, List, Object)}.
*/
@Deprecated
public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score,
@Nullable Object[] sortValues, @Nullable Map<String, List<String>> highlightFields, T content) {
this(index, id, routing, score, sortValues, highlightFields, null, null, null, null, content);
@ -58,8 +63,7 @@ public class SearchHit<T> {
public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score,
@Nullable Object[] sortValues, @Nullable Map<String, List<String>> highlightFields,
@Nullable Map<String, SearchHits<?>> innerHits, @Nullable NestedMetaData nestedMetaData,
@Nullable Explanation explanation,
@Nullable List<String> matchedQueries, T content) {
@Nullable Explanation explanation, @Nullable List<String> matchedQueries, T content) {
this.index = index;
this.id = id;
this.routing = routing;
@ -77,7 +81,10 @@ public class SearchHit<T> {
this.nestedMetaData = nestedMetaData;
this.explanation = explanation;
this.content = content;
this.matchedQueries = matchedQueries;
if (matchedQueries != null) {
this.matchedQueries.addAll(matchedQueries);
}
}
/**
@ -125,7 +132,7 @@ public class SearchHit<T> {
/**
* gets the highlight values for a field.
*
*
* @param field must not be {@literal null}
* @return possibly empty List, never null
*/
@ -141,7 +148,7 @@ public class SearchHit<T> {
* nested entity class, the returned data will be of this type, otherwise
* {{@link org.springframework.data.elasticsearch.core.document.SearchDocument}} instances are returned in this
* {@link SearchHits} object.
*
*
* @param name the inner hits name
* @return {@link SearchHits} if available, otherwise {@literal null}
*/
@ -160,7 +167,7 @@ public class SearchHit<T> {
/**
* If this is a nested inner hit, return the nested metadata information
*
*
* @return {{@link NestedMetaData}
* @since 4.1
*/

View File

@ -31,10 +31,6 @@ import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
@ -51,6 +47,10 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
/**
* Utility class to adapt {@link org.elasticsearch.action.get.GetResponse},
* {@link org.elasticsearch.index.get.GetResult}, {@link org.elasticsearch.action.get.MultiGetResponse}
@ -131,7 +131,7 @@ public class DocumentAdapters {
/**
* Creates a List of {@link MultiGetItem<Document>}s from {@link MultiGetResponse}.
*
*
* @param source the source {@link MultiGetResponse}, not {@literal null}.
* @return a list of Documents, contains null values for not found Documents.
*/
@ -146,7 +146,7 @@ public class DocumentAdapters {
/**
* Creates a {@link MultiGetItem<Document>} from a {@link MultiGetItemResponse}.
*
*
* @param itemResponse the response, must not be {@literal null}
* @return the MultiGetItem
*/
@ -495,7 +495,8 @@ public class DocumentAdapters {
SearchDocumentAdapter(float score, Object[] sortValues, Map<String, DocumentField> fields,
Map<String, List<String>> highlightFields, Document delegate, Map<String, SearchDocumentResponse> innerHits,
@Nullable NestedMetaData nestedMetaData, @Nullable Explanation explanation, @Nullable List<String> matchedQueries) {
@Nullable NestedMetaData nestedMetaData, @Nullable Explanation explanation,
@Nullable List<String> matchedQueries) {
this.score = score;
this.sortValues = sortValues;

View File

@ -15,9 +15,8 @@
*/
package org.springframework.data.elasticsearch.core;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
@ -138,13 +137,13 @@ public class DocumentAdaptersUnitTests {
}
@Test // DATAES-628, DATAES-848
public void shouldAdaptSearchResponse() throws IOException {
public void shouldAdaptSearchResponse() {
Map<String, DocumentField> fields = Collections.singletonMap("field",
new DocumentField("field", Collections.singletonList("value")));
SearchShardTarget shard = new SearchShardTarget("node", new ShardId("index", "uuid", 42), null, null);
SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), fields, null);
SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), null, fields);
searchHit.shard(shard);
searchHit.setSeqNo(1);
searchHit.setPrimaryTerm(2);
@ -219,7 +218,7 @@ public class DocumentAdaptersUnitTests {
BytesArray source = new BytesArray("{\"field\":\"value\"}");
SearchShardTarget shard = new SearchShardTarget("node", new ShardId("index", "uuid", 42), null, null);
SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), Collections.emptyMap(), null);
SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), null, null);
searchHit.shard(shard);
searchHit.sourceRef(source).score(42);
searchHit.version(22);

View File

@ -59,11 +59,11 @@ class SearchHitSupportTest {
void shouldReturnTheSameListInstanceInSearchHitsAndGetContent() {
List<SearchHit<String>> hits = new ArrayList<>();
hits.add(new SearchHit<>(null, null, null, 0, null, null, "one"));
hits.add(new SearchHit<>(null, null, null, 0, null, null, "two"));
hits.add(new SearchHit<>(null, null, null, 0, null, null, "three"));
hits.add(new SearchHit<>(null, null, null, 0, null, null, "four"));
hits.add(new SearchHit<>(null, null, null, 0, null, null, "five"));
hits.add(new SearchHit<>(null, null, null, 0, null, null, null, null, null, null, "one"));
hits.add(new SearchHit<>(null, null, null, 0, null, null, null, null, null, null, "two"));
hits.add(new SearchHit<>(null, null, null, 0, null, null, null, null, null, null, "three"));
hits.add(new SearchHit<>(null, null, null, 0, null, null, null, null, null, null, "four"));
hits.add(new SearchHit<>(null, null, null, 0, null, null, null, null, null, null, "five"));
SearchHits<String> originalSearchHits = new SearchHitsImpl<>(hits.size(), TotalHitsRelation.EQUAL_TO, 0, "scroll",
hits, null);
@ -112,7 +112,7 @@ class SearchHitSupportTest {
@Override
public SearchHit<String> next() {
String nextString = iterator.next();
return new SearchHit<>("index", "id", null, 1.0f, new Object[0], emptyMap(), nextString);
return new SearchHit<>("index", "id", null, 1.0f, new Object[0], emptyMap(), null, null, null, null, nextString);
}
}

View File

@ -62,7 +62,7 @@ public class StreamQueriesTest {
}
private SearchHit<String> getOneSearchHit() {
return new SearchHit<String>(null, null, null, 0, null, null, "one");
return new SearchHit<String>(null, null, null, 0, null, null, null, null, null, null, "one");
}
@Test // DATAES-766