Search in sources :

Example 31 with SearchHits

use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class QueryRescorerIT method assertEquivalentOrSubstringMatch.

private static void assertEquivalentOrSubstringMatch(String query, SearchResponse plain, SearchResponse rescored) {
    assertNoFailures(plain);
    assertNoFailures(rescored);
    SearchHits leftHits = plain.getHits();
    SearchHits rightHits = rescored.getHits();
    assertThat(leftHits.getTotalHits(), equalTo(rightHits.getTotalHits()));
    assertThat(leftHits.getHits().length, equalTo(rightHits.getHits().length));
    SearchHit[] hits = leftHits.getHits();
    SearchHit[] otherHits = rightHits.getHits();
    if (!hits[0].getId().equals(otherHits[0].getId())) {
        assertThat(((String) otherHits[0].getSourceAsMap().get("field1")).contains(query), equalTo(true));
    } else {
        Arrays.sort(hits, searchHitsComparator);
        Arrays.sort(otherHits, searchHitsComparator);
        for (int i = 0; i < hits.length; i++) {
            if (hits[i].getScore() == hits[hits.length - 1].getScore()) {
                // we need to cut off here since this is the tail of the queue and we might not have fetched enough docs
                return;
            }
            assertThat(query, hits[i].getId(), equalTo(rightHits.getHits()[i].getId()));
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits)

Example 32 with SearchHits

use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class SearchQueryIT method testConstantScoreQuery.

// see #3521
public void testConstantScoreQuery() throws Exception {
    Random random = random();
    createIndex("test");
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox"), client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox"));
    SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(matchQuery("field1", "quick"))).get();
    assertHitCount(searchResponse, 2L);
    for (SearchHit searchHit : searchResponse.getHits().getHits()) {
        assertSearchHit(searchHit, hasScore(1.0f));
    }
    searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + random().nextFloat()))).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
    client().prepareSearch("test").setQuery(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + random().nextFloat())).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
    searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat()))))).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
    for (SearchHit searchHit : searchResponse.getHits().getHits()) {
        assertSearchHit(searchHit, hasScore(1.0f));
    }
    int num = scaledRandomIntBetween(100, 200);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[num];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test_1", "type", "" + i).setSource("f", English.intToEnglish(i));
    }
    createIndex("test_1");
    indexRandom(true, builders);
    int queryRounds = scaledRandomIntBetween(10, 20);
    for (int i = 0; i < queryRounds; i++) {
        MatchQueryBuilder matchQuery = matchQuery("f", English.intToEnglish(between(0, num)));
        searchResponse = client().prepareSearch("test_1").setQuery(constantScoreQuery(matchQuery)).setSize(num).get();
        long totalHits = searchResponse.getHits().getTotalHits();
        SearchHits hits = searchResponse.getHits();
        for (SearchHit searchHit : hits) {
            assertSearchHit(searchHit, hasScore(1.0f));
        }
        searchResponse = client().prepareSearch("test_1").setQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat())))).setSize(num).get();
        hits = searchResponse.getHits();
        assertThat(hits.getTotalHits(), equalTo(totalHits));
        if (totalHits > 1) {
            float expected = hits.getAt(0).getScore();
            for (SearchHit searchHit : hits) {
                assertSearchHit(searchHit, hasScore(expected));
            }
        }
    }
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Random(java.util.Random) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 33 with SearchHits

use of org.elasticsearch.search.SearchHits in project graylog2-server by Graylog2.

the class ScrollResult method nextChunk.

