Java Code Examples for org.elasticsearch.index.engine.Engine#Searcher

The following examples show how to use org.elasticsearch.index.engine.Engine#Searcher . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexShard.java    From crate with Apache License 2.0 6 votes vote down vote up
private Engine.Searcher acquireSearcher(String source, Engine.SearcherScope scope) {
    readAllowed();
    final Engine engine = getEngine();
    final Engine.Searcher searcher = engine.acquireSearcher(source, scope);
    boolean success = false;
    try {
        final Engine.Searcher wrappedSearcher = searcherWrapper == null ? searcher : searcherWrapper.wrap(searcher);
        assert wrappedSearcher != null;
        success = true;
        return wrappedSearcher;
    } catch (IOException ex) {
        throw new ElasticsearchException("failed to wrap searcher", ex);
    } finally {
        if (success == false) {
            Releasables.close(success, searcher);
        }
    }
}
 
Example 2
Source File: GroupByOptimizedIterator.java    From crate with Apache License 2.0 6 votes vote down vote up
static boolean hasHighCardinalityRatio(Supplier<Engine.Searcher> acquireSearcher, String fieldName) {
    // acquire separate searcher:
    // Can't use sharedShardContexts() yet, if we bail out the "getOrCreateContext" causes issues later on in the fallback logic
    try (Engine.Searcher searcher = acquireSearcher.get()) {
        for (LeafReaderContext leaf : searcher.reader().leaves()) {
            Terms terms = leaf.reader().terms(fieldName);
            if (terms == null) {
                return true;
            }
            double cardinalityRatio = terms.size() / (double) leaf.reader().numDocs();
            if (cardinalityRatio > CARDINALITY_RATIO_THRESHOLD) {
                return true;
            }
        }
    } catch (IOException e) {
        return true;
    }
    return false;
}
 
Example 3
Source File: FetchCollector.java    From crate with Apache License 2.0 6 votes vote down vote up
FetchCollector(List<LuceneCollectorExpression<?>> collectorExpressions,
               Streamer<?>[] streamers,
               Engine.Searcher searcher,
               RamAccounting ramAccounting,
               int readerId) {
    // use toArray to avoid iterator allocations in docIds loop
    this.collectorExpressions = collectorExpressions.toArray(new LuceneCollectorExpression[0]);
    this.streamers = streamers;
    this.readerContexts = searcher.searcher().getIndexReader().leaves();
    this.ramAccounting = ramAccounting;
    CollectorContext collectorContext = new CollectorContext(readerId);
    for (LuceneCollectorExpression<?> collectorExpression : this.collectorExpressions) {
        collectorExpression.startCollect(collectorContext);
    }
    this.row = new InputRow(collectorExpressions);

}
 
Example 4
Source File: DefaultSearchContext.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public DefaultSearchContext(long id, ShardSearchRequest request, SearchShardTarget shardTarget,
                            Engine.Searcher engineSearcher, IndexService indexService, IndexShard indexShard,
                            ScriptService scriptService, PageCacheRecycler pageCacheRecycler,
                            BigArrays bigArrays, Counter timeEstimateCounter, ParseFieldMatcher parseFieldMatcher,
                            TimeValue timeout
) {
    super(parseFieldMatcher, request);
    this.id = id;
    this.request = request;
    this.searchType = request.searchType();
    this.shardTarget = shardTarget;
    this.engineSearcher = engineSearcher;
    this.scriptService = scriptService;
    this.pageCacheRecycler = pageCacheRecycler;
    // SearchContexts use a BigArrays that can circuit break
    this.bigArrays = bigArrays.withCircuitBreaking();
    this.dfsResult = new DfsSearchResult(id, shardTarget);
    this.queryResult = new QuerySearchResult(id, shardTarget);
    this.fetchResult = new FetchSearchResult(id, shardTarget);
    this.indexShard = indexShard;
    this.indexService = indexService;
    this.searcher = new ContextIndexSearcher(engineSearcher, indexService.cache().query(), indexShard.getQueryCachingPolicy());
    this.timeEstimateCounter = timeEstimateCounter;
    this.timeoutInMillis = timeout.millis();
}
 
