Skip to content
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 @@ -100,7 +100,7 @@ public void process(HitContext hitContext) throws IOException {
IntStream slots = convertTopDocsToSlots(topDocs, pc.rootDocsBySlot);
// _percolator_document_slot fields are document fields and should be under "fields" section in a hit
List<Object> docSlots = slots.boxed().collect(Collectors.toList());
hitContext.hit().setDocumentField(fieldName, new DocumentField(fieldName, docSlots));
hitContext.hit().setDocumentField(new DocumentField(fieldName, docSlots));

// Add info what sub-queries of percolator query matched this each percolated document
if (fetchContext.getSearchExecutionContext().hasNamedQueries()) {
Expand All @@ -120,7 +120,7 @@ public void process(HitContext hitContext) throws IOException {
matchedQueries.add(match.getName());
}
String matchedFieldName = fieldName + "_" + docSlots.get(i) + "_matched_queries";
hitContext.hit().setDocumentField(matchedFieldName, new DocumentField(matchedFieldName, matchedQueries));
hitContext.hit().setDocumentField(new DocumentField(matchedFieldName, matchedQueries));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1327,8 +1327,7 @@ public void setNextReader(LeafReaderContext ctx) {
public void process(FetchSubPhase.HitContext hitContext) {
leafSearchLookup.setDocument(hitContext.docId());
FieldLookup fieldLookup = leafSearchLookup.fields().get("text");
hitContext.hit()
.setDocumentField("text_stored_lookup", new DocumentField("text_stored_lookup", fieldLookup.getValues()));
hitContext.hit().setDocumentField(new DocumentField("text_stored_lookup", fieldLookup.getValues()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void hitExecute(FetchContext context, HitContext hitContext) throws IOEx
DocumentField hitField = hitContext.hit().getFields().get(NAME);
if (hitField == null) {
hitField = new DocumentField(NAME, new ArrayList<>(1));
hitContext.hit().setDocumentField(NAME, hitField);
hitContext.hit().setDocumentField(hitField);
}
Terms terms = hitContext.reader().termVectors().get(hitContext.docId(), field);
if (terms != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ static TransportVersion def(int id) {
public static final TransportVersion SEARCH_INCREMENTAL_TOP_DOCS_NULL = def(9_058_0_00);
public static final TransportVersion COMPRESS_DELAYABLE_WRITEABLE = def(9_059_0_00);
public static final TransportVersion SYNONYMS_REFRESH_PARAM = def(9_060_0_00);
public static final TransportVersion DOC_FIELDS_AS_LIST = def(9_061_0_00);

/*
* STOP! READ THIS FIRST! No, really,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
Expand Down Expand Up @@ -70,6 +71,15 @@ public DocumentField(String name, List<Object> values, List<Object> ignoredValue
: "DocumentField can't have both lookup fields and values";
}

/**
* Read map of document fields written via {@link StreamOutput#writeMapValues(Map)}.
* @param in stream input
* @return map of {@link DocumentField} keyed by {@link DocumentField#getName()}
*/
public static Map<String, DocumentField> readFieldsFromMapValues(StreamInput in) throws IOException {
return in.readMapValues(DocumentField::new, DocumentField::getName);
}

/**
* The name of the field.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public GetResult(StreamInput in) throws IOException {
if (source.length() == 0) {
source = null;
}
documentFields = in.readMapValues(DocumentField::new, DocumentField::getName);
metaFields = in.readMapValues(DocumentField::new, DocumentField::getName);
documentFields = DocumentField.readFieldsFromMapValues(in);
metaFields = DocumentField.readFieldsFromMapValues(in);
} else {
metaFields = Collections.emptyMap();
documentFields = Collections.emptyMap();
Expand Down
26 changes: 19 additions & 7 deletions server/src/main/java/org/elasticsearch/search/SearchHit.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,15 @@ public static SearchHit readFrom(StreamInput in, boolean pooled) throws IOExcept
if (in.readBoolean()) {
explanation = readExplanation(in);
}
final Map<String, DocumentField> documentFields = in.readMap(DocumentField::new);
final Map<String, DocumentField> metaFields = in.readMap(DocumentField::new);
final Map<String, DocumentField> documentFields;
final Map<String, DocumentField> metaFields;
if (in.getTransportVersion().onOrAfter(TransportVersions.DOC_FIELDS_AS_LIST)) {
documentFields = DocumentField.readFieldsFromMapValues(in);
metaFields = DocumentField.readFieldsFromMapValues(in);
} else {
documentFields = in.readMap(DocumentField::new);
metaFields = in.readMap(DocumentField::new);
}
Map<String, HighlightField> highlightFields = in.readMapValues(HighlightField::new, HighlightField::name);
highlightFields = highlightFields.isEmpty() ? null : unmodifiableMap(highlightFields);

Expand Down Expand Up @@ -322,8 +329,13 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(true);
writeExplanation(out, explanation);
}
out.writeMap(documentFields, StreamOutput::writeWriteable);
out.writeMap(metaFields, StreamOutput::writeWriteable);
if (out.getTransportVersion().onOrAfter(TransportVersions.DOC_FIELDS_AS_LIST)) {
out.writeMapValues(documentFields);
out.writeMapValues(metaFields);
} else {
out.writeMap(documentFields, StreamOutput::writeWriteable);
out.writeMap(metaFields, StreamOutput::writeWriteable);
}
if (highlightFields == null) {
out.writeVInt(0);
} else {
Expand Down Expand Up @@ -502,9 +514,9 @@ public DocumentField field(String fieldName) {
/*
* Adds a new DocumentField to the map in case both parameters are not null.
* */
public void setDocumentField(String fieldName, DocumentField field) {
if (fieldName == null || field == null) return;
this.documentFields.put(fieldName, field);
public void setDocumentField(DocumentField field) {
if (field == null) return;
this.documentFields.put(field.getName(), field);
}

public void addDocumentFields(Map<String, DocumentField> docFields, Map<String, DocumentField> metaFields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void process(HitContext hit) throws IOException {
hitField = new DocumentField(f.field, new ArrayList<>(2));
// even if we request a doc values of a meta-field (e.g. _routing),
// docValues fields will still be document fields, and put under "fields" section of a hit.
hit.hit().setDocumentField(f.field, hitField);
hit.hit().setDocumentField(hitField);
}
List<Object> ignoredValues = new ArrayList<>();
hitField.getValues().addAll(f.fetcher.fetchValues(hit.source(), hit.docId(), ignoredValues));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void process(HitContext hitContext) {
}
hitField = new DocumentField(scriptFieldName, values);
// script fields are never meta-fields
hitContext.hit().setDocumentField(scriptFieldName, hitField);
hitContext.hit().setDocumentField(hitField);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public void process(HitContext hitContext) {
Map<String, List<Object>> loadedFields = hitContext.loadedFields();
for (StoredField storedField : storedFields) {
if (storedField.hasValue(loadedFields)) {
hitContext.hit()
.setDocumentField(storedField.name, new DocumentField(storedField.name, storedField.process(loadedFields)));
hitContext.hit().setDocumentField(new DocumentField(storedField.name, storedField.process(loadedFields)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
};

SearchHit hit = new SearchHit(1, "ID");
hit.setDocumentField("someField", new DocumentField("someField", Collections.singletonList(collapseValue)));
hit.setDocumentField(new DocumentField("someField", Collections.singletonList(collapseValue)));
ExpandSearchPhase phase = newExpandSearchPhase(
mockSearchPhaseContext,
new SearchResponseSections(
Expand Down Expand Up @@ -188,9 +188,9 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
};

SearchHit hit1 = new SearchHit(1, "ID");
hit1.setDocumentField("someField", new DocumentField("someField", Collections.singletonList(collapseValue)));
hit1.setDocumentField(new DocumentField("someField", Collections.singletonList(collapseValue)));
SearchHit hit2 = new SearchHit(2, "ID2");
hit2.setDocumentField("someField", new DocumentField("someField", Collections.singletonList(collapseValue)));
hit2.setDocumentField(new DocumentField("someField", Collections.singletonList(collapseValue)));
try (
SearchResponseSections searchResponseSections = new SearchResponseSections(
new SearchHits(new SearchHit[] { hit1, hit2 }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0F),
Expand Down Expand Up @@ -225,9 +225,9 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
};

SearchHit hit1 = new SearchHit(1, "ID");
hit1.setDocumentField("someField", new DocumentField("someField", Collections.singletonList(null)));
hit1.setDocumentField(new DocumentField("someField", Collections.singletonList(null)));
SearchHit hit2 = new SearchHit(2, "ID2");
hit2.setDocumentField("someField", new DocumentField("someField", Collections.singletonList(null)));
hit2.setDocumentField(new DocumentField("someField", Collections.singletonList(null)));
ExpandSearchPhase phase = newExpandSearchPhase(
mockSearchPhaseContext,
new SearchResponseSections(
Expand Down Expand Up @@ -313,7 +313,7 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
.routing("baz");

SearchHit hit = new SearchHit(1, "ID");
hit.setDocumentField("someField", new DocumentField("someField", Collections.singletonList("foo")));
hit.setDocumentField(new DocumentField("someField", Collections.singletonList("foo")));
try (
SearchResponseSections searchResponseSections = new SearchResponseSections(
new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0F),
Expand Down Expand Up @@ -384,7 +384,7 @@ void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionL
.routing("baz");

SearchHit hit = new SearchHit(1, "ID");
hit.setDocumentField("someField", new DocumentField("someField", Collections.singletonList("foo")));
hit.setDocumentField(new DocumentField("someField", Collections.singletonList("foo")));
try (
SearchResponseSections searchResponseSections = new SearchResponseSections(
new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0F),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void sendExecuteMultiSearch(
final SearchHits searchHits;
if (fields != null) {
final SearchHit hit = new SearchHit(randomInt(1000));
fields.forEach((f, values) -> hit.setDocumentField(f, new DocumentField(f, values, List.of())));
fields.forEach((f, values) -> hit.setDocumentField(new DocumentField(f, values, List.of())));
searchHits = new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f);
} else {
searchHits = SearchHits.empty(new TotalHits(0, TotalHits.Relation.EQUAL_TO), 1.0f);
Expand Down Expand Up @@ -143,7 +143,6 @@ void sendExecuteMultiSearch(
final List<FieldAndFormat> fetchFields = List.of(new FieldAndFormat(randomAlphaOfLength(10), null));
{
leftHit0.setDocumentField(
"lookup_field_1",
new DocumentField(
"lookup_field_1",
List.of(),
Expand All @@ -155,7 +154,6 @@ void sendExecuteMultiSearch(
)
);
leftHit0.setDocumentField(
"lookup_field_2",
new DocumentField(
"lookup_field_2",
List.of(),
Expand All @@ -168,7 +166,6 @@ void sendExecuteMultiSearch(
SearchHit leftHit1 = new SearchHit(randomInt(100));
{
leftHit1.setDocumentField(
"lookup_field_2",
new DocumentField(
"lookup_field_2",
List.of(),
Expand All @@ -180,7 +177,6 @@ void sendExecuteMultiSearch(
)
);
leftHit1.setDocumentField(
"lookup_field_3",
new DocumentField(
"lookup_field_3",
List.of(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ private void buildRankFeatureResult(
searchHits[i] = SearchHit.unpooled(scoreDocs[i].doc);
searchHits[i].shard(shardTarget);
searchHits[i].score(scoreDocs[i].score);
searchHits[i].setDocumentField(DEFAULT_FIELD, new DocumentField(DEFAULT_FIELD, Collections.singletonList(scoreDocs[i].doc)));
searchHits[i].setDocumentField(new DocumentField(DEFAULT_FIELD, Collections.singletonList(scoreDocs[i].doc)));
if (scoreDocs[i].score > maxScore) {
maxScore = scoreDocs[i].score;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,13 @@ public void testProcessFetch() {
searchContext.addFetchResult();
SearchHit[] hits = new SearchHit[3];
hits[0] = SearchHit.unpooled(4);
hits[0].setDocumentField(fieldName, new DocumentField(fieldName, Collections.singletonList(expectedFieldData.get(4))));
hits[0].setDocumentField(new DocumentField(fieldName, Collections.singletonList(expectedFieldData.get(4))));

hits[1] = SearchHit.unpooled(9);
hits[1].setDocumentField(fieldName, new DocumentField(fieldName, Collections.singletonList(expectedFieldData.get(9))));
hits[1].setDocumentField(new DocumentField(fieldName, Collections.singletonList(expectedFieldData.get(9))));

hits[2] = SearchHit.unpooled(numDocs - 1);
hits[2].setDocumentField(
fieldName,
new DocumentField(fieldName, Collections.singletonList(expectedFieldData.get(numDocs - 1)))
);
hits[2].setDocumentField(new DocumentField(fieldName, Collections.singletonList(expectedFieldData.get(numDocs - 1))));
searchHits = SearchHits.unpooled(hits, new TotalHits(3, TotalHits.Relation.EQUAL_TO), 1.0f);
searchContext.fetchResult().shardResult(searchHits, null);
when(searchContext.isCancelled()).thenReturn(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SearchHitBuilder addField(String name, Object value) {
}

public SearchHitBuilder addField(String name, List<Object> values) {
hit.setDocumentField(name, new DocumentField(name, values));
hit.setDocumentField(new DocumentField(name, values));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testGet() {
double expected = Math.log(value);
DocumentField field = new DocumentField(fieldName, singletonList(value));
SearchHit hit = SearchHit.unpooled(1, null);
hit.setDocumentField(fieldName, field);
hit.setDocumentField(field);
assertEquals(expected, extractor.process(hit));
}
}
Expand Down
Loading