public ScrollChunk nextChunk() {
    final SearchResponse search;
    // make sure to return the initial hits, see https://github.com/Graylog2/graylog2-server/issues/2126
    if (firstResponse == null) {
        search = client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMinutes(1)).execute().actionGet();
    } else {
        search = firstResponse;
        firstResponse = null;
    }
    final SearchHits hits = search.getHits();
    if (hits.getHits().length == 0) {
        // scroll exhausted
        LOG.debug("[{}] Reached end of scroll results.", queryHash, getOriginalQuery());
        return null;
    }
    LOG.debug("[{}][{}] New scroll id {}, number of hits in chunk: {}", queryHash, chunkId, search.getScrollId(), hits.getHits().length);
    // save the id for the next request.
    scrollId = search.getScrollId();
    return new ScrollChunk(hits, fields, chunkId++);
}
Also used : SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 34 with SearchHits

use of org.elasticsearch.search.SearchHits in project titan by thinkaurelius.

the class ElasticSearchIndex method query.

@Override
public Iterable<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    SearchRequestBuilder srb = client.prepareSearch(indexName);
    srb.setTypes(query.getStore());
    srb.setQuery(QueryBuilders.queryStringQuery(query.getQuery()));
    srb.setFrom(query.getOffset());
    if (query.hasLimit())
        srb.setSize(query.getLimit());
    else
        srb.setSize(maxResultsSize);
    srb.setNoFields();
    //srb.setExplain(true);
    SearchResponse response = srb.execute().actionGet();
    log.debug("Executed query [{}] in {} ms", query.getQuery(), response.getTookInMillis());
    SearchHits hits = response.getHits();
    if (!query.hasLimit() && hits.totalHits() >= maxResultsSize)
        log.warn("Query result set truncated to first [{}] elements for query: {}", maxResultsSize, query);
    List<RawQuery.Result<String>> result = new ArrayList<RawQuery.Result<String>>(hits.hits().length);
    for (SearchHit hit : hits) {
        result.add(new RawQuery.Result<String>(hit.id(), hit.getScore()));
    }
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 35 with SearchHits

use of org.elasticsearch.search.SearchHits in project fess by codelibs.

the class FessEsClient method deleteByQuery.

public int deleteByQuery(final String index, final String type, final QueryBuilder queryBuilder) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    SearchResponse response = client.prepareSearch(index).setTypes(type).setScroll(scrollForDelete).setSize(sizeForDelete).setFetchSource(new String[] { fessConfig.getIndexFieldId() }, null).setQuery(queryBuilder).setPreference(Constants.SEARCH_PREFERENCE_PRIMARY).execute().actionGet(fessConfig.getIndexScrollSearchTimeoutTimeout());
    int count = 0;
    String scrollId = response.getScrollId();
    while (scrollId != null) {
        final SearchHits searchHits = response.getHits();
        final SearchHit[] hits = searchHits.getHits();
        if (hits.length == 0) {
            scrollId = null;
            break;
        }
        final BulkRequestBuilder bulkRequest = client.prepareBulk();
        for (final SearchHit hit : hits) {
            bulkRequest.add(client.prepareDelete(index, type, hit.getId()));
        }
        count += hits.length;
        final BulkResponse bulkResponse = bulkRequest.execute().actionGet(fessConfig.getIndexBulkTimeout());
        if (bulkResponse.hasFailures()) {
            throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
        }
        response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(fessConfig.getIndexBulkTimeout());
        scrollId = response.getScrollId();
    }
    return count;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) IllegalBehaviorStateException(org.dbflute.exception.IllegalBehaviorStateException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) SearchHits(org.elasticsearch.search.SearchHits) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)136 SearchResponse (org.elasticsearch.action.search.SearchResponse)92 SearchHit (org.elasticsearch.search.SearchHit)86 ArrayList (java.util.ArrayList)45 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)27 IOException (java.io.IOException)24 Map (java.util.Map)23 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 HashMap (java.util.HashMap)19 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)16 SearchRequest (org.elasticsearch.action.search.SearchRequest)15 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)15 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)13 TotalHits (org.apache.lucene.search.TotalHits)12 Test (org.junit.Test)12 ScoreDoc (org.apache.lucene.search.ScoreDoc)11 Text (org.elasticsearch.common.text.Text)11 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)11 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)10