Example 5
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void initialize(Engine.Searcher docSearcher, ParsedDocument parsedDocument) {
    this.docSearcher = docSearcher;

    IndexReader indexReader = docSearcher.reader();
    LeafReaderContext atomicReaderContext = indexReader.leaves().get(0);
    LeafSearchLookup leafLookup = lookup().getLeafSearchLookup(atomicReaderContext);
    leafLookup.setDocument(0);
    leafLookup.source().setSource(parsedDocument.source());

    Map<String, SearchHitField> fields = new HashMap<>();
    for (IndexableField field : parsedDocument.rootDoc().getFields()) {
        fields.put(field.name(), new InternalSearchHitField(field.name(), Collections.emptyList()));
    }
    hitContext().reset(
            new InternalSearchHit(0, "unknown", new Text(parsedDocument.type()), fields),
            atomicReaderContext, 0, docSearcher.searcher()
    );
}
 
Example 6
Source File: CollectTask.java    From crate with Apache License 2.0 6 votes vote down vote up
public void addSearcher(int searcherId, Engine.Searcher searcher) {
    if (isClosed()) {
        // if this is closed and addContext is called this means the context got killed.
        searcher.close();
        return;
    }

    synchronized (subContextLock) {
        Engine.Searcher replacedSearcher = searchers.put(searcherId, searcher);
        if (replacedSearcher != null) {
            replacedSearcher.close();
            searcher.close();
            throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                "ShardCollectContext for %d already added", searcherId));
        }
    }
}
 
Example 7
Source File: TransportSuggestAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected ShardSuggestResponse shardOperation(ShardSuggestRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());
    ShardSuggestMetric suggestMetric = indexShard.getSuggestMetric();
    suggestMetric.preSuggest();
    long startTime = System.nanoTime();
    XContentParser parser = null;
    try (Engine.Searcher searcher = indexShard.acquireSearcher("suggest")) {
        BytesReference suggest = request.suggest();
        if (suggest != null && suggest.length() > 0) {
            parser = XContentFactory.xContent(suggest).createParser(suggest);
            if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                throw new IllegalArgumentException("suggest content missing");
            }
            final SuggestionSearchContext context = suggestPhase.parseElement().parseInternal(parser, indexService.mapperService(),
                    indexService.queryParserService(), request.shardId().getIndex(), request.shardId().id(), request);
            final Suggest result = suggestPhase.execute(context, searcher.searcher());
            return new ShardSuggestResponse(request.shardId(), result);
        }
        return new ShardSuggestResponse(request.shardId(), new Suggest());
    } catch (Throwable ex) {
        throw new ElasticsearchException("failed to execute suggest", ex);
    } finally {
        if (parser != null) {
            parser.close();
        }
        suggestMetric.postSuggest(System.nanoTime() - startTime);
    }
}
 
Example 8
Source File: MockEngineSupport.java    From crate with Apache License 2.0 5 votes vote down vote up
SearcherCloseable(final Engine.Searcher wrappedSearcher, Logger logger, InFlightSearchers inFlightSearchers) {
    // we only use the given index searcher here instead of the IS of the wrapped searcher. the IS might be a wrapped searcher
    // with a wrapped reader.
    this.wrappedSearcher = wrappedSearcher;
    this.logger = logger;
    initialRefCount = wrappedSearcher.reader().getRefCount();
    this.inFlightSearchers = inFlightSearchers;
    assert initialRefCount > 0 :
        "IndexReader#getRefCount() was [" + initialRefCount + "] expected a value > [0] - reader is already closed";
    inFlightSearchers.add(this, wrappedSearcher.source());
}
 
Example 9
Source File: RefCountSearcher.java    From crate with Apache License 2.0 5 votes vote down vote up
RefCountSearcher(ShardId shardId,
                 Engine.Searcher searcher,
                 IndexSearcher indexSearcher) {
    super(searcher.source(), indexSearcher, () -> {});
    this.shardId = shardId;
    this.searcher = searcher;
    this.traceEnabled = LOGGER.isTraceEnabled();
}
 
