Skip to content

Add matched_queries field to SearchHit. #1722

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -33,6 +33,7 @@
*
* @param <T> the result data class.
* @author Peter-Josef Meisch
* @author Matt Gilene
* @since 4.0
*/
public class SearchHit<T> {
Expand All @@ -47,16 +48,18 @@ 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;

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, 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,
@Nullable Object[] sortValues, @Nullable Map<String, List<String>> highlightFields,
@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.id = id;
this.routing = routing;
Expand All @@ -74,6 +77,7 @@ public SearchHit(@Nullable String index, @Nullable String id, @Nullable String r
this.nestedMetaData = nestedMetaData;
this.explanation = explanation;
this.content = content;
this.matchedQueries = matchedQueries;
}

/**
Expand Down Expand Up @@ -188,4 +192,12 @@ public String getRouting() {
public Explanation getExplanation() {
return explanation;
}

/**
* @return the matched queries for this SearchHit.
*/
@Nullable
public List<String> getMatchedQueries() {
return matchedQueries;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @author Peter-Josef Meisch
* @author Mark Paluch
* @author Roman Puchkovskiy
* @author Matt Gilene
* @since 4.0
*/
class SearchHitMapping<T> {
Expand Down Expand Up @@ -114,6 +115,7 @@ SearchHit<T> mapHit(SearchDocument searchDocument, T content) {
mapInnerHits(searchDocument), //
searchDocument.getNestedMetaData(), //
searchDocument.getExplanation(), //
searchDocument.getMatchedQueries(), //
content); //
}

Expand Down Expand Up @@ -198,6 +200,7 @@ private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, C
searchHit.getInnerHits(), //
persistentEntityWithNestedMetaData.nestedMetaData, //
searchHit.getExplanation(), //
searchHit.getMatchedQueries(), //
targetObject));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
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;
Expand All @@ -47,10 +51,6 @@
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}
Expand All @@ -60,6 +60,7 @@
* @author Mark Paluch
* @author Peter-Josef Meisch
* @author Roman Puchkovskiy
* @author Matt Gilene
* @since 4.0
*/
public class DocumentAdapters {
Expand Down Expand Up @@ -181,14 +182,15 @@ public static SearchDocument from(SearchHit source) {

NestedMetaData nestedMetaData = from(source.getNestedIdentity());
Explanation explanation = from(source.getExplanation());
List<String> matchedQueries = from(source.getMatchedQueries());

BytesReference sourceRef = source.getSourceRef();

if (sourceRef == null || sourceRef.length() == 0) {
return new SearchDocumentAdapter(
source.getScore(), source.getSortValues(), source.getFields(), highlightFields, fromDocumentFields(source,
source.getIndex(), source.getId(), source.getVersion(), source.getSeqNo(), source.getPrimaryTerm()),
innerHits, nestedMetaData, explanation);
innerHits, nestedMetaData, explanation, matchedQueries);
}

Document document = Document.from(source.getSourceAsMap());
Expand All @@ -202,7 +204,7 @@ public static SearchDocument from(SearchHit source) {
document.setPrimaryTerm(source.getPrimaryTerm());

return new SearchDocumentAdapter(source.getScore(), source.getSortValues(), source.getFields(), highlightFields,
document, innerHits, nestedMetaData, explanation);
document, innerHits, nestedMetaData, explanation, matchedQueries);
}

@Nullable
Expand Down Expand Up @@ -231,6 +233,15 @@ private static NestedMetaData from(@Nullable SearchHit.NestedIdentity nestedIden
return NestedMetaData.of(nestedIdentity.getField().string(), nestedIdentity.getOffset(), child);
}

@Nullable
private static List<String> from(@Nullable String[] matchedQueries) {
if (matchedQueries == null) {
return null;
}

return List.of(matchedQueries);
}

/**
* Create an unmodifiable {@link Document} from {@link Iterable} of {@link DocumentField}s.
*
Expand Down Expand Up @@ -484,10 +495,11 @@ static class SearchDocumentAdapter implements SearchDocument {
private final Map<String, SearchDocumentResponse> innerHits = new HashMap<>();
@Nullable private final NestedMetaData nestedMetaData;
@Nullable private final Explanation explanation;
@Nullable private final List<String> matchedQueries;

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 NestedMetaData nestedMetaData, @Nullable Explanation explanation, @Nullable List<String> matchedQueries) {

this.score = score;
this.sortValues = sortValues;
Expand All @@ -497,6 +509,7 @@ static class SearchDocumentAdapter implements SearchDocument {
this.innerHits.putAll(innerHits);
this.nestedMetaData = nestedMetaData;
this.explanation = explanation;
this.matchedQueries = matchedQueries;
}

@Override
Expand Down Expand Up @@ -679,6 +692,12 @@ public Explanation getExplanation() {
return explanation;
}

@Override
@Nullable
public List<String> getMatchedQueries() {
return matchedQueries;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*
* @author Mark Paluch
* @author Peter-Josef Meisch
* @author Matt Gilene
* @since 4.0
* @see Document
*/
Expand Down Expand Up @@ -105,4 +106,10 @@ default String getRouting() {
*/
@Nullable
Explanation getExplanation();

/**
* @return the matched queries for the SearchHit.
*/
@Nullable
List<String> getMatchedQueries();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package org.springframework.data.elasticsearch.core;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -45,6 +46,7 @@
* @author Mark Paluch
* @author Peter-Josef Meisch
* @author Roman Puchkovskiy
* @author Matt Gilene
*/
public class DocumentAdaptersUnitTests {

Expand Down Expand Up @@ -262,4 +264,18 @@ void shouldAdaptReturnedExplanations() {
List<Explanation> details = explanation.getDetails();
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(List.of("query1", "query2"));
}
}