Skip to content

Commit 686d507

Browse files
authored
Add methods to retrieve doc and/or metadata fields only from SearchHit (#77192) (#77197)
The SearchHit.getFields() methods returns both document and metadata fields commingled. This commit adds new methods to retrieve them separately. Fixes #77171
1 parent 791a3e8 commit 686d507

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

server/src/internalClusterTest/java/org/elasticsearch/search/fields/SearchFieldsIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,10 @@ public void testLoadMetadata() throws Exception {
11941194
assertSearchResponse(response);
11951195
assertHitCount(response, 1);
11961196

1197-
Map<String, DocumentField> fields = response.getHits().getAt(0).getFields();
1197+
Map<String, DocumentField> fields = response.getHits().getAt(0).getMetadataFields();
11981198

11991199
assertThat(fields.get("field1"), nullValue());
12001200
assertThat(fields.get("_routing").getValue().toString(), equalTo("1"));
1201+
assertThat(response.getHits().getAt(0).getDocumentFields().size(), equalTo(0));
12011202
}
12021203
}

server/src/main/java/org/elasticsearch/search/SearchHit.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,23 @@ public void setDocumentField(String fieldName, DocumentField field) {
456456
this.documentFields.put(fieldName, field);
457457
}
458458

459+
/**
460+
* @return a map of metadata fields for this hit
461+
*/
462+
public Map<String, DocumentField> getMetadataFields() {
463+
return Collections.unmodifiableMap(metaFields);
464+
}
465+
466+
/**
467+
* @return a map of non-metadata fields requested for this hit
468+
*/
469+
public Map<String, DocumentField> getDocumentFields() {
470+
return Collections.unmodifiableMap(documentFields);
471+
}
472+
459473
/**
460474
* A map of hit fields (from field name to hit fields) if additional fields
461-
* were required to be loaded.
475+
* were required to be loaded. Includes both document and metadata fields.
462476
*/
463477
public Map<String, DocumentField> getFields() {
464478
if (metaFields.size() > 0 || documentFields.size() > 0) {

0 commit comments

Comments
 (0)