Example 10
Source File: SharedShardContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public Engine.Searcher searcher() throws IndexNotFoundException {
    if (searcher != null) {
        searcher.inc();
        return searcher;
    }
    synchronized (mutex) {
        if (searcher == null) {
            searcher = new RefCountSearcher(indexShard().acquireSearcher("shared-shard-context"));
        }
    }
    searcher.inc();
    return searcher;
}
 
Example 11
Source File: CollectTaskTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddingSameContextTwice() throws Exception {
    Engine.Searcher mock1 = mock(Engine.Searcher.class);
    Engine.Searcher mock2 = mock(Engine.Searcher.class);
    try {
        collectTask.addSearcher(1, mock1);
        collectTask.addSearcher(1, mock2);

        assertFalse(true); // second addContext call should have raised an exception
    } catch (IllegalArgumentException e) {
        verify(mock1, times(1)).close();
        verify(mock2, times(1)).close();
    }
}
 
Example 12
Source File: InternalCountOperation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public long count(TransactionContext txnCtx, Index index, int shardId, Symbol filter) throws IOException, InterruptedException {
    IndexService indexService;
    try {
        indexService = indicesService.indexServiceSafe(index);
    } catch (IndexNotFoundException e) {
        if (IndexParts.isPartitioned(index.getName())) {
            return 0L;
        }
        throw e;
    }

    IndexShard indexShard = indexService.getShard(shardId);
    try (Engine.Searcher searcher = indexShard.acquireSearcher("count-operation")) {
        String indexName = indexShard.shardId().getIndexName();
        var relationName = RelationName.fromIndexName(indexName);
        DocTableInfo table = schemas.getTableInfo(relationName, Operation.READ);
        LuceneQueryBuilder.Context queryCtx = queryBuilder.convert(
            filter,
            txnCtx,
            indexService.mapperService(),
            indexName,
            indexService.newQueryShardContext(),
            table,
            indexService.cache()
        );
        if (Thread.interrupted()) {
            throw new InterruptedException("thread interrupted during count-operation");
        }
        return searcher.searcher().count(queryCtx.query());
    }
}
 
Example 13
Source File: ReservoirSampler.java    From crate with Apache License 2.0 5 votes vote down vote up
DocIdToRow(Engine.Searcher searcher,
           List<Input<?>> inputs,
           List<? extends LuceneCollectorExpression<?>> expressions) {
    this.searcher = searcher;
    this.inputs = inputs;
    this.expressions = expressions;
}
 
Example 14
Source File: ContextIndexSearcher.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public ContextIndexSearcher(Engine.Searcher searcher,
                            QueryCache queryCache, QueryCachingPolicy queryCachingPolicy) {
    super(searcher.reader());
    in = searcher.searcher();
    engineSearcher = searcher;
    setSimilarity(searcher.searcher().getSimilarity(true));
    setQueryCache(queryCache);
    setQueryCachingPolicy(queryCachingPolicy);
}
 
Example 15
Source File: IndexShard.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Engine.Searcher acquireSearcher(String source) {
    readAllowed(source);
    return engine().acquireSearcher(source);
}
 
Example 16
Source File: SearchService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
final SearchContext createContext(ShardSearchRequest request, @Nullable Engine.Searcher searcher) {
    IndexService indexService = indicesService.indexServiceSafe(request.index());
    IndexShard indexShard = indexService.shardSafe(request.shardId());

    SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(), request.index(), request.shardId());
    String searchSource = "search";
    if (request.hasHeader("search_source")) {
        searchSource = request.getHeader("search_source");
    }
    Engine.Searcher engineSearcher = searcher == null ? indexShard.acquireSearcher(searchSource) : searcher;

    DefaultSearchContext context = new DefaultSearchContext(idGenerator.incrementAndGet(), request, shardTarget, engineSearcher, indexService, indexShard, scriptService, pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher, defaultSearchTimeout);
    SearchContext.setCurrent(context);
    try {
        if (request.scroll() != null) {
            context.scrollContext(new ScrollContext());
            context.scrollContext().scroll = request.scroll();
        }

        parseTemplate(request, context);
        parseSource(context, request.source());
        parseSource(context, request.extraSource());

        // if the from and size are still not set, default them
        if (context.from() == -1) {
            context.from(0);
        }
        if (context.searchType() == SearchType.COUNT) {
            // so that the optimizations we apply to size=0 also apply to search_type=COUNT
            // and that we close contexts when done with the query phase
            context.searchType(SearchType.QUERY_THEN_FETCH);
            context.size(0);
        } else if (context.size() == -1) {
            context.size(10);
        }

        if (context.request().isProfile()) {
            context.setProfilers(new Profilers(context.searcher()));
        }

        // pre process
        dfsPhase.preProcess(context);
        queryPhase.preProcess(context);
        fetchPhase.preProcess(context);

        // compute the context keep alive
        long keepAlive = defaultKeepAlive;
        if (request.scroll() != null && request.scroll().keepAlive() != null) {
            keepAlive = request.scroll().keepAlive().millis();
        }
        context.keepAlive(keepAlive);
    } catch (Throwable e) {
        context.close();
        throw ExceptionsHelper.convertToRuntime(e);
    }

    return context;
}
 
