Search in sources :

Example 26 with SearchContext

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

the class ExceptionSerializationTests method testSearchParseException.

public void testSearchParseException() throws IOException {
    SearchContext ctx = new TestSearchContext(null);
    SearchParseException ex = serialize(new SearchParseException(ctx, "foo", new XContentLocation(66, 666)));
    assertEquals("foo", ex.getMessage());
    assertEquals(66, ex.getLineNumber());
    assertEquals(666, ex.getColumnNumber());
    assertEquals(ctx.shardTarget(), ex.shard());
}
Also used : TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchParseException(org.elasticsearch.search.SearchParseException) TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchContext(org.elasticsearch.search.internal.SearchContext) XContentLocation(org.elasticsearch.common.xcontent.XContentLocation)

Example 27 with SearchContext

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

the class AggregatorTestCase method createSearchContext.

protected SearchContext createSearchContext(IndexSearcher indexSearcher, IndexSettings indexSettings) {
    Engine.Searcher searcher = new Engine.Searcher("aggregator_test", indexSearcher);
    QueryCache queryCache = new DisabledQueryCache(indexSettings);
    QueryCachingPolicy queryCachingPolicy = new QueryCachingPolicy() {

        @Override
        public void onUse(Query query) {
        }

        @Override
        public boolean shouldCache(Query query) throws IOException {
            // never cache a query
            return false;
        }
    };
    ContextIndexSearcher contextIndexSearcher = new ContextIndexSearcher(searcher, queryCache, queryCachingPolicy);
    SearchContext searchContext = mock(SearchContext.class);
    when(searchContext.numberOfShards()).thenReturn(1);
    when(searchContext.searcher()).thenReturn(contextIndexSearcher);
    when(searchContext.fetchPhase()).thenReturn(new FetchPhase(Arrays.asList(new FetchSourceSubPhase(), new DocValueFieldsFetchSubPhase())));
    doAnswer(invocation -> {
        releasables.add((Releasable) invocation.getArguments()[0]);
        return null;
    }).when(searchContext).addReleasable(anyObject(), anyObject());
    return searchContext;
}
Also used : QueryCachingPolicy(org.apache.lucene.search.QueryCachingPolicy) DisabledQueryCache(org.elasticsearch.index.cache.query.DisabledQueryCache) QueryCache(org.apache.lucene.search.QueryCache) Query(org.apache.lucene.search.Query) ContextIndexSearcher(org.elasticsearch.search.internal.ContextIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) FetchPhase(org.elasticsearch.search.fetch.FetchPhase) ContextIndexSearcher(org.elasticsearch.search.internal.ContextIndexSearcher) SearchContext(org.elasticsearch.search.internal.SearchContext) Engine(org.elasticsearch.index.engine.Engine) DisabledQueryCache(org.elasticsearch.index.cache.query.DisabledQueryCache) FetchSourceSubPhase(org.elasticsearch.search.fetch.subphase.FetchSourceSubPhase) DocValueFieldsFetchSubPhase(org.elasticsearch.search.fetch.subphase.DocValueFieldsFetchSubPhase)

Example 28 with SearchContext

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

the class BestBucketsDeferringCollectorTests method testReplay.

public void testReplay() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    int numDocs = randomIntBetween(1, 128);
    int maxNumValues = randomInt(16);
    for (int i = 0; i < numDocs; i++) {
        Document document = new Document();
        document.add(new StringField("field", String.valueOf(randomInt(maxNumValues)), Field.Store.NO));
        indexWriter.addDocument(document);
    }
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    TermQuery termQuery = new TermQuery(new Term("field", String.valueOf(randomInt(maxNumValues))));
    TopDocs topDocs = indexSearcher.search(termQuery, numDocs);
    SearchContext searchContext = createSearchContext(indexSearcher, createIndexSettings());
    BestBucketsDeferringCollector collector = new BestBucketsDeferringCollector(searchContext);
    Set<Integer> deferredCollectedDocIds = new HashSet<>();
    collector.setDeferredCollector(Collections.singleton(bla(deferredCollectedDocIds)));
    collector.preCollection();
    indexSearcher.search(termQuery, collector);
    collector.postCollection();
    collector.replay(0);
    assertEquals(topDocs.scoreDocs.length, deferredCollectedDocIds.size());
    for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
        assertTrue("expected docid [" + scoreDoc.doc + "] is missing", deferredCollectedDocIds.contains(scoreDoc.doc));
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) SearchContext(org.elasticsearch.search.internal.SearchContext) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet)

Example 29 with SearchContext

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

Example 30 with SearchContext

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

the class CollapseBuilderTests method testBuild.

public void testBuild() throws IOException {
    Directory dir = new RAMDirectory();
    try (IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())))) {
        writer.commit();
    }
    SearchContext searchContext = mockSearchContext();
    try (IndexReader reader = DirectoryReader.open(dir)) {
        when(searchContext.getQueryShardContext().getIndexReader()).thenReturn(reader);
        MappedFieldType numberFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
        MappedFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType();
        for (MappedFieldType fieldType : new MappedFieldType[] { numberFieldType, keywordFieldType }) {
            fieldType.setName("field");
            fieldType.setHasDocValues(true);
            when(searchContext.getQueryShardContext().fieldMapper("field")).thenReturn(fieldType);
            CollapseBuilder builder = new CollapseBuilder("field");
            CollapseContext collapseContext = builder.build(searchContext);
            assertEquals(collapseContext.getFieldType(), fieldType);
            fieldType.setIndexOptions(IndexOptions.NONE);
            collapseContext = builder.build(searchContext);
            assertEquals(collapseContext.getFieldType(), fieldType);
            fieldType.setHasDocValues(false);
            SearchContextException exc = expectThrows(SearchContextException.class, () -> builder.build(searchContext));
            assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`");
            fieldType.setHasDocValues(true);
            builder.setInnerHits(new InnerHitBuilder());
            exc = expectThrows(SearchContextException.class, () -> builder.build(searchContext));
            assertEquals(exc.getMessage(), "cannot expand `inner_hits` for collapse field `field`, " + "only indexed field can retrieve `inner_hits`");
        }
    }
}
Also used : InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchContextException(org.elasticsearch.search.SearchContextException) RAMDirectory(org.apache.lucene.store.RAMDirectory) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

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