Search in sources :

Example 1 with ShardSearchLocalRequest

use of org.elasticsearch.search.internal.ShardSearchLocalRequest in project elasticsearch by elastic.

the class SearchServiceTests method testSearchWhileIndexDeleted.

public void testSearchWhileIndexDeleted() throws IOException, InterruptedException {
    createIndex("index");
    client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).get();
    SearchService service = getInstanceFromNode(SearchService.class);
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    IndexShard indexShard = indexService.getShard(0);
    AtomicBoolean running = new AtomicBoolean(true);
    CountDownLatch startGun = new CountDownLatch(1);
    Semaphore semaphore = new Semaphore(Integer.MAX_VALUE);
    final Thread thread = new Thread() {

        @Override
        public void run() {
            startGun.countDown();
            while (running.get()) {
                service.afterIndexRemoved(indexService.index(), indexService.getIndexSettings(), DELETED);
                if (randomBoolean()) {
                    // context in a non-sane way.
                    try {
                        semaphore.acquire();
                    } catch (InterruptedException e) {
                        throw new AssertionError(e);
                    }
                    client().prepareIndex("index", "type").setSource("field", "value").setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())).execute(new ActionListener<IndexResponse>() {

                        @Override
                        public void onResponse(IndexResponse indexResponse) {
                            semaphore.release();
                        }

                        @Override
                        public void onFailure(Exception e) {
                            semaphore.release();
                        }
                    });
                }
            }
        }
    };
    thread.start();
    startGun.await();
    try {
        final int rounds = scaledRandomIntBetween(100, 10000);
        for (int i = 0; i < rounds; i++) {
            try {
                QuerySearchResultProvider querySearchResultProvider = service.executeQueryPhase(new ShardSearchLocalRequest(indexShard.shardId(), 1, SearchType.DEFAULT, new SearchSourceBuilder(), new String[0], false, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f), new SearchTask(123L, "", "", "", null));
                IntArrayList intCursors = new IntArrayList(1);
                intCursors.add(0);
                ShardFetchRequest req = new ShardFetchRequest(querySearchResultProvider.id(), intCursors, null);
                service.executeFetchPhase(req, new SearchTask(123L, "", "", "", null));
            } catch (AlreadyClosedException ex) {
                throw ex;
            } catch (IllegalStateException ex) {
                assertEquals("search context is already closed can't increment refCount current count [0]", ex.getMessage());
            } catch (SearchContextMissingException ex) {
            // that's fine
            }
        }
    } finally {
        running.set(false);
        thread.join();
        semaphore.acquire(Integer.MAX_VALUE);
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) ShardSearchLocalRequest(org.elasticsearch.search.internal.ShardSearchLocalRequest) IndexService(org.elasticsearch.index.IndexService) Semaphore(java.util.concurrent.Semaphore) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchTask(org.elasticsearch.action.search.SearchTask) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) ShardFetchRequest(org.elasticsearch.search.fetch.ShardFetchRequest) CountDownLatch(java.util.concurrent.CountDownLatch) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexResponse(org.elasticsearch.action.index.IndexResponse) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 2 with ShardSearchLocalRequest

use of org.elasticsearch.search.internal.ShardSearchLocalRequest in project elasticsearch by elastic.

the class TransportExplainAction method shardOperation.

@Override
protected ExplainResponse shardOperation(ExplainRequest request, ShardId shardId) throws IOException {
    ShardSearchLocalRequest shardSearchLocalRequest = new ShardSearchLocalRequest(shardId, new String[] { request.type() }, request.nowInMillis, request.filteringAlias());
    SearchContext context = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT, null);
    Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id()));
    Engine.GetResult result = null;
    try {
        result = context.indexShard().get(new Engine.Get(false, uidTerm));
        if (!result.exists()) {
            return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), false);
        }
        context.parsedQuery(context.getQueryShardContext().toQuery(request.query()));
        context.preProcess(true);
        int topLevelDocId = result.docIdAndVersion().docId + result.docIdAndVersion().context.docBase;
        Explanation explanation = context.searcher().explain(context.query(), topLevelDocId);
        for (RescoreSearchContext ctx : context.rescore()) {
            Rescorer rescorer = ctx.rescorer();
            explanation = rescorer.explain(topLevelDocId, context, ctx, explanation);
        }
        if (request.storedFields() != null || (request.fetchSourceContext() != null && request.fetchSourceContext().fetchSource())) {
            // Advantage is that we're not opening a second searcher to retrieve the _source. Also
            // because we are working in the same searcher in engineGetResult we can be sure that a
            // doc isn't deleted between the initial get and this call.
            GetResult getResult = context.indexShard().getService().get(result, request.id(), request.type(), request.storedFields(), request.fetchSourceContext());
            return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), true, explanation, getResult);
        } else {
            return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), true, explanation);
        }
    } catch (IOException e) {
        throw new ElasticsearchException("Could not explain", e);
    } finally {
        Releasables.close(result, context);
    }
}
Also used : ShardSearchLocalRequest(org.elasticsearch.search.internal.ShardSearchLocalRequest) RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) GetResult(org.elasticsearch.index.get.GetResult) Explanation(org.apache.lucene.search.Explanation) SearchContext(org.elasticsearch.search.internal.SearchContext) RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) Rescorer(org.elasticsearch.search.rescore.Rescorer) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) Engine(org.elasticsearch.index.engine.Engine)

