Skip to content

Commit

Permalink
Merge pull request searchbox-io#510 from graylog-labs/optional-metadata
Browse files Browse the repository at this point in the history
Make adding metadata fields in SearchResult optional
  • Loading branch information
ferhatsb authored May 25, 2017
2 parents fb95ef0 + 0d3bfd9 commit 923bb62
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
42 changes: 26 additions & 16 deletions jest-common/src/main/java/io/searchbox/core/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,22 @@ public <T, K> Hit<T, K> getFirstHit(Class<T> sourceType, Class<K> explanationTyp
}

public <T> List<Hit<T, Void>> getHits(Class<T> sourceType) {
return getHits(sourceType, Void.class);
return getHits(sourceType, true);
}

public <T> List<Hit<T, Void>> getHits(Class<T> sourceType, boolean addEsMetadataFields) {
return getHits(sourceType, Void.class, addEsMetadataFields);
}

public <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType) {
return getHits(sourceType, explanationType, false);
return getHits(sourceType, explanationType, false, true);
}

public <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean addEsMetadataFields) {
return getHits(sourceType, explanationType, false, addEsMetadataFields);
}

protected <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean returnSingle) {
protected <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean returnSingle, boolean addEsMetadataFields) {
List<Hit<T, K>> sourceList = new ArrayList<Hit<T, K>>();

if (jsonObject != null) {
Expand All @@ -78,10 +86,10 @@ protected <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanati
}

if (obj.isJsonObject()) {
sourceList.add(extractHit(sourceType, explanationType, obj, sourceKey));
sourceList.add(extractHit(sourceType, explanationType, obj, sourceKey, addEsMetadataFields));
} else if (obj.isJsonArray()) {
for (JsonElement hitElement : obj.getAsJsonArray()) {
sourceList.add(extractHit(sourceType, explanationType, hitElement, sourceKey));
sourceList.add(extractHit(sourceType, explanationType, hitElement, sourceKey, addEsMetadataFields));
if (returnSingle) break;
}
}
Expand All @@ -91,7 +99,7 @@ protected <T, K> List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanati
return sourceList;
}

protected <T, K> Hit<T, K> extractHit(Class<T> sourceType, Class<K> explanationType, JsonElement hitElement, String sourceKey) {
protected <T, K> Hit<T, K> extractHit(Class<T> sourceType, Class<K> explanationType, JsonElement hitElement, String sourceKey, boolean addEsMetadataFields) {
Hit<T, K> hit = null;

if (hitElement.isJsonObject()) {
Expand All @@ -113,18 +121,20 @@ protected <T, K> Hit<T, K> extractHit(Class<T> sourceType, Class<K> explanationT
Map<String, List<String>> highlight = extractHighlight(hitObject.getAsJsonObject(HIGHLIGHT_KEY));
List<String> sort = extractSort(hitObject.getAsJsonArray(SORT_KEY));

JsonObject clonedSource = null;
for (MetaField metaField : META_FIELDS) {
JsonElement metaElement = hitObject.get(metaField.esFieldName);
if (metaElement != null) {
if (clonedSource == null) {
clonedSource = (JsonObject) CloneUtils.deepClone(source);
if (addEsMetadataFields) {
JsonObject clonedSource = null;
for (MetaField metaField : META_FIELDS) {
JsonElement metaElement = hitObject.get(metaField.esFieldName);
if (metaElement != null) {
if (clonedSource == null) {
clonedSource = (JsonObject) CloneUtils.deepClone(source);
}
clonedSource.add(metaField.internalFieldName, metaElement);
}
clonedSource.add(metaField.internalFieldName, metaElement);
}
}
if (clonedSource != null) {
source = clonedSource;
if (clonedSource != null) {
source = clonedSource;
}
}

hit = new Hit<T, K>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
import org.junit.Test;

import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
* @author cihat keser
Expand Down Expand Up @@ -152,6 +157,28 @@ public void testGetHits() {
assertFalse("should have 1 hit", hits.isEmpty());
}

@Test
public void testGetHitsWithoutMetadata() {
final SearchResult searchResult = new SearchResult(new Gson());
searchResult.setSucceeded(true);
searchResult.setJsonString(json);
searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());
searchResult.setPathToResult("hits/hits/_source");

assertTrue(getFirstHitSource(searchResult.getHits(Object.class)).containsKey(SearchResult.ES_METADATA_ID));
assertFalse(getFirstHitSource(searchResult.getHits(Object.class, false)).containsKey(SearchResult.ES_METADATA_ID));
assertTrue(getFirstHitSource(searchResult.getHits(Object.class, Object.class)).containsKey(SearchResult.ES_METADATA_ID));
assertFalse(getFirstHitSource(searchResult.getHits(Object.class, Object.class, false)).containsKey(SearchResult.ES_METADATA_ID));
}

private Map getFirstHitSource(List hits) {
assertNotNull(hits);
assertTrue("should have 1 hit", hits.size() == 1);
SearchResult.Hit hit = (SearchResult.Hit) hits.get(0);
assertNotNull(hit.source);
return (Map) hit.source;
}

@Test
public void testGetFirstHit() {
SearchResult searchResult = new SearchResult(new Gson());
Expand Down

0 comments on commit 923bb62

Please sign in to comment.