Search in sources :

Example 41 with SearchRequest

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

the class Searches method scroll.

public ScrollResult scroll(String query, TimeRange range, int limit, int offset, List<String> fields, String filter) {
    final Set<String> indices = determineAffectedIndices(range, filter);
    // only request the fields we asked for otherwise we can't figure out which fields will be in the result set
    // until we've scrolled through the entire set.
    // TODO: Check if we can get away without loading the _source field.
    // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-fields.html#search-request-fields
    // "For backwards compatibility, if the fields parameter specifies fields which are not stored , it will
    // load the _source and extract it from it. This functionality has been replaced by the source filtering
    // parameter." -- So we should look at the source filtering parameter once we switched to ES 1.x.
    final SearchRequest request = standardSearchRequest(query, indices, limit, offset, range, filter, null, false).setScroll(new TimeValue(1, TimeUnit.MINUTES)).setSize(// TODO magic numbers
    500).addSort(SortBuilders.fieldSort(SortParseElement.DOC_FIELD_NAME)).addFields(fields.toArray(new String[fields.size()])).addField(// always request the _source field because otherwise we can't access non-stored values
    "_source").request();
    if (LOG.isDebugEnabled()) {
        try {
            LOG.debug("ElasticSearch scroll query: {}", XContentHelper.convertToJson(request.source(), false));
        } catch (IOException ignored) {
        }
    }
    final SearchResponse r = c.search(request).actionGet();
    recordEsMetrics(r, range);
    return new ScrollResult(c, query, request.source(), r, fields);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ScrollResult(org.graylog2.indexer.results.ScrollResult) IOException(java.io.IOException) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 42 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project metron by apache.

the class ElasticsearchRequestSubmitterTest method searchShouldHandleShardFailure.

@Test
public void searchShouldHandleShardFailure() throws InvalidSearchException {
    // mocks
    SearchResponse response = mock(SearchResponse.class);
    SearchRequest request = new SearchRequest();
    ShardSearchFailure fail = mock(ShardSearchFailure.class);
    SearchShardTarget target = new SearchShardTarget("node1", mock(Index.class), 1, "metron");
    // response will have status of OK
    when(response.status()).thenReturn(RestStatus.OK);
    // the response will report shard failures
    when(response.getFailedShards()).thenReturn(1);
    when(response.getTotalShards()).thenReturn(2);
    // the response will return the failures
    ShardSearchFailure[] failures = { fail };
    when(response.getShardFailures()).thenReturn(failures);
    // shard failure needs to report the node
    when(fail.shard()).thenReturn(target);
    // shard failure needs to report details of failure
    when(fail.index()).thenReturn("bro_index_2017-10-11");
    when(fail.shardId()).thenReturn(1);
    // search should succeed, even with failed shards
    ElasticsearchRequestSubmitter submitter = setup(response);
    SearchResponse actual = submitter.submitSearch(request);
    assertNotNull(actual);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Index(org.elasticsearch.index.Index) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 43 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class AbstractBulkByScrollRequest method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    searchRequest = new SearchRequest();
    searchRequest.readFrom(in);
    abortOnVersionConflict = in.readBoolean();
    size = in.readVInt();
    refresh = in.readBoolean();
    timeout = new TimeValue(in);
    activeShardCount = ActiveShardCount.readFrom(in);
    retryBackoffInitialTime = new TimeValue(in);
    maxRetries = in.readVInt();
    requestsPerSecond = in.readFloat();
    if (in.getVersion().onOrAfter(Version.V_5_1_1_UNRELEASED)) {
        slices = in.readVInt();
    } else {
        slices = 1;
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 44 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class BulkByScrollParallelizationHelper method startSlices.

public static <Request extends AbstractBulkByScrollRequest<Request>> void startSlices(Client client, TaskManager taskManager, Action<Request, BulkByScrollResponse, ?> action, String localNodeId, ParentBulkByScrollTask task, Request request, ActionListener<BulkByScrollResponse> listener) {
    TaskId parentTaskId = new TaskId(localNodeId, task.getId());
    for (final SearchRequest slice : sliceIntoSubRequests(request.getSearchRequest(), UidFieldMapper.NAME, request.getSlices())) {
        // TODO move the request to the correct node. maybe here or somehow do it as part of startup for reindex in general....
        Request requestForSlice = request.forSlice(parentTaskId, slice);
        ActionListener<BulkByScrollResponse> sliceListener = ActionListener.wrap(r -> task.onSliceResponse(listener, slice.source().slice().getId(), r), e -> task.onSliceFailure(listener, slice.source().slice().getId(), e));
        client.execute(action, requestForSlice, sliceListener);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TaskId(org.elasticsearch.tasks.TaskId) SearchRequest(org.elasticsearch.action.search.SearchRequest)

Example 45 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class AsyncBulkByScrollActionTests method setupForTest.

@Before
public void setupForTest() {
    // Fill the context with something random so we can make sure we inherited it appropriately.
    expectedHeaders.clear();
    expectedHeaders.put(randomSimpleString(random()), randomSimpleString(random()));
    setupClient(new TestThreadPool(getTestName()));
    firstSearchRequest = new SearchRequest();
    testRequest = new DummyAbstractBulkByScrollRequest(firstSearchRequest);
    listener = new PlainActionFuture<>();
    scrollId = null;
    taskManager = new TaskManager(Settings.EMPTY);
    testTask = (WorkingBulkByScrollTask) taskManager.register("don'tcare", "hereeither", testRequest);
    localNode = new DiscoveryNode("thenode", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    taskId = new TaskId(localNode.getId(), testTask.getId());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TaskManager(org.elasticsearch.tasks.TaskManager) TaskId(org.elasticsearch.tasks.TaskId) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Before(org.junit.Before)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)69 SearchResponse (org.elasticsearch.action.search.SearchResponse)21 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)8 Test (org.junit.Test)7 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)6 BytesArray (org.elasticsearch.common.bytes.BytesArray)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 XContentParser (org.elasticsearch.common.xcontent.XContentParser)6 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)6 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)6 ArrayList (java.util.ArrayList)5 TestUtil.randomSimpleString (org.apache.lucene.util.TestUtil.randomSimpleString)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)5 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 List (java.util.List)4