Search in sources :

Example 1 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class RestCountAction method doCatRequest.

@Override
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    SearchRequest countRequest = new SearchRequest(indices);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0);
    countRequest.source(searchSourceBuilder);
    try {
        request.withContentOrSourceParamParserOrNull(parser -> {
            if (parser == null) {
                QueryBuilder queryBuilder = RestActions.urlParamsToQueryBuilder(request);
                if (queryBuilder != null) {
                    searchSourceBuilder.query(queryBuilder);
                }
            } else {
                searchSourceBuilder.query(RestActions.getQueryContent(parser));
            }
        });
    } catch (IOException e) {
        throw new ElasticsearchException("Couldn't parse query", e);
    }
    return channel -> client.search(countRequest, new RestResponseListener<SearchResponse>(channel) {

        @Override
        public RestResponse buildResponse(SearchResponse countResponse) throws Exception {
            return RestTable.buildResponse(buildTable(request, countResponse), channel);
        }
    });
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) Table(org.elasticsearch.common.Table) IOException(java.io.IOException) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) Settings(org.elasticsearch.common.settings.Settings) SearchResponse(org.elasticsearch.action.search.SearchResponse) RestRequest(org.elasticsearch.rest.RestRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) NodeClient(org.elasticsearch.client.node.NodeClient) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) SearchRequest(org.elasticsearch.action.search.SearchRequest) RestResponse(org.elasticsearch.rest.RestResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class SearchPhaseControllerTests method testConsumerConcurrently.

public void testConsumerConcurrently() throws InterruptedException {
    int expectedNumResults = randomIntBetween(1, 100);
    int bufferSize = randomIntBetween(2, 200);
    SearchRequest request = new SearchRequest();
    request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.avg("foo")));
    request.setBatchedReduceSize(bufferSize);
    InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> consumer = searchPhaseController.newSearchPhaseResults(request, expectedNumResults);
    AtomicInteger max = new AtomicInteger();
    CountDownLatch latch = new CountDownLatch(expectedNumResults);
    for (int i = 0; i < expectedNumResults; i++) {
        int id = i;
        Thread t = new Thread(() -> {
            int number = randomIntBetween(1, 1000);
            max.updateAndGet(prev -> Math.max(prev, number));
            QuerySearchResult result = new QuerySearchResult(id, new SearchShardTarget("node", new Index("a", "b"), id));
            result.topDocs(new TopDocs(id, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
            InternalAggregations aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", (double) number, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
            result.aggregations(aggs);
            consumer.consumeResult(id, result);
            latch.countDown();
        });
        t.start();
    }
    latch.await();
    SearchPhaseController.ReducedQueryPhase reduce = consumer.reduce();
    InternalMax internalMax = (InternalMax) reduce.aggregations.asList().get(0);
    assertEquals(max.get(), internalMax.getValue(), 0.0D);
}
Also used : QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) InternalMax(org.elasticsearch.search.aggregations.metrics.max.InternalMax) Index(org.elasticsearch.index.Index) CountDownLatch(java.util.concurrent.CountDownLatch) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Example 3 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class ExpandSearchPhaseTests method testCollapseSingleHit.

public void testCollapseSingleHit() throws IOException {
    final int iters = randomIntBetween(5, 10);
    for (int i = 0; i < iters; i++) {
        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);
        AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
        QueryBuilder originalQuery = randomBoolean() ? null : QueryBuilders.termQuery("foo", "bar");
        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.getRequest().source().query(originalQuery);
        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));
                assertEquals(1, request.requests().size());
                SearchRequest searchRequest = request.requests().get(0);
                assertTrue(searchRequest.source().query() instanceof BoolQueryBuilder);
                BoolQueryBuilder groupBuilder = (BoolQueryBuilder) searchRequest.source().query();
                if (collapseValue == null) {
                    assertThat(groupBuilder.mustNot(), Matchers.contains(QueryBuilders.existsQuery("someField")));
                } else {
                    assertThat(groupBuilder.filter(), Matchers.contains(QueryBuilders.matchQuery("someField", "boom")));
                }
                if (originalQuery != null) {
                    assertThat(groupBuilder.must(), Matchers.contains(QueryBuilders.termQuery("foo", "bar")));
                }
                assertArrayEquals(mockSearchPhaseContext.getRequest().indices(), searchRequest.indices());
                assertArrayEquals(mockSearchPhaseContext.getRequest().types(), searchRequest.types());
                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(response, null) }));
            }
        };
        SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", 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();
        mockSearchPhaseContext.assertNoFailure();
        assertNotNull(reference.get());
        SearchResponse theResponse = reference.get();
        assertSame(theResponse, response);
        assertEquals(1, theResponse.getHits().getHits()[0].getInnerHits().size());
        assertSame(theResponse.getHits().getHits()[0].getInnerHits().get("foobarbaz"), collapsedHits);
        assertTrue(executedMultiSearch.get());
        assertEquals(1, mockSearchPhaseContext.phasesExecuted.get());
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) 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 4 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class BulkByScrollParallelizationHelper method sliceIntoSubRequests.

