Search in sources :

Example 61 with SearchHits

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

the class MoreExpressionTests method testMultiValueMethods.

public void testMultiValueMethods() throws Exception {
    ElasticsearchAssertions.assertAcked(prepareCreate("test").addMapping("doc", "double0", "type=double", "double1", "type=double", "double2", "type=double"));
    ensureGreen("test");
    Map<String, Object> doc1 = new HashMap<>();
    doc1.put("double0", new Double[] { 5.0d, 1.0d, 1.5d });
    doc1.put("double1", new Double[] { 1.2d, 2.4d });
    doc1.put("double2", 3.0d);
    Map<String, Object> doc2 = new HashMap<>();
    doc2.put("double0", 5.0d);
    doc2.put("double1", 3.0d);
    Map<String, Object> doc3 = new HashMap<>();
    doc3.put("double0", new Double[] { 5.0d, 1.0d, 1.5d, -1.5d });
    doc3.put("double1", 4.0d);
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource(doc1), client().prepareIndex("test", "doc", "2").setSource(doc2), client().prepareIndex("test", "doc", "3").setSource(doc3));
    SearchResponse rsp = buildRequest("doc['double0'].count() + doc['double1'].count()").get();
    assertSearchResponse(rsp);
    SearchHits hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(2.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    assertEquals(5.0, hits.getAt(2).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['double0'].sum()").get();
    assertSearchResponse(rsp);
    hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals(7.5, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(5.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    assertEquals(6.0, hits.getAt(2).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['double0'].avg() + doc['double1'].avg()").get();
    assertSearchResponse(rsp);
    hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals(4.3, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(8.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    assertEquals(5.5, hits.getAt(2).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['double0'].median()").get();
    assertSearchResponse(rsp);
    hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals(1.5, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(5.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    assertEquals(1.25, hits.getAt(2).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['double0'].min()").get();
    assertSearchResponse(rsp);
    hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals(1.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(5.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    assertEquals(-1.5, hits.getAt(2).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['double0'].max()").get();
    assertSearchResponse(rsp);
    hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(5.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    assertEquals(5.0, hits.getAt(2).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['double0'].sum()/doc['double0'].count()").get();
    assertSearchResponse(rsp);
    hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals(2.5, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(5.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    assertEquals(1.5, hits.getAt(2).field("foo").getValue(), 0.0D);
    // make sure count() works for missing
    rsp = buildRequest("doc['double2'].count()").get();
    assertSearchResponse(rsp);
    hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals(1.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(0.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    assertEquals(0.0, hits.getAt(2).field("foo").getValue(), 0.0D);
    // make sure .empty works in the same way
    rsp = buildRequest("doc['double2'].empty ? 5.0 : 2.0").get();
    assertSearchResponse(rsp);
    hits = rsp.getHits();
    assertEquals(3, hits.getTotalHits());
    assertEquals(2.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(5.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    assertEquals(5.0, hits.getAt(2).field("foo").getValue(), 0.0D);
}
Also used : HashMap(java.util.HashMap) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 62 with SearchHits

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

the class ExpandSearchPhaseTests method testFailOneItemFailsEntirePhase.

public void testFailOneItemFailsEntirePhase() throws IOException {
    AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
    SearchHits collapsedHits = new SearchHits(new SearchHit[] { new SearchHit(2, "ID", new Text("type"), Collections.emptyMap()), new SearchHit(3, "ID", new Text("type"), Collections.emptyMap()) }, 1, 1.0F);
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    String collapseValue = randomBoolean() ? null : "boom";
    mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz"))));
    mockSearchPhaseContext.searchTransport = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

        @Override
        void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
            assertTrue(executedMultiSearch.compareAndSet(false, true));
            InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits, null, null, null, false, null, 1);
            SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
            listener.onResponse(new MultiSearchResponse(new MultiSearchResponse.Item[] { new MultiSearchResponse.Item(null, new RuntimeException("boom")), new MultiSearchResponse.Item(response, null) }));
        }
    };
    SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(collapseValue)))), new SearchHit(2, "ID2", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(collapseValue)))) }, 1, 1.0F);
    InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
    SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
    AtomicReference<SearchResponse> reference = new AtomicReference<>();
    ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, response, r -> new SearchPhase("test") {

        @Override
        public void run() throws IOException {
            reference.set(r);
        }
    });
    phase.run();
    assertThat(mockSearchPhaseContext.phaseFailure.get(), Matchers.instanceOf(RuntimeException.class));
    assertEquals("boom", mockSearchPhaseContext.phaseFailure.get().getMessage());
    assertNotNull(mockSearchPhaseContext.phaseFailure.get());
    assertNull(reference.get());
    assertEquals(0, mockSearchPhaseContext.phasesExecuted.get());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchHits(org.elasticsearch.search.SearchHits) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) Text(org.elasticsearch.common.text.Text) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SearchHitField(org.elasticsearch.search.SearchHitField) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Example 63 with SearchHits

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

the class ExpandSearchPhaseTests method testSkipPhase.

public void testSkipPhase() throws IOException {
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    mockSearchPhaseContext.searchTransport = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

        @Override
        void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
            fail("no collapsing here");
        }
    };
    SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(null)))), new SearchHit(2, "ID2", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(null)))) }, 1, 1.0F);
    InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
    SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
    AtomicReference<SearchResponse> reference = new AtomicReference<>();
    ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, response, r -> new SearchPhase("test") {

        @Override
        public void run() throws IOException {
            reference.set(r);
        }
    });
    phase.run();
    mockSearchPhaseContext.assertNoFailure();
    assertNotNull(reference.get());
    assertEquals(1, mockSearchPhaseContext.phasesExecuted.get());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Text(org.elasticsearch.common.text.Text) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Example 64 with SearchHits

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

the class FetchSearchPhaseTests method testFetchTwoDocument.

public void testFetchTwoDocument() throws IOException {
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(2);
    SearchPhaseController controller = new SearchPhaseController(Settings.EMPTY, BigArrays.NON_RECYCLING_INSTANCE, null);
    InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> results = controller.newSearchPhaseResults(mockSearchPhaseContext.getRequest(), 2);
    AtomicReference<SearchResponse> responseRef = new AtomicReference<>();
    int resultSetSize = randomIntBetween(2, 10);
    QuerySearchResult queryResult = new QuerySearchResult(123, new SearchShardTarget("node1", new Index("test", "na"), 0));
    queryResult.topDocs(new TopDocs(1, new ScoreDoc[] { new ScoreDoc(42, 1.0F) }, 2.0F), new DocValueFormat[0]);
    // the size of the result set
    queryResult.size(resultSetSize);
    results.consumeResult(0, queryResult);
    queryResult = new QuerySearchResult(321, new SearchShardTarget("node2", new Index("test", "na"), 1));
    queryResult.topDocs(new TopDocs(1, new ScoreDoc[] { new ScoreDoc(84, 2.0F) }, 2.0F), new DocValueFormat[0]);
    queryResult.size(resultSetSize);
    results.consumeResult(1, queryResult);
    SearchTransportService searchTransportService = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

        @Override
        public void sendExecuteFetch(Transport.Connection connection, ShardFetchSearchRequest request, SearchTask task, ActionListener<FetchSearchResult> listener) {
            FetchSearchResult fetchResult = new FetchSearchResult();
            if (request.id() == 321) {
                fetchResult.hits(new SearchHits(new SearchHit[] { new SearchHit(84) }, 1, 2.0F));
            } else {
                assertEquals(123, request.id());
                fetchResult.hits(new SearchHits(new SearchHit[] { new SearchHit(42) }, 1, 1.0F));
            }
            listener.onResponse(fetchResult);
        }
    };
    mockSearchPhaseContext.searchTransport = searchTransportService;
    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(2, responseRef.get().getHits().totalHits);
    assertEquals(84, responseRef.get().getHits().getAt(0).docId());
    assertEquals(42, responseRef.get().getHits().getAt(1).docId());
    assertEquals(0, responseRef.get().getFailedShards());
    assertEquals(2, responseRef.get().getSuccessfulShards());
    assertTrue(mockSearchPhaseContext.releasedSearchContexts.isEmpty());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) QueryFetchSearchResult(org.elasticsearch.search.fetch.QueryFetchSearchResult) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) Index(org.elasticsearch.index.Index) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) ShardFetchSearchRequest(org.elasticsearch.search.fetch.ShardFetchSearchRequest) SearchHits(org.elasticsearch.search.SearchHits) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ActionListener(org.elasticsearch.action.ActionListener) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Example 65 with SearchHits

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

