mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-01 09:42:11 +00:00
Add matched_queries field to SearchHit.
Original Pull Request #1722 Closes #1514
This commit is contained in:
parent
3f39f5d5b7
commit
4dc8b2589a
@ -33,6 +33,7 @@ import org.springframework.util.Assert;
|
|||||||
*
|
*
|
||||||
* @param <T> the result data class.
|
* @param <T> the result data class.
|
||||||
* @author Peter-Josef Meisch
|
* @author Peter-Josef Meisch
|
||||||
|
* @author Matt Gilene
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
public class SearchHit<T> {
|
public class SearchHit<T> {
|
||||||
@ -47,16 +48,18 @@ public class SearchHit<T> {
|
|||||||
@Nullable private final NestedMetaData nestedMetaData;
|
@Nullable private final NestedMetaData nestedMetaData;
|
||||||
@Nullable private final String routing;
|
@Nullable private final String routing;
|
||||||
@Nullable private final Explanation explanation;
|
@Nullable private final Explanation explanation;
|
||||||
|
@Nullable private final List<String> matchedQueries;
|
||||||
|
|
||||||
public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score,
|
public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score,
|
||||||
@Nullable Object[] sortValues, @Nullable Map<String, List<String>> highlightFields, T content) {
|
@Nullable Object[] sortValues, @Nullable Map<String, List<String>> highlightFields, T content) {
|
||||||
this(index, id, routing, score, sortValues, highlightFields, null, null, null, content);
|
this(index, id, routing, score, sortValues, highlightFields, null, null, null, null, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score,
|
public SearchHit(@Nullable String index, @Nullable String id, @Nullable String routing, float score,
|
||||||
@Nullable Object[] sortValues, @Nullable Map<String, List<String>> highlightFields,
|
@Nullable Object[] sortValues, @Nullable Map<String, List<String>> highlightFields,
|
||||||
@Nullable Map<String, SearchHits<?>> innerHits, @Nullable NestedMetaData nestedMetaData,
|
@Nullable Map<String, SearchHits<?>> innerHits, @Nullable NestedMetaData nestedMetaData,
|
||||||
@Nullable Explanation explanation, T content) {
|
@Nullable Explanation explanation,
|
||||||
|
@Nullable List<String> matchedQueries, T content) {
|
||||||
this.index = index;
|
this.index = index;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.routing = routing;
|
this.routing = routing;
|
||||||
@ -74,6 +77,7 @@ public class SearchHit<T> {
|
|||||||
this.nestedMetaData = nestedMetaData;
|
this.nestedMetaData = nestedMetaData;
|
||||||
this.explanation = explanation;
|
this.explanation = explanation;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
|
this.matchedQueries = matchedQueries;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -188,4 +192,12 @@ public class SearchHit<T> {
|
|||||||
public Explanation getExplanation() {
|
public Explanation getExplanation() {
|
||||||
return explanation;
|
return explanation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the matched queries for this SearchHit.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public List<String> getMatchedQueries() {
|
||||||
|
return matchedQueries;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,7 @@ import org.springframework.util.Assert;
|
|||||||
* @author Peter-Josef Meisch
|
* @author Peter-Josef Meisch
|
||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
* @author Roman Puchkovskiy
|
* @author Roman Puchkovskiy
|
||||||
|
* @author Matt Gilene
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
class SearchHitMapping<T> {
|
class SearchHitMapping<T> {
|
||||||
@ -114,6 +115,7 @@ class SearchHitMapping<T> {
|
|||||||
mapInnerHits(searchDocument), //
|
mapInnerHits(searchDocument), //
|
||||||
searchDocument.getNestedMetaData(), //
|
searchDocument.getNestedMetaData(), //
|
||||||
searchDocument.getExplanation(), //
|
searchDocument.getExplanation(), //
|
||||||
|
searchDocument.getMatchedQueries(), //
|
||||||
content); //
|
content); //
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,6 +200,7 @@ class SearchHitMapping<T> {
|
|||||||
searchHit.getInnerHits(), //
|
searchHit.getInnerHits(), //
|
||||||
persistentEntityWithNestedMetaData.nestedMetaData, //
|
persistentEntityWithNestedMetaData.nestedMetaData, //
|
||||||
searchHit.getExplanation(), //
|
searchHit.getExplanation(), //
|
||||||
|
searchHit.getMatchedQueries(), //
|
||||||
targetObject));
|
targetObject));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -31,6 +31,10 @@ import java.util.Set;
|
|||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.stream.Collectors;
|
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.GetResponse;
|
||||||
import org.elasticsearch.action.get.MultiGetItemResponse;
|
import org.elasticsearch.action.get.MultiGetItemResponse;
|
||||||
import org.elasticsearch.action.get.MultiGetResponse;
|
import org.elasticsearch.action.get.MultiGetResponse;
|
||||||
@ -47,10 +51,6 @@ import org.springframework.lang.Nullable;
|
|||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
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},
|
* Utility class to adapt {@link org.elasticsearch.action.get.GetResponse},
|
||||||
* {@link org.elasticsearch.index.get.GetResult}, {@link org.elasticsearch.action.get.MultiGetResponse}
|
* {@link org.elasticsearch.index.get.GetResult}, {@link org.elasticsearch.action.get.MultiGetResponse}
|
||||||
@ -60,6 +60,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
|
|||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
* @author Peter-Josef Meisch
|
* @author Peter-Josef Meisch
|
||||||
* @author Roman Puchkovskiy
|
* @author Roman Puchkovskiy
|
||||||
|
* @author Matt Gilene
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
public class DocumentAdapters {
|
public class DocumentAdapters {
|
||||||
@ -181,6 +182,7 @@ public class DocumentAdapters {
|
|||||||
|
|
||||||
NestedMetaData nestedMetaData = from(source.getNestedIdentity());
|
NestedMetaData nestedMetaData = from(source.getNestedIdentity());
|
||||||
Explanation explanation = from(source.getExplanation());
|
Explanation explanation = from(source.getExplanation());
|
||||||
|
List<String> matchedQueries = from(source.getMatchedQueries());
|
||||||
|
|
||||||
BytesReference sourceRef = source.getSourceRef();
|
BytesReference sourceRef = source.getSourceRef();
|
||||||
|
|
||||||
@ -188,7 +190,7 @@ public class DocumentAdapters {
|
|||||||
return new SearchDocumentAdapter(
|
return new SearchDocumentAdapter(
|
||||||
source.getScore(), source.getSortValues(), source.getFields(), highlightFields, fromDocumentFields(source,
|
source.getScore(), source.getSortValues(), source.getFields(), highlightFields, fromDocumentFields(source,
|
||||||
source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(), source.getPrimaryTerm()),
|
source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(), source.getPrimaryTerm()),
|
||||||
innerHits, nestedMetaData, explanation);
|
innerHits, nestedMetaData, explanation, matchedQueries);
|
||||||
}
|
}
|
||||||
|
|
||||||
Document document = Document.from(source.getSourceAsMap());
|
Document document = Document.from(source.getSourceAsMap());
|
||||||
@ -202,7 +204,7 @@ public class DocumentAdapters {
|
|||||||
document.setPrimaryTerm(source.getPrimaryTerm());
|
document.setPrimaryTerm(source.getPrimaryTerm());
|
||||||
|
|
||||||
return new SearchDocumentAdapter(source.getScore(), source.getSortValues(), source.getFields(), highlightFields,
|
return new SearchDocumentAdapter(source.getScore(), source.getSortValues(), source.getFields(), highlightFields,
|
||||||
document, innerHits, nestedMetaData, explanation);
|
document, innerHits, nestedMetaData, explanation, matchedQueries);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -231,6 +233,11 @@ public class DocumentAdapters {
|
|||||||
return NestedMetaData.of(nestedIdentity.getField().string(), nestedIdentity.getOffset(), child);
|
return NestedMetaData.of(nestedIdentity.getField().string(), nestedIdentity.getOffset(), child);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static List<String> from(@Nullable String[] matchedQueries) {
|
||||||
|
return matchedQueries == null ? null : Arrays.asList(matchedQueries);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s.
|
* Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s.
|
||||||
*
|
*
|
||||||
@ -484,10 +491,11 @@ public class DocumentAdapters {
|
|||||||
private final Map<String, SearchDocumentResponse> innerHits = new HashMap<>();
|
private final Map<String, SearchDocumentResponse> innerHits = new HashMap<>();
|
||||||
@Nullable private final NestedMetaData nestedMetaData;
|
@Nullable private final NestedMetaData nestedMetaData;
|
||||||
@Nullable private final Explanation explanation;
|
@Nullable private final Explanation explanation;
|
||||||
|
@Nullable private final List<String> matchedQueries;
|
||||||
|
|
||||||
SearchDocumentAdapter(float score, Object[] sortValues, Map<String, DocumentField> fields,
|
SearchDocumentAdapter(float score, Object[] sortValues, Map<String, DocumentField> fields,
|
||||||
Map<String, List<String>> highlightFields, Document delegate, Map<String, SearchDocumentResponse> innerHits,
|
Map<String, List<String>> highlightFields, Document delegate, Map<String, SearchDocumentResponse> innerHits,
|
||||||
@Nullable NestedMetaData nestedMetaData, @Nullable Explanation explanation) {
|
@Nullable NestedMetaData nestedMetaData, @Nullable Explanation explanation, @Nullable List<String> matchedQueries) {
|
||||||
|
|
||||||
this.score = score;
|
this.score = score;
|
||||||
this.sortValues = sortValues;
|
this.sortValues = sortValues;
|
||||||
@ -497,6 +505,7 @@ public class DocumentAdapters {
|
|||||||
this.innerHits.putAll(innerHits);
|
this.innerHits.putAll(innerHits);
|
||||||
this.nestedMetaData = nestedMetaData;
|
this.nestedMetaData = nestedMetaData;
|
||||||
this.explanation = explanation;
|
this.explanation = explanation;
|
||||||
|
this.matchedQueries = matchedQueries;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -679,6 +688,12 @@ public class DocumentAdapters {
|
|||||||
return explanation;
|
return explanation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public List<String> getMatchedQueries() {
|
||||||
|
return matchedQueries;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
|
@ -25,6 +25,7 @@ import org.springframework.lang.Nullable;
|
|||||||
*
|
*
|
||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
* @author Peter-Josef Meisch
|
* @author Peter-Josef Meisch
|
||||||
|
* @author Matt Gilene
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
* @see Document
|
* @see Document
|
||||||
*/
|
*/
|
||||||
@ -105,4 +106,10 @@ public interface SearchDocument extends Document {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
Explanation getExplanation();
|
Explanation getExplanation();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the matched queries for the SearchHit.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
List<String> getMatchedQueries();
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -45,6 +45,7 @@ import org.springframework.data.elasticsearch.core.document.SearchDocument;
|
|||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
* @author Peter-Josef Meisch
|
* @author Peter-Josef Meisch
|
||||||
* @author Roman Puchkovskiy
|
* @author Roman Puchkovskiy
|
||||||
|
* @author Matt Gilene
|
||||||
*/
|
*/
|
||||||
public class DocumentAdaptersUnitTests {
|
public class DocumentAdaptersUnitTests {
|
||||||
|
|
||||||
@ -262,4 +263,18 @@ public class DocumentAdaptersUnitTests {
|
|||||||
List<Explanation> details = explanation.getDetails();
|
List<Explanation> details = explanation.getDetails();
|
||||||
assertThat(details).containsExactly(new Explanation(false, 0.0, "explanation noMatch", Collections.emptyList()));
|
assertThat(details).containsExactly(new Explanation(false, 0.0, "explanation noMatch", Collections.emptyList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // DATAES-979
|
||||||
|
@DisplayName("should adapt returned matched queries")
|
||||||
|
void shouldAdaptReturnedMatchedQueries() {
|
||||||
|
SearchHit searchHit = new SearchHit(42);
|
||||||
|
searchHit.matchedQueries(new String[] { "query1", "query2" });
|
||||||
|
|
||||||
|
SearchDocument searchDocument = DocumentAdapters.from(searchHit);
|
||||||
|
|
||||||
|
List<String> matchedQueries = searchDocument.getMatchedQueries();
|
||||||
|
assertThat(matchedQueries).isNotNull();
|
||||||
|
assertThat(matchedQueries).hasSize(2);
|
||||||
|
assertThat(matchedQueries).isEqualTo(Arrays.asList("query1", "query2"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user