Search in sources :

Example 81 with SearchHits

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class MoreExpressionTests method testDateMethods.

public void testDateMethods() throws Exception {
    ElasticsearchAssertions.assertAcked(prepareCreate("test").addMapping("doc", "date0", "type=date", "date1", "type=date"));
    ensureGreen("test");
    indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("date0", "2015-04-28T04:02:07Z", "date1", "1985-09-01T23:11:01Z"), client().prepareIndex("test", "doc", "2").setSource("date0", "2013-12-25T11:56:45Z", "date1", "1983-10-13T23:15:00Z"));
    SearchResponse rsp = buildRequest("doc['date0'].getSeconds() - doc['date0'].getMinutes()").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    SearchHits hits = rsp.getHits();
    assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(-11.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['date0'].getHourOfDay() + doc['date1'].getDayOfMonth()").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    hits = rsp.getHits();
    assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(24.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['date1'].getMonth() + 1").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    hits = rsp.getHits();
    assertEquals(9.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(10.0, hits.getAt(1).field("foo").getValue(), 0.0D);
    rsp = buildRequest("doc['date1'].getYear()").get();
    assertEquals(2, rsp.getHits().getTotalHits());
    hits = rsp.getHits();
    assertEquals(1985.0, hits.getAt(0).field("foo").getValue(), 0.0D);
    assertEquals(1983.0, hits.getAt(1).field("foo").getValue(), 0.0D);
}
Also used : SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 82 with SearchHits

use of org.graylog.shaded.elasticsearch7.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 83 with SearchHits

use of org.graylog.shaded.elasticsearch7.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 84 with SearchHits

use of org.graylog.shaded.elasticsearch7.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 85 with SearchHits

use of org.graylog.shaded.elasticsearch7.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)

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)93 SearchHit (org.elasticsearch.search.SearchHit)86 ArrayList (java.util.ArrayList)45 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)27 IOException (java.io.IOException)25 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 SearchRequest (org.elasticsearch.action.search.SearchRequest)16 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)16 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)15 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)14 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