Search in sources :

Example 71 with SearchHits

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

the class FetchSearchPhaseTests method testShortcutQueryAndFetchOptimization.

public void testShortcutQueryAndFetchOptimization() throws IOException {
    SearchPhaseController controller = new SearchPhaseController(Settings.EMPTY, BigArrays.NON_RECYCLING_INSTANCE, null);
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> results = controller.newSearchPhaseResults(mockSearchPhaseContext.getRequest(), 1);
    AtomicReference<SearchResponse> responseRef = new AtomicReference<>();
    boolean hasHits = randomBoolean();
    final int numHits;
    if (hasHits) {
        QuerySearchResult queryResult = new QuerySearchResult();
        queryResult.topDocs(new TopDocs(1, new ScoreDoc[] { new ScoreDoc(42, 1.0F) }, 1.0F), new DocValueFormat[0]);
        queryResult.size(1);
        FetchSearchResult fetchResult = new FetchSearchResult();
        fetchResult.hits(new SearchHits(new SearchHit[] { new SearchHit(42) }, 1, 1.0F));
        results.consumeResult(0, new QueryFetchSearchResult(queryResult, fetchResult));
        numHits = 1;
    } else {
        numHits = 0;
    }
    FetchSearchPhase phase = new FetchSearchPhase(results, controller, mockSearchPhaseContext, (searchResponse) -> new SearchPhase("test") {

        @Override
        public void run() throws IOException {
            responseRef.set(searchResponse);
        }
    });
    assertEquals("fetch", phase.getName());
    phase.run();
    mockSearchPhaseContext.assertNoFailure();
    assertNotNull(responseRef.get());
    assertEquals(numHits, responseRef.get().getHits().totalHits);
    if (numHits != 0) {
        assertEquals(42, responseRef.get().getHits().getAt(0).docId());
    }
    assertTrue(mockSearchPhaseContext.releasedSearchContexts.isEmpty());
}
Also used : QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) SearchHit(org.elasticsearch.search.SearchHit) QueryFetchSearchResult(org.elasticsearch.search.fetch.QueryFetchSearchResult) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) QueryFetchSearchResult(org.elasticsearch.search.fetch.QueryFetchSearchResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchHits(org.elasticsearch.search.SearchHits)

Example 72 with SearchHits

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

the class DecayFunctionScoreIT method testDistanceScoreGeoLinGaussExp.

public void testDistanceScoreGeoLinGaussExp() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("loc").field("type", "geo_point").endObject().endObject().endObject().endObject()));
    List<IndexRequestBuilder> indexBuilders = new ArrayList<>();
    indexBuilders.add(client().prepareIndex().setType("type1").setId("1").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 10).field("lon", 20).endObject().endObject()));
    indexBuilders.add(client().prepareIndex().setType("type1").setId("2").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 11).field("lon", 22).endObject().endObject()));
    int numDummyDocs = 20;
    for (int i = 1; i <= numDummyDocs; i++) {
        indexBuilders.add(client().prepareIndex().setType("type1").setId(Integer.toString(i + 3)).setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 11 + i).field("lon", 22 + i).endObject().endObject()));
    }
    indexRandom(true, indexBuilders);
    // Test Gauss
    List<Float> lonlat = new ArrayList<>();
    lonlat.add(20f);
    lonlat.add(11f);
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(baseQuery)));
    SearchResponse sr = response.actionGet();
    SearchHits sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("loc", lonlat, "1000km")))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat(sh.getAt(1).getId(), equalTo("2"));
    // Test Exp
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(baseQuery)));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, linearDecayFunction("loc", lonlat, "1000km")))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat(sh.getAt(1).getId(), equalTo("2"));
    // Test Lin
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(baseQuery)));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, exponentialDecayFunction("loc", lonlat, "1000km")))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat(sh.getAt(1).getId(), equalTo("2"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 73 with SearchHits

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

the class DecayFunctionScoreIT method testManyDocsLin.

public void testManyDocsLin() throws Exception {
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_5_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    XContentBuilder xContentBuilder = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("test").field("type", "text").endObject().startObject("date").field("type", "date").field("doc_values", true).endObject().startObject("num").field("type", "double").field("doc_values", true).endObject().startObject("geo").field("type", "geo_point").field("ignore_malformed", true);
    xContentBuilder.endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type", xContentBuilder));
    int numDocs = 200;
    List<IndexRequestBuilder> indexBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        double lat = 100 + (int) (10.0 * (i) / (numDocs));
        double lon = 100;
        int day = (int) (29.0 * (i) / (numDocs)) + 1;
        String dayString = day < 10 ? "0" + Integer.toString(day) : Integer.toString(day);
        String date = "2013-05-" + dayString;
        indexBuilders.add(client().prepareIndex().setType("type").setId(Integer.toString(i)).setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").field("date", date).field("num", i).startObject("geo").field("lat", lat).field("lon", lon).endObject().endObject()));
    }
    indexRandom(true, indexBuilders);
    List<Float> lonlat = new ArrayList<>();
    lonlat.add(100f);
    lonlat.add(110f);
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().size(numDocs).query(functionScoreQuery(termQuery("test", "value"), new FilterFunctionBuilder[] { new FilterFunctionBuilder(linearDecayFunction("date", "2013-05-30", "+15d")), new FilterFunctionBuilder(linearDecayFunction("geo", lonlat, "1000km")), new FilterFunctionBuilder(linearDecayFunction("num", numDocs, numDocs / 2.0)) }).scoreMode(ScoreMode.MULTIPLY).boostMode(CombineFunction.REPLACE))));
    SearchResponse sr = response.actionGet();
    assertNoFailures(sr);
    SearchHits sh = sr.getHits();
    assertThat(sh.getHits().length, equalTo(numDocs));
    double[] scores = new double[numDocs];
    for (int i = 0; i < numDocs; i++) {
        scores[Integer.parseInt(sh.getAt(i).getId())] = sh.getAt(i).getScore();
    }
    for (int i = 0; i < numDocs - 1; i++) {
        assertThat(scores[i], lessThan(scores[i + 1]));
    }
}
Also used : FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) ArrayList(java.util.ArrayList) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Version(org.elasticsearch.Version) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 74 with SearchHits

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

