Java Code Examples for org.elasticsearch.search.SearchHits#getHits()

The following examples show how to use org.elasticsearch.search.SearchHits#getHits() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScrollSpout.java    From storm-crawler with Apache License 2.0 6 votes vote down vote up
@Override
public void onResponse(SearchResponse response) {
    SearchHits hits = response.getHits();
    LOG.info("{} ES query returned {} hits in {} msec", logIdprefix,
            hits.getHits().length, response.getTook().getMillis());
    hasFinished = hits.getHits().length == 0;
    synchronized (this.queue) {
        // Unlike standard spouts, the scroll queries should never return
        // the same
        // document twice -> no need to look in the buffer or cache
        for (SearchHit hit : hits) {
            Map<String, Object> keyValues = hit.getSourceAsMap();
            String url = (String) keyValues.get("url");
            String status = (String) keyValues.get("status");
            String nextFetchDate = (String) keyValues.get("nextFetchDate");
            Metadata metadata = fromKeyValues(keyValues);
            metadata.setValue(
                    AbstractStatusUpdaterBolt.AS_IS_NEXTFETCHDATE_METADATA,
                    nextFetchDate);
            this.queue.add(new Values(url, metadata, Status.valueOf(status)));
        }
    }
    scrollId = response.getScrollId();
    // remove lock
    markQueryReceivedNow();
}
 