Example 17
Source File: LuceneShardCollectorProvider.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public OrderedDocCollector getOrderedCollector(RoutedCollectPhase phase,
                                               SharedShardContext sharedShardContext,
                                               CollectTask collectTask,
                                               boolean requiresRepeat) {
    RoutedCollectPhase collectPhase = phase.normalize(shardNormalizer, collectTask.txnCtx());

    CollectorContext collectorContext;
    InputFactory.Context<? extends LuceneCollectorExpression<?>> ctx;
    Engine.Searcher searcher = null;
    LuceneQueryBuilder.Context queryContext;
    try {
        searcher = sharedShardContext.acquireSearcher(formatSource(phase));
        IndexService indexService = sharedShardContext.indexService();
        QueryShardContext queryShardContext = indexService.newQueryShardContext();
        queryContext = luceneQueryBuilder.convert(
            collectPhase.where(),
            collectTask.txnCtx(),
            indexService.mapperService(),
            indexShard.shardId().getIndexName(),
            queryShardContext,
            table,
            indexService.cache()
        );
        collectTask.addSearcher(sharedShardContext.readerId(), searcher);
        ctx = docInputFactory.extractImplementations(collectTask.txnCtx(), collectPhase);
        collectorContext = new CollectorContext(sharedShardContext.readerId());
    } catch (Throwable t) {
        if (searcher != null) {
            searcher.close();
        }
        throw t;
    }
    int batchSize = collectPhase.shardQueueSize(localNodeId.get());
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[{}][{}] creating LuceneOrderedDocCollector. Expected number of rows to be collected: {}",
            sharedShardContext.indexShard().routingEntry().currentNodeId(),
            sharedShardContext.indexShard().shardId(),
            batchSize);
    }
    OptimizeQueryForSearchAfter optimizeQueryForSearchAfter = new OptimizeQueryForSearchAfter(
        collectPhase.orderBy(),
        queryContext.queryShardContext(),
        fieldTypeLookup
    );
    return new LuceneOrderedDocCollector(
        indexShard.shardId(),
        searcher.searcher(),
        queryContext.query(),
        queryContext.minScore(),
        Symbols.containsColumn(collectPhase.toCollect(), DocSysColumns.SCORE),
        batchSize,
        collectTask.getRamAccounting(),
        collectorContext,
        optimizeQueryForSearchAfter,
        LuceneSortGenerator.generateLuceneSort(collectTask.txnCtx(), collectorContext, collectPhase.orderBy(), docInputFactory, fieldTypeLookup),
        ctx.topLevelInputs(),
        ctx.expressions()
    );
}
 
Example 18
Source File: IndicesWarmer.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/** Return a searcher instance that only wraps the segments to warm. */
public Engine.Searcher searcher() {
    return searcher;
}
 
Example 19
Source File: IndicesWarmer.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public WarmerContext(ShardId shardId, Engine.Searcher searcher) {
    this.shardId = shardId;
    this.searcher = searcher;
}
 
Example 20
Source File: FetchContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected void innerClose(@Nullable Throwable t) {
    for (IntObjectCursor<Engine.Searcher> cursor : searchers) {
        cursor.value.close();
    }
}