SearchResult extends JestResult { public <T> Hit<T, Void> getFirstHit(Class<T> sourceType) { return getFirstHit(sourceType, Void.class); } SearchResult(SearchResult searchResult);  SearchResult(Gson gson); @Override @Deprecated T getSourceAsObject(Class<T> clazz); @Override @Deprecated List<T> getSourceAsObjectList(Class<T> type); Hit<T, Void> getFirstHit(Class<T> sourceType); Hit<T, K> getFirstHit(Class<T> sourceType, Class<K> explanationType); List<Hit<T, Void>> getHits(Class<T> sourceType); List<Hit<T, Void>> getHits(Class<T> sourceType, boolean addEsMetadataFields); List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType); List<Hit<T, K>> getHits(Class<T> sourceType, Class<K> explanationType, boolean addEsMetadataFields); Long getTotal(); Float getMaxScore(); MetricAggregation getAggregations(); static final String EXPLANATION_KEY; static final String HIGHLIGHT_KEY; static final String SORT_KEY; static final String[] PATH_TO_TOTAL; static final String[] PATH_TO_MAX_SCORE; }
@Test public void testGetFirstHit() { SearchResult searchResult = new SearchResult(new Gson()); searchResult.setSucceeded(true); searchResult.setJsonString(json); searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject()); searchResult.setPathToResult("hits/hits/_source"); SearchResult.Hit hit = searchResult.getFirstHit(Object.class); assertNotNull(hit); assertNotNull(hit.source); assertNull(hit.explanation); assertNotNull(hit.sort); assertNotNull(hit.id); assertNull(hit.score); assertEquals(Collections.emptyList(), hit.matchedQueries); hit = searchResult.getFirstHit(Object.class, Object.class); assertNotNull(hit); assertNotNull(hit.source); assertNull(hit.explanation); assertNotNull(hit.sort); assertNotNull(hit.id); assertNull(hit.score); assertEquals(Collections.emptyList(), hit.matchedQueries); }
@Test public void testGetFirstHitWhenOperationFails() { SearchResult searchResult = new SearchResult(new Gson()); searchResult.setSucceeded(false); SearchResult.Hit hit = searchResult.getFirstHit(Object.class); assertNull(hit); hit = searchResult.getFirstHit(Object.class, Object.class); assertNull(hit); }
@Test public void testGetScore() { String jsonWithScore = "{\n" + " \"_shards\":{\n" + " \"total\" : 5,\n" + " \"successful\" : 5,\n" + " \"failed\" : 0\n" + " },\n" + " \"hits\":{\n" + " \"total\" : 1,\n" + " \"hits\" : [\n" + " {\n" + " \"_index\" : \"twitter\",\n" + " \"_type\" : \"tweet\",\n" + " \"_score\" : \"1.02332\",\n" + " \"_id\" : \"1\",\n" + " \"_source\" : {\n" + " \"user\" : \"kimchy\",\n" + " \"postDate\" : \"2009-11-15T14:12:12\",\n" + " \"message\" : \"trying out Elasticsearch\"\n" + " },\n" + " \"sort\" : [\n" + " 1234.5678\n" + " ]\n" + " }\n" + " ]\n" + " }\n" + "}"; SearchResult searchResult = new SearchResult(new Gson()); searchResult.setSucceeded(true); searchResult.setJsonString(jsonWithScore); searchResult.setJsonObject(new JsonParser().parse(jsonWithScore).getAsJsonObject()); searchResult.setPathToResult("hits/hits/_source"); SearchResult.Hit hit = searchResult.getFirstHit(Object.class); assertNotNull(hit); 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); }
@Test public void testGetVersion() { Long someVersion = Integer.MAX_VALUE + 10L; String jsonWithVersion = "{\n" + " \"_shards\":{\n" + " \"total\" : 5,\n" + " \"successful\" : 5,\n" + " \"failed\" : 0\n" + " },\n" + " \"hits\":{\n" + " \"total\" : 1,\n" + " \"hits\" : [\n" + " {\n" + " \"_index\" : \"twitter\",\n" + " \"_type\" : \"tweet\",\n" + " \"_score\" : \"1.02332\",\n" + " \"_id\" : \"1\",\n" + " \"_version\" : \"" + someVersion + "\",\n" + " \"_source\" : {\n" + " \"user\" : \"kimchy\",\n" + " \"postDate\" : \"2009-11-15T14:12:12\",\n" + " \"message\" : \"trying out Elasticsearch\"\n" + " },\n" + " \"sort\" : [\n" + " 1234.5678\n" + " ]\n" + " }\n" + " ]\n" + " }\n" + "}"; SearchResult searchResult = new SearchResult(new Gson()); searchResult.setSucceeded(true); searchResult.setJsonString(jsonWithVersion); searchResult.setJsonObject(new JsonParser().parse(jsonWithVersion).getAsJsonObject()); searchResult.setPathToResult("hits/hits/_source"); SearchResult.Hit<TestObject, Void> hit = searchResult.getFirstHit(TestObject.class); assertNotNull(hit.source); assertNull(hit.explanation); assertNotNull(hit.sort); assertNotNull(hit.score); assertEquals("Incorrect version", someVersion, hit.source.getVersion()); }
@Test public void testParentChild() { Long someVersion = Integer.MAX_VALUE + 10L; String jsonWithVersion = "{\n" + " \"_shards\":{\n" + " \"total\" : 5,\n" + " \"successful\" : 5,\n" + " \"failed\" : 0\n" + " },\n" + " \"hits\":{\n" + " \"total\" : 1,\n" + " \"hits\" : [\n" + " {\n" + " \"_index\" : \"twitter\",\n" + " \"_type\" : \"tweet\",\n" + " \"_score\" : \"1.02332\",\n" + " \"_routing\": \"1\", \n" + " \"_parent\": \"1\", \n" + " \"_id\" : \"1\",\n" + " \"_version\" : \"" + someVersion + "\",\n" + " \"_source\" : {\n" + " \"user\" : \"kimchy\",\n" + " \"postDate\" : \"2009-11-15T14:12:12\",\n" + " \"message\" : \"trying out Elasticsearch\"\n" + " },\n" + " \"sort\" : [\n" + " 1234.5678\n" + " ]\n" + " }\n" + " ]\n" + " }\n" + "}"; SearchResult searchResult = new SearchResult(new Gson()); searchResult.setSucceeded(true); searchResult.setJsonString(jsonWithVersion); searchResult.setJsonObject(new JsonParser().parse(jsonWithVersion).getAsJsonObject()); searchResult.setPathToResult("hits/hits/_source"); SearchResult.Hit<TestObject, Void> hit = searchResult.getFirstHit(TestObject.class); assertNotNull(hit.source); assertNull(hit.explanation); assertNotNull(hit.sort); assertNotNull(hit.score); assertNotNull(hit.parent); assertNotNull(hit.routing); assertEquals("Incorrect version", someVersion, hit.source.getVersion()); }
@Test public void testGetMatchedQueries() { String jsonWithScore = "{\n" + " \"_shards\":{\n" + " \"total\" : 5,\n" + " \"successful\" : 5,\n" + " \"failed\" : 0\n" + " },\n" + " \"hits\":{\n" + " \"total\" : 1,\n" + " \"hits\" : [\n" + " {\n" + " \"_index\" : \"twitter\",\n" + " \"_type\" : \"tweet\",\n" + " \"_score\" : \"1.02332\",\n" + " \"_id\" : \"1\",\n" + " \"_source\" : {\n" + " \"user\" : \"kimchy\",\n" + " \"postDate\" : \"2009-11-15T14:12:12\",\n" + " \"message\" : \"trying out Elasticsearch\"\n" + " },\n" + " \"sort\" : [\n" + " 1234.5678\n" + " ], \n" + " \"matched_queries\" : [\"some-query\"] \n" + " }\n" + " ]\n" + " }\n" + "}"; SearchResult searchResult = new SearchResult(new Gson()); searchResult.setSucceeded(true); searchResult.setJsonString(jsonWithScore); searchResult.setJsonObject(new JsonParser().parse(jsonWithScore).getAsJsonObject()); searchResult.setPathToResult("hits/hits/_source"); SearchResult.Hit hit = searchResult.getFirstHit(Object.class); assertNotNull(hit); assertNotNull(hit.source); assertNull(hit.explanation); assertNotNull(hit.sort); assertNotNull(hit.id); assertNotNull(hit.score); assertEquals("Incorrect Matched Query", Collections.singletonList("some-query"), hit.matchedQueries); hit = searchResult.getFirstHit(Object.class, Object.class); assertNotNull(hit); assertNotNull(hit.source); assertNull(hit.explanation); assertNotNull(hit.sort); assertNotNull(hit.id); assertNotNull(hit.score); assertEquals("Incorrect Matched Query", Collections.singletonList("some-query"), hit.matchedQueries); }
@Test public void testGetHitsWithoutSource() { Long version = 2L; String jsonWithoutSource = "{\n" + " \"_shards\":{\n" + " \"total\" : 5,\n" + " \"successful\" : 5,\n" + " \"failed\" : 0\n" + " },\n" + " \"hits\":{\n" + " \"total\" : 1,\n" + " \"hits\" : [\n" + " {\n" + " \"_index\" : \"twitter\",\n" + " \"_type\" : \"tweet\",\n" + " \"_score\" : \"1.02332\",\n" + " \"_id\" : \"1\",\n" + " \"_version\" : \"" + version + "\"\n" + " }\n" + " ]\n" + " }\n" + "}"; SearchResult searchResult = new SearchResult(new Gson()); searchResult.setSucceeded(true); searchResult.setJsonString(jsonWithoutSource); searchResult.setJsonObject(new JsonParser().parse(jsonWithoutSource).getAsJsonObject()); searchResult.setPathToResult("hits/hits/_source"); SearchResult.Hit<TestObject, Void> hit = searchResult.getFirstHit(TestObject.class); assertNotNull(hit.source); assertNull(hit.explanation); assertNull(hit.sort); assertNotNull(hit.score); assertEquals("1", hit.source.getId()); assertEquals(version, hit.source.getVersion()); }