Example 2
Source File: ElasticsearchUtil.java    From SpringBootLearn with Apache License 2.0 6 votes vote down vote up
/**
     * 处理scroll结果
     * @Author lihaodong
     * @Description
     * @Date 20:18 2018/12/21
     * @Param [response, highlightField]
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    private static   List<Map<String, Object>>   disposeScrollResult(SearchResponse response ,String highlightField){
        List<Map<String, Object>> sourceList = new ArrayList<Map<String, Object>>();
        //使用scrollId迭代查询
        while (response.getHits().getHits().length > 0) {
            String scrollId = response.getScrollId();
            response = client.prepareSearchScroll(scrollId)
                    .setScroll(TimeValue.timeValueMinutes(1))//设置查询context的存活时间
                    .execute()
                    .actionGet();
            SearchHits hits = response.getHits();
            for (SearchHit hit : hits.getHits()) {
                Map<String, Object> resultMap =getResultMap(hit, highlightField);
                sourceList.add(resultMap);
//                System.out.println(JSON.toJSONString(resultMap));
            }
        }
        ClearScrollRequest request = new ClearScrollRequest();
        request.addScrollId(response.getScrollId());
        client.clearScroll(request);
        return sourceList;
    }
 
Example 3
Source File: TestSchema.java    From kafka-connect-elasticsearch-source with Apache License 2.0 6 votes vote down vote up
public void testSearch() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    searchRequest.source(searchSourceBuilder);
    searchRequest.indices("metricbeat-6.2.4-2018.05.20");
    SearchResponse searchResponse = es.getClient().search(searchRequest);
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.getHits();
    for (SearchHit hit : searchHits) {
        // do something with the SearchHit
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        System.out.println(sourceAsMap);
        Schema schema = SchemaConverter.convertElasticMapping2AvroSchema(sourceAsMap, "test");
        schema.toString();
        Struct struct = StructConverter.convertElasticDocument2AvroStruct(sourceAsMap,schema);
        struct.toString();
    }

}
 
Example 4
Source File: ESClientTest.java    From emotional_analysis with Apache License 2.0 5 votes vote down vote up
@Test
public void test4(){
	SearchRequestBuilder responsebuilder = client.prepareSearch("twitter").setTypes("tweet") ;
	SearchResponse myresponse=responsebuilder.setQuery(QueryBuilders.matchPhraseQuery("user", "kimchy"))  
			.setFrom(0).setSize(10).setExplain(true).execute().actionGet();
	SearchHits hits = myresponse.getHits();  
	for (int i = 0; i < hits.getHits().length; i++) {  
	           System.out.println(hits.getHits()[i].getSourceAsString());}  
 }
 
Example 5
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void orderByDescTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	SearchHits response = query(String.format("SELECT age FROM %s/account ORDER BY age DESC LIMIT 1000", TEST_INDEX_ACCOUNT));
	SearchHit[] hits = response.getHits();

	ArrayList<Integer> ages = new ArrayList<Integer>();
	for(SearchHit hit : hits) {
		ages.add((int)hit.getSourceAsMap().get("age"));
	}

	ArrayList<Integer> sortedAges = (ArrayList<Integer>)ages.clone();
	Collections.sort(sortedAges, Collections.reverseOrder());
	Assert.assertTrue("The list is not ordered descending", sortedAges.equals(ages));
}
 
Example 6
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
@Override
protected int delegateQueryDelete(final ConditionBean cb, final DeleteOption<? extends ConditionBean> option) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForDelete).setSize(sizeForDelete);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        esCb.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    int count = 0;
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }

            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                bulkRequest.add(client.prepareDelete().setIndex(asEsIndex()).setId(hit.getId()));
            }
            count += hits.length;
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
            if (bulkResponse.hasFailures()) {
                throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
            }

            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
        }
    } finally {
        deleteScrollContext(scrollId);
    }
    return count;
}
 
Example 7
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void selectFieldWithSpace() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	String[] arr = new String[] {"test field"};
	Set expectedSource = new HashSet(Arrays.asList(arr));

	SearchHits response = query(String.format("SELECT ['test field'] FROM %s/phrase where ['test field'] is not null", TEST_INDEX_PHRASE));
	SearchHit[] hits = response.getHits();
	for(SearchHit hit : hits) {
		Assert.assertEquals(expectedSource, hit.getSourceAsMap().keySet());
	}
}
 
Example 8
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void inTermsTestWithNumbers() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
    SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.ofHisName = IN_TERMS(4,2) LIMIT 1000", TEST_INDEX_GAME_OF_THRONES));
    SearchHit[] hits = response.getHits();
    Assert.assertEquals(1, response.getTotalHits().value);
    SearchHit hit = hits[0];
    String firstname =  ((Map<String,Object>) hit.getSourceAsMap().get("name")).get("firstname").toString();
    Assert.assertEquals("Brandon",firstname);
}
 
Example 9
Source File: SourceFieldTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void excludeTest() throws SqlParseException, SQLFeatureNotSupportedException {

	SearchHits response = query(String.format("SELECT exclude('*name','*ge'),exclude('b*'),exclude('*ddre*'),exclude('gender') FROM %s/account LIMIT 1000", TEST_INDEX_ACCOUNT));

	for (SearchHit hit : response.getHits()) {
		Set<String> keySet = hit.getSourceAsMap().keySet();
		for (String field : keySet) {
			Assert.assertFalse(field.endsWith("name") || field.endsWith("ge") || field.startsWith("b") || field.contains("ddre") || field.equals("gender"));
		}
	}
}
 
Example 10
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void notLikeTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	SearchHits response = query(String.format("SELECT * FROM %s/account WHERE firstname NOT LIKE 'amb%%'", TEST_INDEX_ACCOUNT));
	SearchHit[] hits = response.getHits();

	// assert we got hits
	Assert.assertNotEquals(0, response.getTotalHits().value);
	for (SearchHit hit : hits) {
		Assert.assertFalse(hit.getSourceAsMap().get("firstname").toString().toLowerCase().startsWith("amb"));
	}
}
 
Example 11
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void termQueryWithNumber() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
    SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.ofHisName = term(4) LIMIT 1000", TEST_INDEX_GAME_OF_THRONES));
    SearchHit[] hits = response.getHits();
    Assert.assertEquals(1, response.getTotalHits().value);
    SearchHit hit = hits[0];
    String firstname =  ((Map<String,Object>) hit.getSourceAsMap().get("name")).get("firstname").toString();
    Assert.assertEquals("Brandon",firstname);
}
 
Example 12
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void betweenTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	int min = 27;
	int max = 30;
	SearchHits response = query(String.format("SELECT * FROM %s WHERE age BETWEEN %s AND %s LIMIT 1000", TEST_INDEX_PEOPLE, min, max));
	SearchHit[] hits = response.getHits();
	for(SearchHit hit : hits) {
		int age = (int) hit.getSourceAsMap().get("age");
		assertThat(age, allOf(greaterThanOrEqualTo(min), lessThanOrEqualTo(max)));
	}
}
 
Example 13
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void inTermsTestWithIdentifiersTreatLikeStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
    SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = IN_TERMS(daenerys,eddard) LIMIT 1000", TEST_INDEX_GAME_OF_THRONES));
    SearchHit[] hits = response.getHits();
    Assert.assertEquals(2, response.getTotalHits().value);
    for(SearchHit hit : hits) {
        String firstname =  ((Map<String,Object>) hit.getSourceAsMap().get("name")).get("firstname").toString();
        assertThat(firstname, isOneOf("Daenerys", "Eddard"));
    }
}
 
Example 14
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void equallityTest_phrase() throws SqlParseException, SQLFeatureNotSupportedException {
	SearchHits response = query(String.format("SELECT * FROM %s/phrase WHERE phrase = 'quick fox here' LIMIT 1000", TEST_INDEX_PHRASE));
	SearchHit[] hits = response.getHits();

	// assert the results is correct according to accounts.json data.
	Assert.assertEquals(1, response.getTotalHits().value);
	Assert.assertEquals("quick fox here", hits[0].getSourceAsMap().get("phrase"));
}
 
Example 15
Source File: JsonContent.java    From elasticsearch-dataformat with Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(final SearchResponse response) {
    final String scrollId = response.getScrollId();
    final SearchHits hits = response.getHits();
    final int size = hits.getHits().length;
    currentCount += size;
    if (logger.isDebugEnabled()) {
        logger.debug("scrollId: {}, totalHits: {}, hits: {}, current: {}",
                scrollId, hits.getTotalHits(), size, currentCount);
    }
    try {
        for (final SearchHit hit : hits) {
            final String index = bulkIndex == null ? hit.getIndex()
                    : bulkIndex;
            final String operation = "{\"index\":{\"_index\":\"" + index
                    + "\",\"_id\":\"" + hit.getId() + "\"}}";
            final String source = XContentHelper.convertToJson(
                    hit.getSourceRef(), true, false, XContentType.JSON);
            writer.append(operation).append('\n');
            writer.append(source).append('\n');
        }

        if (size == 0 || scrollId == null) {
            // end
            writer.flush();
            close();
            listener.onResponse(null);
        } else {
            client.prepareSearchScroll(scrollId)
                    .setScroll(RequestUtil.getScroll(request))
                    .execute(this);
        }
    } catch (final Exception e) {
        onFailure(e);
    }
}
 
Example 16
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void greaterThanTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	int someAge = 25;
	SearchHits response = query(String.format("SELECT * FROM %s WHERE age > %s LIMIT 1000", TEST_INDEX_PEOPLE, someAge));
	SearchHit[] hits = response.getHits();
	for(SearchHit hit : hits) {
		int age = (int) hit.getSourceAsMap().get("age");
		assertThat(age, greaterThan(someAge));
	}
}
 
Example 17
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
public long updateByQuery(final String index, final Function<SearchRequestBuilder, SearchRequestBuilder> option,
        final BiFunction<UpdateRequestBuilder, SearchHit, UpdateRequestBuilder> builder) {

    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    SearchResponse response =
            option.apply(
                    client.prepareSearch(index).setScroll(scrollForUpdate).setSize(sizeForUpdate)
                            .setPreference(Constants.SEARCH_PREFERENCE_LOCAL)).execute()
                    .actionGet(fessConfig.getIndexScrollSearchTimeout());

    int count = 0;
    String scrollId = response.getScrollId();
    try {
        while (scrollId != null) {
            final SearchHits searchHits = response.getHits();
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }

            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                final UpdateRequestBuilder requestBuilder =
                        builder.apply(client.prepareUpdate().setIndex(index).setId(hit.getId()), hit);
                if (requestBuilder != null) {
                    bulkRequest.add(requestBuilder);
                }
                count++;
            }
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet(fessConfig.getIndexBulkTimeout());
            if (bulkResponse.hasFailures()) {
                throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
            }

            response =
                    client.prepareSearchScroll(scrollId).setScroll(scrollForUpdate).execute()
                            .actionGet(fessConfig.getIndexBulkTimeout());
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
            scrollId = response.getScrollId();
        }
    } finally {
        deleteScrollContext(scrollId);
    }
    return count;
}
 
Example 18
Source File: TasteSearchRestAction.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
@Override
protected void handleRequest(final RestRequest request,
        final RestChannel channel, final Client client) {

    final Info info = new Info(request);

    final String systemId = request.param("systemId");
    if (StringUtils.isBlank(systemId)) {
        onError(channel, new NotFoundException("No system_id."));
        return;
    }
    final String[] systemIds = systemId.trim().split(",");
    if (systemIds.length == 0) {
        onError(channel, new NotFoundException("No system_id."));
        return;
    }

    if (info.getIdIndex() == null) {
        onError(channel, new NotFoundException("No search type."));
        return;
    }

    final OnResponseListener<SearchResponse> responseListener = searchResponse -> {
        final SearchHits hits = searchResponse.getHits();
        if (hits.totalHits() == 0) {
            onError(channel,
                    new NotFoundException("No " + info.getIdField()
                            + " data for " + systemId + " in "
                            + info.getIdIndex() + "/" + info.getIdType()));
            return;
        }

        final SearchHit[] searchHits = hits.getHits();
        final long[] targetIds = new long[hits.getHits().length];
        for (int i = 0; i < targetIds.length && i < searchHits.length; i++) {
            final SearchHit hit = searchHits[i];
            final SearchHitField field = hit.field(info.getIdField());
            final Number targetId = field.getValue();
            if (targetId != null) {
                targetIds[i] = targetId.longValue();
            }
        }

        if (targetIds.length == 0) {
            onError(channel,
                    new NotFoundException("No " + info.getIdField()
                            + " for " + systemId + " in "
                            + info.getIdIndex() + "/" + info.getIdType()));
            return;
        }

        doSearchRequest(request, channel, client, info, targetIds);
    };

    final BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    for (final String id : systemIds) {
        boolQueryBuilder.should(QueryBuilders.termQuery("system_id", id));
    }
    boolQueryBuilder.minimumNumberShouldMatch(1);

    client.prepareSearch(info.getIdIndex()).setTypes(info.getIdType())
            .setQuery(boolQueryBuilder).addField(info.getIdField())
            .addSort(info.getTimestampField(), SortOrder.DESC)
            .setSize(systemIds.length)
            .execute(on(responseListener, t -> onError(channel, t)));

}
 
Example 19
Source File: SearchApiMain.java    From elasticsearch-pool with Apache License 2.0 4 votes vote down vote up
public static void searchApi() throws IOException {

        RestHighLevelClient client = HighLevelClient.getInstance();

        try {
            SearchRequest searchRequest = new SearchRequest("jingma2_test");//限定index
            searchRequest.types("testlog");//限定type

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            /*查询所有记录*/