the class OldIndexBackwardsCompatibilityIT method assertStoredBinaryFields.

/**
     * Make sure we can load stored binary fields.
     */
void assertStoredBinaryFields(String indexName, Version version) throws Exception {
    SearchRequestBuilder builder = client().prepareSearch(indexName);
    builder.setQuery(QueryBuilders.matchAllQuery());
    builder.setSize(100);
    builder.addStoredField("binary");
    SearchHits hits = builder.get().getHits();
    assertEquals(100, hits.getHits().length);
    for (SearchHit hit : hits) {
        SearchHitField field = hit.field("binary");
        assertNotNull(field);
        Object value = field.getValue();
        assertTrue(value instanceof BytesArray);
        assertEquals(16, ((BytesArray) value).length());
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits)

Example 75 with SearchHits

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

the class SearchWhileRelocatingIT method testSearchAndRelocateConcurrently.

private void testSearchAndRelocateConcurrently(final int numberOfReplicas) throws Exception {
    final int numShards = between(1, 20);
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", numShards).put("index.number_of_replicas", numberOfReplicas)).addMapping("type", "loc", "type=geo_point", "test", "type=text").execute().actionGet();
    ensureGreen();
    List<IndexRequestBuilder> indexBuilders = new ArrayList<>();
    final int numDocs = between(10, 20);
    for (int i = 0; i < numDocs; i++) {
        indexBuilders.add(client().prepareIndex("test", "type", Integer.toString(i)).setSource(jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 11).field("lon", 21).endObject().endObject()));
    }
    indexRandom(true, indexBuilders.toArray(new IndexRequestBuilder[indexBuilders.size()]));
    assertHitCount(client().prepareSearch().get(), (numDocs));
    final int numIters = scaledRandomIntBetween(5, 20);
    for (int i = 0; i < numIters; i++) {
        final AtomicBoolean stop = new AtomicBoolean(false);
        final List<String> nonCriticalExceptions = new CopyOnWriteArrayList<>();
        Thread[] threads = new Thread[scaledRandomIntBetween(1, 3)];
        for (int j = 0; j < threads.length; j++) {
            threads[j] = new Thread() {

                @Override
                public void run() {
                    try {
                        while (!stop.get()) {
                            SearchResponse sr = client().prepareSearch().setSize(numDocs).get();
                            if (sr.getHits().getTotalHits() != numDocs) {
                                // request comes in. It's a small window but a known limitation.
                                if (sr.getTotalShards() != sr.getSuccessfulShards() && sr.getFailedShards() == 0) {
                                    nonCriticalExceptions.add("Count is " + sr.getHits().getTotalHits() + " but " + numDocs + " was expected. " + formatShardStatus(sr));
                                } else {
                                    assertHitCount(sr, numDocs);
                                }
                            }
                            final SearchHits sh = sr.getHits();
                            assertThat("Expected hits to be the same size the actual hits array", sh.getTotalHits(), equalTo((long) (sh.getHits().length)));
                        // this is the more critical but that we hit the actual hit array has a different size than the
                        // actual number of hits.
                        }
                    } catch (SearchPhaseExecutionException ex) {
                        // with replicas this should not happen
                        if (numberOfReplicas == 1 || !ex.getMessage().contains("all shards failed")) {
                            throw ex;
                        }
                    }
                }
            };
        }
        for (int j = 0; j < threads.length; j++) {
            threads[j].start();
        }
        allowNodes("test", between(1, 3));
        client().admin().cluster().prepareReroute().get();
        stop.set(true);
        for (int j = 0; j < threads.length; j++) {
            threads[j].join();
        }
        // this might time out on some machines if they are really busy and you hit lots of throttling
        ClusterHealthResponse resp = client().admin().cluster().prepareHealth().setWaitForYellowStatus().setWaitForNoRelocatingShards(true).setWaitForEvents(Priority.LANGUID).setTimeout("5m").get();
        assertNoTimeout(resp);
        // if we hit only non-critical exceptions we make sure that the post search works
        if (!nonCriticalExceptions.isEmpty()) {
            logger.info("non-critical exceptions: {}", nonCriticalExceptions);
            for (int j = 0; j < 10; j++) {
                assertHitCount(client().prepareSearch().get(), numDocs);
            }
        }
    }
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SearchHits(org.elasticsearch.search.SearchHits) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)95 SearchResponse (org.elasticsearch.action.search.SearchResponse)61 SearchHit (org.elasticsearch.search.SearchHit)52 ArrayList (java.util.ArrayList)24 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)16 IOException (java.io.IOException)15 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)13 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)13 ElasticsearchAssertions.assertSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits)13 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)10 TopDocs (org.apache.lucene.search.TopDocs)9 SearchHitField (org.elasticsearch.search.SearchHitField)9 QuerySearchResultProvider (org.elasticsearch.search.query.QuerySearchResultProvider)9 Text (org.elasticsearch.common.text.Text)7 ElasticsearchAssertions.assertOrderedSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits)7