Skip to content

Commit

Permalink
Expose _id in SearchResult.Hit
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatsb committed Nov 29, 2016
2 parents 59b49c0 + baec742 commit b9da392
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
20 changes: 16 additions & 4 deletions jest-common/src/main/java/io/searchbox/core/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ protected <T, K> Hit<T, K> extractHit(Class<T> sourceType, Class<K> explanationT
String index = hitObject.get("_index").getAsString();
String type = hitObject.get("_type").getAsString();

String id = hitObject.get("_id").getAsString();

Double score = null;
if (hitObject.has("_score") && !hitObject.get("_score").isJsonNull()) {
score = hitObject.get("_score").getAsDouble();
Expand Down Expand Up @@ -136,6 +138,7 @@ protected <T, K> Hit<T, K> extractHit(Class<T> sourceType, Class<K> explanationT
sort,
index,
type,
id,
score
);
}
Expand Down Expand Up @@ -227,6 +230,7 @@ public class Hit<T, K> {
public final List<String> sort;
public final String index;
public final String type;
public final String id;
public final Double score;

public Hit(Class<T> sourceType, JsonElement source) {
Expand All @@ -239,11 +243,11 @@ public Hit(Class<T> sourceType, JsonElement source, Class<K> explanationType, Js

public Hit(Class<T> sourceType, JsonElement source, Class<K> explanationType, JsonElement explanation,
Map<String, List<String>> highlight, List<String> sort) {
this(sourceType, source, explanationType, explanation, highlight, sort, null, null, null);
this(sourceType, source, explanationType, explanation, highlight, sort, null, null, null, null);
}

public Hit(Class<T> sourceType, JsonElement source, Class<K> explanationType, JsonElement explanation,
Map<String, List<String>> highlight, List<String> sort, String index, String type, Double score) {
Map<String, List<String>> highlight, List<String> sort, String index, String type, String id, Double score) {
if (source == null) {
this.source = null;
} else {
Expand All @@ -259,6 +263,7 @@ public Hit(Class<T> sourceType, JsonElement source, Class<K> explanationType, Js

this.index = index;
this.type = type;
this.id = id;
this.score = score;
}

Expand All @@ -271,17 +276,18 @@ public Hit(T source, K explanation) {
}

public Hit(T source, K explanation, Map<String, List<String>> highlight, List<String> sort) {
this(source, explanation, highlight, sort, null, null, null);
this(source, explanation, highlight, sort, null, null, null, null);
}

public Hit(T source, K explanation, Map<String, List<String>> highlight, List<String> sort, String index, String type, Double score) {
public Hit(T source, K explanation, Map<String, List<String>> highlight, List<String> sort, String index, String type, String id, Double score) {
this.source = source;
this.explanation = explanation;
this.highlight = highlight;
this.sort = sort;

this.index = index;
this.type = type;
this.id = id;
this.score = score;
}

Expand All @@ -292,6 +298,9 @@ public int hashCode() {
.append(explanation)
.append(highlight)
.append(sort)
.append(index)
.append(type)
.append(id)
.toHashCode();
}

Expand All @@ -313,6 +322,9 @@ public boolean equals(Object obj) {
.append(explanation, rhs.explanation)
.append(highlight, rhs.highlight)
.append(sort, rhs.sort)
.append(index, rhs.index)
.append(type, rhs.type)
.append(id, rhs.id)
.isEquals();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,15 @@ public void testGetFirstHit() {
assertNotNull(hit.source);
assertNull(hit.explanation);
assertNotNull(hit.sort);
assertNotNull(hit.id);
assertNull(hit.score);

hit = searchResult.getFirstHit(Object.class, Object.class);
assertNotNull(hit);
assertNotNull(hit.source);
assertNull(hit.explanation);
assertNotNull(hit.sort);
assertNotNull(hit.id);
assertNull(hit.score);
}

Expand Down Expand Up @@ -200,7 +202,7 @@ public void testGetFirstHitWhenOperationFails() {
hit = searchResult.getFirstHit(Object.class, Object.class);
assertNull(hit);
}

@Test
public void testGetScore() {
String jsonWithScore = "{\n" +
Expand Down Expand Up @@ -229,7 +231,7 @@ public void testGetScore() {
" ]\n" +
" }\n" +
"}";

SearchResult searchResult = new SearchResult(new Gson());
searchResult.setSucceeded(true);
searchResult.setJsonString(jsonWithScore);
Expand All @@ -241,13 +243,15 @@ public void testGetScore() {
assertNotNull(hit.source);
assertNull(hit.explanation);
assertNotNull(hit.sort);
assertNotNull(hit.id);
assertNotNull(hit.score);

hit = searchResult.getFirstHit(Object.class, Object.class);
assertNotNull(hit);
assertNotNull(hit.source);
assertNull(hit.explanation);
assertNotNull(hit.sort);
assertNotNull(hit.id);
assertNotNull(hit.score);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public void searchWithMultipleHits() throws Exception {
List<SearchResult.Hit<Object, Void>> hits = result.getHits(Object.class);
assertEquals(3, hits.size());

assertEquals(hits.get(0).id, "swmh1");
assertEquals(hits.get(1).id, "swmh2");
assertEquals(hits.get(2).id, "swmh3");

JSONAssert.assertEquals("{\"user\":\"kimchy1\"}," +
"{\"user\":\"kimchy2\"}," +
"{\"user\":\"kimchy3\"}", result.getSourceAsString(), false);
Expand Down

0 comments on commit b9da392

Please sign in to comment.