Example 3 with ShardSearchLocalRequest

use of org.elasticsearch.search.internal.ShardSearchLocalRequest in project elasticsearch by elastic.

the class TransportValidateQueryAction method shardOperation.

@Override
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request) throws IOException {
    boolean valid;
    String explanation = null;
    String error = null;
    ShardSearchLocalRequest shardSearchLocalRequest = new ShardSearchLocalRequest(request.shardId(), request.types(), request.nowInMillis(), request.filteringAliases());
    SearchContext searchContext = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT, null);
    try {
        ParsedQuery parsedQuery = searchContext.getQueryShardContext().toQuery(request.query());
        searchContext.parsedQuery(parsedQuery);
        searchContext.preProcess(request.rewrite());
        valid = true;
        explanation = explain(searchContext, request.rewrite());
    } catch (QueryShardException | ParsingException e) {
        valid = false;
        error = e.getDetailedMessage();
    } catch (AssertionError | IOException e) {
        valid = false;
        error = e.getMessage();
    } finally {
        Releasables.close(searchContext);
    }
    return new ShardValidateQueryResponse(request.shardId(), valid, explanation, error);
}
Also used : ShardSearchLocalRequest(org.elasticsearch.search.internal.ShardSearchLocalRequest) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) ParsingException(org.elasticsearch.common.ParsingException) SearchContext(org.elasticsearch.search.internal.SearchContext) QueryShardException(org.elasticsearch.index.query.QueryShardException) IOException(java.io.IOException)

Example 4 with ShardSearchLocalRequest

use of org.elasticsearch.search.internal.ShardSearchLocalRequest in project elasticsearch by elastic.

the class SearchServiceTests method testTimeout.

public void testTimeout() throws IOException {
    createIndex("index");
    final SearchService service = getInstanceFromNode(SearchService.class);
    final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    final IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    final IndexShard indexShard = indexService.getShard(0);
    final SearchContext contextWithDefaultTimeout = service.createContext(new ShardSearchLocalRequest(indexShard.shardId(), 1, SearchType.DEFAULT, new SearchSourceBuilder(), new String[0], false, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f), null);
    // the search context should inherit the default timeout
    assertThat(contextWithDefaultTimeout.timeout(), equalTo(TimeValue.timeValueSeconds(5)));
    final long seconds = randomIntBetween(6, 10);
    final SearchContext context = service.createContext(new ShardSearchLocalRequest(indexShard.shardId(), 1, SearchType.DEFAULT, new SearchSourceBuilder().timeout(TimeValue.timeValueSeconds(seconds)), new String[0], false, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f), null);
    // the search context should inherit the query timeout
    assertThat(context.timeout(), equalTo(TimeValue.timeValueSeconds(seconds)));
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) ShardSearchLocalRequest(org.elasticsearch.search.internal.ShardSearchLocalRequest) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Aggregations

ShardSearchLocalRequest (org.elasticsearch.search.internal.ShardSearchLocalRequest)4 IOException (java.io.IOException)3 SearchContext (org.elasticsearch.search.internal.SearchContext)3 IndexService (org.elasticsearch.index.IndexService)2 IndexShard (org.elasticsearch.index.shard.IndexShard)2 IndicesService (org.elasticsearch.indices.IndicesService)2 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)2 AliasFilter (org.elasticsearch.search.internal.AliasFilter)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Term (org.apache.lucene.index.Term)1 Explanation (org.apache.lucene.search.Explanation)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 SearchTask (org.elasticsearch.action.search.SearchTask)1