the class FetchSearchPhaseTests method testFailFetchOneDoc.

public void testFailFetchOneDoc() throws IOException {
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(2);
    SearchPhaseController controller = new SearchPhaseController(Settings.EMPTY, BigArrays.NON_RECYCLING_INSTANCE, null);
    InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> results = controller.newSearchPhaseResults(mockSearchPhaseContext.getRequest(), 2);
    AtomicReference<SearchResponse> responseRef = new AtomicReference<>();
    int resultSetSize = randomIntBetween(2, 10);
    QuerySearchResult queryResult = new QuerySearchResult(123, new SearchShardTarget("node1", new Index("test", "na"), 0));
    queryResult.topDocs(new TopDocs(1, new ScoreDoc[] { new ScoreDoc(42, 1.0F) }, 2.0F), new DocValueFormat[0]);
    // the size of the result set
    queryResult.size(resultSetSize);
    results.consumeResult(0, queryResult);
    queryResult = new QuerySearchResult(321, new SearchShardTarget("node2", new Index("test", "na"), 1));
    queryResult.topDocs(new TopDocs(1, new ScoreDoc[] { new ScoreDoc(84, 2.0F) }, 2.0F), new DocValueFormat[0]);
    queryResult.size(resultSetSize);
    results.consumeResult(1, queryResult);
    SearchTransportService searchTransportService = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

        @Override
        public void sendExecuteFetch(Transport.Connection connection, ShardFetchSearchRequest request, SearchTask task, ActionListener<FetchSearchResult> listener) {
            if (request.id() == 321) {
                FetchSearchResult fetchResult = new FetchSearchResult();
                fetchResult.hits(new SearchHits(new SearchHit[] { new SearchHit(84) }, 1, 2.0F));
                listener.onResponse(fetchResult);
            } else {
                listener.onFailure(new MockDirectoryWrapper.FakeIOException());
            }
        }
    };
    mockSearchPhaseContext.searchTransport = searchTransportService;
    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(2, responseRef.get().getHits().totalHits);
    assertEquals(84, responseRef.get().getHits().getAt(0).docId());
    assertEquals(1, responseRef.get().getFailedShards());
    assertEquals(1, responseRef.get().getSuccessfulShards());
    assertEquals(1, responseRef.get().getShardFailures().length);
    assertTrue(responseRef.get().getShardFailures()[0].getCause() instanceof MockDirectoryWrapper.FakeIOException);
    assertEquals(1, mockSearchPhaseContext.releasedSearchContexts.size());
    assertTrue(mockSearchPhaseContext.releasedSearchContexts.contains(123L));
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) QueryFetchSearchResult(org.elasticsearch.search.fetch.QueryFetchSearchResult) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) Index(org.elasticsearch.index.Index) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) ShardFetchSearchRequest(org.elasticsearch.search.fetch.ShardFetchSearchRequest) SearchHits(org.elasticsearch.search.SearchHits) MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ActionListener(org.elasticsearch.action.ActionListener) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)92 SearchResponse (org.elasticsearch.action.search.SearchResponse)60 SearchHit (org.elasticsearch.search.SearchHit)49 ArrayList (java.util.ArrayList)22 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)15 IOException (java.io.IOException)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)13 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)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