//        searchSourceBuilder.query(QueryBuilders.matchAllQuery());


            /*根据匹配查询*/
            QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "风雷");
            /*设置中文分词器*/
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_max_word");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_smart");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("standard");
            searchSourceBuilder.query(matchQueryBuilder);

            /*限定查询条件和查询条数*/
//            searchSourceBuilder.query(QueryBuilders.termQuery("name", "风雷"));
            searchSourceBuilder.from(0);
            searchSourceBuilder.size(5);
//            searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

            /*限定查询结果排序*/
//            searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
//            searchSourceBuilder.sort(new FieldSortBuilder("age").order(SortOrder.ASC));

            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest);
            System.out.println(searchResponse);

            SearchHits hits = searchResponse.getHits();
            long totalHits = hits.getTotalHits();
            float maxScore = hits.getMaxScore();

            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                System.out.println(sourceAsString);
//                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            }

        }finally {
            HighLevelClient.close();
        }

    }
 
Example 20
Source File: ImageIntegrationTests.java    From elasticsearch-image with Apache License 2.0 4 votes vote down vote up
@Test
public void test_index_search_image() throws Exception {
    String mapping = copyToStringFromClasspath("/mapping/test-mapping.json");
    client().admin().indices().putMapping(putMappingRequest(INDEX_NAME).type(DOC_TYPE_NAME).source(mapping)).actionGet();

    int totalImages = randomIntBetween(10, 50);

    // generate random images and index
    String nameToSearch = null;
    byte[] imgToSearch = null;
    String idToSearch = null;
    for (int i = 0; i < totalImages; i ++) {
        byte[] imageByte = getRandomImage();
        String name = randomAsciiOfLength(5);
        IndexResponse response = index(INDEX_NAME, DOC_TYPE_NAME, jsonBuilder().startObject().field("img", imageByte).field("name", name).endObject());
        if (nameToSearch == null || imgToSearch == null || idToSearch == null) {
            nameToSearch = name;
            imgToSearch = imageByte;
            idToSearch = response.getId();
        }
    }

    refresh();

    // test search with hash
    ImageQueryBuilder imageQueryBuilder = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).image(imgToSearch).hash(HashEnum.BIT_SAMPLING.name());
    SearchResponse searchResponse = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder).addFields("img.metadata.exif_ifd0.x_resolution", "name").setSize(totalImages).get();
    assertNoFailures(searchResponse);
    SearchHits hits = searchResponse.getHits();
    assertThat("Should match at least one image", hits.getTotalHits(), greaterThanOrEqualTo(1l)); // if using hash, total result maybe different than number of images
    SearchHit hit = hits.getHits()[0];
    assertThat("First should be exact match and has score 1", hit.getScore(), equalTo(2.0f));
    assertImageScore(hits, nameToSearch, 2.0f);
    assertThat("Should have metadata", hit.getFields().get("img.metadata.exif_ifd0.x_resolution").getValues(), hasSize(1));

    // test search without hash and with boost
    ImageQueryBuilder imageQueryBuilder2 = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).image(imgToSearch).boost(2.0f);
    SearchResponse searchResponse2 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder2).setSize(totalImages).get();
    assertNoFailures(searchResponse2);
    SearchHits hits2 = searchResponse2.getHits();
    assertThat("Should get all images", hits2.getTotalHits(), equalTo((long)totalImages));  // no hash used, total result should be same as number of images
    assertThat("First should be exact match and has score 2", searchResponse2.getHits().getMaxScore(), equalTo(4.0f));
    assertImageScore(hits2, nameToSearch, 4.0f);

    // test search for name as well
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must(QueryBuilders.termQuery("name", nameToSearch));
    boolQueryBuilder.must(new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).image(imgToSearch));
    SearchResponse searchResponse3 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(boolQueryBuilder).setSize(totalImages).get();
    assertNoFailures(searchResponse3);
    SearchHits hits3 = searchResponse3.getHits();
    assertThat("Should match one document only", hits3.getTotalHits(), equalTo(1l)); // added filename to query, should have only one result
    SearchHit hit3 = hits3.getHits()[0];
    assertThat((String)hit3.getSource().get("name"), equalTo(nameToSearch));

    // test search with hash and limit
    ImageQueryBuilder imageQueryBuilder4 = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).image(imgToSearch).hash(HashEnum.BIT_SAMPLING.name()).limit(10);
    SearchResponse searchResponse4 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder4).setSize(totalImages).get();
    assertNoFailures(searchResponse4);
    SearchHits hits4 = searchResponse4.getHits();
    assertThat("Should match at least one image", hits4.getTotalHits(), greaterThanOrEqualTo(1l)); // if using hash, total result maybe different than number of images
    SearchHit hit4 = hits4.getHits()[0];
    assertThat("First should be exact match and has score 1", hit4.getScore(), equalTo(2.0f));
    assertImageScore(hits4, nameToSearch, 2.0f);

    // test search metadata
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("img.metadata.exif_ifd0.x_resolution", "72 dots per inch");
    SearchResponse searchResponse5 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(termQueryBuilder).setSize(totalImages).get();
    assertNoFailures(searchResponse5);
    SearchHits hits5 = searchResponse5.getHits();
    assertThat("Should match at least one record", hits5.getTotalHits(), greaterThanOrEqualTo(1l)); // if using hash, total result maybe different than number of images

    // test search with exist image
    ImageQueryBuilder imageQueryBuilder6 = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).lookupIndex(INDEX_NAME).lookupType(DOC_TYPE_NAME).lookupId(idToSearch).lookupPath("img");
    SearchResponse searchResponse6 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder6).setSize(totalImages).get();
    assertNoFailures(searchResponse6);
    SearchHits hits6 = searchResponse6.getHits();
    assertThat("Should match at least one image", hits6.getTotalHits(), equalTo((long) totalImages));
    SearchHit hit6 = hits6.getHits()[0];
    assertThat("First should be exact match and has score 1", hit6.getScore(), equalTo(2.0f));
    assertImageScore(hits6, nameToSearch, 2.0f);

    // test search with exist image using hash
    ImageQueryBuilder imageQueryBuilder7 = new ImageQueryBuilder("img").feature(FeatureEnum.CEDD.name()).lookupIndex(INDEX_NAME).lookupType(DOC_TYPE_NAME).lookupId(idToSearch).lookupPath("img").hash(HashEnum.BIT_SAMPLING.name());
    SearchResponse searchResponse7 = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE_NAME).setQuery(imageQueryBuilder7).setSize(totalImages).get();
    assertNoFailures(searchResponse7);
    SearchHits hits7 = searchResponse7.getHits();
    assertThat("Should match at least one image", hits7.getTotalHits(), equalTo((long) totalImages));
    SearchHit hit7 = hits7.getHits()[0];
    assertThat("First should be exact match and has score 1", hit7.getScore(), equalTo(2.0f));
    assertImageScore(hits7, nameToSearch, 2.0f);
}