Search in sources :

Example 21 with SearchContext

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

the class SearchService method executeFetchPhase.

public ScrollQueryFetchSearchResult executeFetchPhase(InternalScrollSearchRequest request, SearchTask task) {
    final SearchContext context = findContext(request.id());
    context.incRef();
    try {
        context.setTask(task);
        contextProcessing(context);
        SearchOperationListener operationListener = context.indexShard().getSearchOperationListener();
        processScroll(request, context);
        operationListener.onPreQueryPhase(context);
        final long time = System.nanoTime();
        try {
            queryPhase.execute(context);
        } catch (Exception e) {
            operationListener.onFailedQueryPhase(context);
            throw ExceptionsHelper.convertToRuntime(e);
        }
        long afterQueryTime = System.nanoTime();
        operationListener.onQueryPhase(context, afterQueryTime - time);
        QueryFetchSearchResult fetchSearchResult = executeFetchPhase(context, operationListener, afterQueryTime);
        return new ScrollQueryFetchSearchResult(fetchSearchResult, context.shardTarget());
    } catch (Exception e) {
        logger.trace("Fetch phase failed", e);
        processFailure(context, e);
        throw ExceptionsHelper.convertToRuntime(e);
    } finally {
        cleanContext(context);
    }
}
Also used : ScrollQueryFetchSearchResult(org.elasticsearch.search.fetch.ScrollQueryFetchSearchResult) QueryFetchSearchResult(org.elasticsearch.search.fetch.QueryFetchSearchResult) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchOperationListener(org.elasticsearch.index.shard.SearchOperationListener) ElasticsearchException(org.elasticsearch.ElasticsearchException) AggregationInitializationException(org.elasticsearch.search.aggregations.AggregationInitializationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ScrollQueryFetchSearchResult(org.elasticsearch.search.fetch.ScrollQueryFetchSearchResult)

Example 22 with SearchContext

use of org.elasticsearch.search.internal.SearchContext 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 23 with SearchContext

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

the class SearchOperationListenerTests method testListenersAreExecuted.

// this test also tests if calls are correct if one or more listeners throw exceptions
public void testListenersAreExecuted() {
    AtomicInteger preQuery = new AtomicInteger();
    AtomicInteger failedQuery = new AtomicInteger();
    AtomicInteger onQuery = new AtomicInteger();
    AtomicInteger onFetch = new AtomicInteger();
    AtomicInteger preFetch = new AtomicInteger();
    AtomicInteger failedFetch = new AtomicInteger();
    AtomicInteger newContext = new AtomicInteger();
    AtomicInteger freeContext = new AtomicInteger();
    AtomicInteger newScrollContext = new AtomicInteger();
    AtomicInteger freeScrollContext = new AtomicInteger();
    AtomicInteger timeInNanos = new AtomicInteger(randomIntBetween(0, 10));
    SearchOperationListener listener = new SearchOperationListener() {

        @Override
        public void onPreQueryPhase(SearchContext searchContext) {
            assertNotNull(searchContext);
            preQuery.incrementAndGet();
        }

        @Override
        public void onFailedQueryPhase(SearchContext searchContext) {
            assertNotNull(searchContext);
            failedQuery.incrementAndGet();
        }

        @Override
        public void onQueryPhase(SearchContext searchContext, long tookInNanos) {
            assertEquals(timeInNanos.get(), tookInNanos);
            assertNotNull(searchContext);
            onQuery.incrementAndGet();
        }

        @Override
        public void onPreFetchPhase(SearchContext searchContext) {
            assertNotNull(searchContext);
            preFetch.incrementAndGet();
        }

        @Override
        public void onFailedFetchPhase(SearchContext searchContext) {
            assertNotNull(searchContext);
            failedFetch.incrementAndGet();
        }

        @Override
        public void onFetchPhase(SearchContext searchContext, long tookInNanos) {
            assertEquals(timeInNanos.get(), tookInNanos);
            onFetch.incrementAndGet();
        }

        @Override
        public void onNewContext(SearchContext context) {
            assertNotNull(context);
            newContext.incrementAndGet();
        }

        @Override
        public void onFreeContext(SearchContext context) {
            assertNotNull(context);
            freeContext.incrementAndGet();
        }

        @Override
        public void onNewScrollContext(SearchContext context) {
            assertNotNull(context);
            newScrollContext.incrementAndGet();
        }

        @Override
        public void onFreeScrollContext(SearchContext context) {
            assertNotNull(context);
            freeScrollContext.incrementAndGet();
        }
    };
    SearchOperationListener throwingListener = (SearchOperationListener) Proxy.newProxyInstance(SearchOperationListener.class.getClassLoader(), new Class[] { SearchOperationListener.class }, (a, b, c) -> {
        throw new RuntimeException();
    });
    final List<SearchOperationListener> indexingOperationListeners = new ArrayList<>(Arrays.asList(listener, listener));
    if (randomBoolean()) {
        indexingOperationListeners.add(throwingListener);
        if (randomBoolean()) {
            indexingOperationListeners.add(throwingListener);
        }
    }
    Collections.shuffle(indexingOperationListeners, random());
    SearchOperationListener.CompositeListener compositeListener = new SearchOperationListener.CompositeListener(indexingOperationListeners, logger);
    SearchContext ctx = new TestSearchContext(null);
    compositeListener.onQueryPhase(ctx, timeInNanos.get());
    assertEquals(0, preFetch.get());
    assertEquals(0, preQuery.get());
    assertEquals(0, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(0, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFetchPhase(ctx, timeInNanos.get());
    assertEquals(0, preFetch.get());
    assertEquals(0, preQuery.get());
    assertEquals(0, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onPreQueryPhase(ctx);
    assertEquals(0, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(0, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onPreFetchPhase(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(0, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFailedFetchPhase(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFailedQueryPhase(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onNewContext(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(2, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onNewScrollContext(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(2, newContext.get());
    assertEquals(2, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFreeContext(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(2, newContext.get());
    assertEquals(2, newScrollContext.get());
    assertEquals(2, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFreeScrollContext(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(2, newContext.get());
    assertEquals(2, newScrollContext.get());
    assertEquals(2, freeContext.get());
    assertEquals(2, freeScrollContext.get());
}
Also used : Arrays(java.util.Arrays) List(java.util.List) Proxy(java.lang.reflect.Proxy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Client(org.elasticsearch.client.Client) Term(org.apache.lucene.index.Term) SearchContext(org.elasticsearch.search.internal.SearchContext) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Engine(org.elasticsearch.index.engine.Engine) TestSearchContext(org.elasticsearch.test.TestSearchContext) ArrayList(java.util.ArrayList) TestSearchContext(org.elasticsearch.test.TestSearchContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) SearchContext(org.elasticsearch.search.internal.SearchContext) TestSearchContext(org.elasticsearch.test.TestSearchContext)

Example 24 with SearchContext

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

the class ExtendedBoundsTests method testParseAndValidate.

public void testParseAndValidate() {
    long now = randomLong();
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build();
    SearchContext context = mock(SearchContext.class);
    QueryShardContext qsc = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> now);
    when(context.getQueryShardContext()).thenReturn(qsc);
    FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime");
    DocValueFormat format = new DocValueFormat.DateTime(formatter, DateTimeZone.UTC);
    ExtendedBounds expected = randomParsedExtendedBounds();
    ExtendedBounds parsed = unparsed(expected).parseAndValidate("test", context, format);
    // parsed won't *equal* expected because equal includes the String parts
    assertEquals(expected.getMin(), parsed.getMin());
    assertEquals(expected.getMax(), parsed.getMax());
    parsed = new ExtendedBounds("now", null).parseAndValidate("test", context, format);
    assertEquals(now, (long) parsed.getMin());
    assertNull(parsed.getMax());
    parsed = new ExtendedBounds(null, "now").parseAndValidate("test", context, format);
    assertNull(parsed.getMin());
    assertEquals(now, (long) parsed.getMax());
    SearchParseException e = expectThrows(SearchParseException.class, () -> new ExtendedBounds(100L, 90L).parseAndValidate("test", context, format));
    assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
    e = expectThrows(SearchParseException.class, () -> unparsed(new ExtendedBounds(100L, 90L)).parseAndValidate("test", context, format));
    assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
}
Also used : SearchParseException(org.elasticsearch.search.SearchParseException) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DocValueFormat(org.elasticsearch.search.DocValueFormat) IndexSettings(org.elasticsearch.index.IndexSettings) SearchContext(org.elasticsearch.search.internal.SearchContext) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 25 with SearchContext

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

the class PercolatorHighlightSubFetchPhaseTests method testHitsExecutionNeeded.

public void testHitsExecutionNeeded() {
    PercolateQuery percolateQuery = new PercolateQuery("", ctx -> null, new BytesArray("{}"), new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), new MatchAllDocsQuery());
    PercolatorHighlightSubFetchPhase subFetchPhase = new PercolatorHighlightSubFetchPhase(Settings.EMPTY, emptyMap());
    SearchContext searchContext = Mockito.mock(SearchContext.class);
    Mockito.when(searchContext.highlight()).thenReturn(new SearchContextHighlight(Collections.emptyList()));
    Mockito.when(searchContext.query()).thenReturn(new MatchAllDocsQuery());
    assertThat(subFetchPhase.hitsExecutionNeeded(searchContext), is(false));
    Mockito.when(searchContext.query()).thenReturn(percolateQuery);
    assertThat(subFetchPhase.hitsExecutionNeeded(searchContext), is(true));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BytesArray(org.elasticsearch.common.bytes.BytesArray) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchContextHighlight(org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

SearchContext (org.elasticsearch.search.internal.SearchContext)33 IOException (java.io.IOException)11 ElasticsearchException (org.elasticsearch.ElasticsearchException)7 ExecutionException (java.util.concurrent.ExecutionException)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 SearchOperationListener (org.elasticsearch.index.shard.SearchOperationListener)6 AggregationInitializationException (org.elasticsearch.search.aggregations.AggregationInitializationException)6 TestSearchContext (org.elasticsearch.test.TestSearchContext)6 ArrayList (java.util.ArrayList)5 FetchSubPhase (org.elasticsearch.search.fetch.FetchSubPhase)5 Term (org.apache.lucene.index.Term)4 Encoder (org.apache.lucene.search.highlight.Encoder)4 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)4 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)4 FetchPhaseExecutionException (org.elasticsearch.search.fetch.FetchPhaseExecutionException)4 Analyzer (org.apache.lucene.analysis.Analyzer)3 TermQuery (org.apache.lucene.search.TermQuery)3 InnerHitsContext (org.elasticsearch.search.fetch.subphase.InnerHitsContext)3 Document (org.apache.lucene.document.Document)2 IndexReader (org.apache.lucene.index.IndexReader)2