/**
     * Slice a search request into {@code times} separate search requests slicing on {@code field}. Note that the slices are *shallow*
     * copies of this request so don't change them.
     */
static SearchRequest[] sliceIntoSubRequests(SearchRequest request, String field, int times) {
    SearchRequest[] slices = new SearchRequest[times];
    for (int slice = 0; slice < times; slice++) {
        SliceBuilder sliceBuilder = new SliceBuilder(field, slice, times);
        SearchSourceBuilder slicedSource;
        if (request.source() == null) {
            slicedSource = new SearchSourceBuilder().slice(sliceBuilder);
        } else {
            if (request.source().slice() != null) {
                throw new IllegalStateException("Can't slice a request that already has a slice configuration");
            }
            slicedSource = request.source().copyWithNewSlice(sliceBuilder);
        }
        slices[slice] = new SearchRequest().source(slicedSource).searchType(request.searchType()).indices(request.indices()).types(request.types()).routing(request.routing()).preference(request.preference()).requestCache(request.requestCache()).scroll(request.scroll()).indicesOptions(request.indicesOptions());
    }
    return slices;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SliceBuilder(org.elasticsearch.search.slice.SliceBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 5 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class RestSearchAction method parseSearchRequest.

/**
     * Parses the rest request on top of the SearchRequest, preserving values that are not overridden by the rest request.
     *
     * @param requestContentParser body of the request to read. This method does not attempt to read the body from the {@code request}
     *        parameter
     */
public static void parseSearchRequest(SearchRequest searchRequest, RestRequest request, XContentParser requestContentParser) throws IOException {
    if (searchRequest.source() == null) {
        searchRequest.source(new SearchSourceBuilder());
    }
    searchRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
    if (requestContentParser != null) {
        QueryParseContext context = new QueryParseContext(requestContentParser);
        searchRequest.source().parseXContent(context);
    }
    final int batchedReduceSize = request.paramAsInt("batched_reduce_size", searchRequest.getBatchedReduceSize());
    searchRequest.setBatchedReduceSize(batchedReduceSize);
    // do not allow 'query_and_fetch' or 'dfs_query_and_fetch' search types
    // from the REST layer. these modes are an internal optimization and should
    // not be specified explicitly by the user.
    String searchType = request.param("search_type");
    if ("query_and_fetch".equals(searchType) || "dfs_query_and_fetch".equals(searchType)) {
        throw new IllegalArgumentException("Unsupported search type [" + searchType + "]");
    } else {
        searchRequest.searchType(searchType);
    }
    parseSearchSource(searchRequest.source(), request);
    searchRequest.requestCache(request.paramAsBoolean("request_cache", null));
    String scroll = request.param("scroll");
    if (scroll != null) {
        searchRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
    }
    searchRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    searchRequest.routing(request.param("routing"));
    searchRequest.preference(request.param("preference"));
    searchRequest.indicesOptions(IndicesOptions.fromRequest(request, searchRequest.indicesOptions()));
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) Scroll(org.elasticsearch.search.Scroll) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Aggregations

SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)197 SearchResponse (org.elasticsearch.action.search.SearchResponse)103 SearchRequest (org.elasticsearch.action.search.SearchRequest)80 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)41 IOException (java.io.IOException)37 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)34 Search (io.searchbox.core.Search)32 Matchers.containsString (org.hamcrest.Matchers.containsString)32 Test (org.junit.Test)30 List (java.util.List)26 Map (java.util.Map)25 SearchSourceBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder)24 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)24 ArrayList (java.util.ArrayList)20 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)20 Set (java.util.Set)19 Collectors (java.util.stream.Collectors)18 SearchHit (org.elasticsearch.search.SearchHit)18 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)17