Java Code Examples for org.elasticsearch.index.shard.IndexShard#acquireSearcher()

The following examples show how to use org.elasticsearch.index.shard.IndexShard#acquireSearcher() . 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: TransportFieldStatsTransportAction.java    From Elasticsearch with Apache License 2.0 7 votes vote down vote up
@Override
protected FieldStatsShardResponse shardOperation(FieldStatsShardRequest request) {
    ShardId shardId = request.shardId();
    Map<String, FieldStats> fieldStats = new HashMap<>();
    IndexService indexServices = indicesService.indexServiceSafe(shardId.getIndex());
    MapperService mapperService = indexServices.mapperService();
    IndexShard shard = indexServices.shardSafe(shardId.id());
    try (Engine.Searcher searcher = shard.acquireSearcher("fieldstats")) {
        for (String field : request.getFields()) {
            MappedFieldType fieldType = mapperService.fullName(field);
            if (fieldType != null) {
                IndexReader reader = searcher.reader();
                Terms terms = MultiFields.getTerms(reader, field);
                if (terms != null) {
                    fieldStats.put(field, fieldType.stats(terms, reader.maxDoc()));
                }
            } else {
                throw new IllegalArgumentException("field [" + field + "] doesn't exist");
            }
        }
    } catch (IOException e) {
        throw ExceptionsHelper.convertToElastic(e);
    }
    return new FieldStatsShardResponse(shardId, fieldStats);
}
 
Example 2
Source File: InternalCountOperation.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public long count(String index, int shardId, WhereClause whereClause) throws IOException, InterruptedException {
    IndexService indexService;
    try {
        indexService = indicesService.indexServiceSafe(index);
    } catch (IndexNotFoundException e) {
        if (PartitionName.isPartition(index)) {
            return 0L;
        }
        throw e;
    }

    IndexShard indexShard = indexService.shardSafe(shardId);
    try (Engine.Searcher searcher = indexShard.acquireSearcher("count-operation")) {
        LuceneQueryBuilder.Context queryCtx = queryBuilder.convert(
                whereClause, indexService.mapperService(), indexService.fieldData(), indexService.cache());
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        return searcher.searcher().count(queryCtx.query());
    }
}
 
Example 3
Source File: IndicesTTLService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void purgeShards(List<IndexShard> shardsToPurge) {
    for (IndexShard shardToPurge : shardsToPurge) {
        Query query = shardToPurge.indexService().mapperService().smartNameFieldType(TTLFieldMapper.NAME).rangeQuery(null, System.currentTimeMillis(), false, true);
        Engine.Searcher searcher = shardToPurge.acquireSearcher("indices_ttl");
        try {
            logger.debug("[{}][{}] purging shard", shardToPurge.routingEntry().index(), shardToPurge.routingEntry().id());
            ExpiredDocsCollector expiredDocsCollector = new ExpiredDocsCollector();
            searcher.searcher().search(query, expiredDocsCollector);
            List<DocToPurge> docsToPurge = expiredDocsCollector.getDocsToPurge();

            BulkRequest bulkRequest = new BulkRequest();
            for (DocToPurge docToPurge : docsToPurge) {

                bulkRequest.add(new DeleteRequest().index(shardToPurge.routingEntry().index()).type(docToPurge.type).id(docToPurge.id).version(docToPurge.version).routing(docToPurge.routing));
                bulkRequest = processBulkIfNeeded(bulkRequest, false);
            }
            processBulkIfNeeded(bulkRequest, true);
        } catch (Exception e) {
            logger.warn("failed to purge", e);
        } finally {
            searcher.close();
        }
    }
}
 
Example 4
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public PercolateContext(PercolateShardRequest request, SearchShardTarget searchShardTarget, IndexShard indexShard,
                        IndexService indexService, PageCacheRecycler pageCacheRecycler,
                        BigArrays bigArrays, ScriptService scriptService, Query aliasFilter, ParseFieldMatcher parseFieldMatcher) {
    super(parseFieldMatcher, request);
    this.indexShard = indexShard;
    this.indexService = indexService;
    this.fieldDataService = indexService.fieldData();
    this.searchShardTarget = searchShardTarget;
    this.percolateQueries = indexShard.percolateRegistry().percolateQueries();
    this.types = new String[]{request.documentType()};
    this.pageCacheRecycler = pageCacheRecycler;
    this.bigArrays = bigArrays.withCircuitBreaking();
    this.querySearchResult = new QuerySearchResult(0, searchShardTarget);
    this.engineSearcher = indexShard.acquireSearcher("percolate");
    this.searcher = new ContextIndexSearcher(engineSearcher, indexService.cache().query(), indexShard.getQueryCachingPolicy());
    this.scriptService = scriptService;
    this.numberOfShards = request.getNumberOfShards();
    this.aliasFilter = aliasFilter;
    this.startTime = request.getStartTime();
}
 
Example 5
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 6
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 7
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 8
Source File: TransportExistsAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardExistsResponse shardOperation(ShardExistsRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());

    SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(), request.shardId().getIndex(), request.shardId().id());
    SearchContext context = new DefaultSearchContext(0,
            new ShardSearchLocalRequest(request.types(), request.nowInMillis(), request.filteringAliases()),
            shardTarget, indexShard.acquireSearcher("exists"), indexService, indexShard,
            scriptService, pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher,
            SearchService.NO_TIMEOUT
    );
    SearchContext.setCurrent(context);

    try {
        if (request.minScore() != DEFAULT_MIN_SCORE) {
            context.minimumScore(request.minScore());
        }
        BytesReference source = request.querySource();
        if (source != null && source.length() > 0) {
            try {
                QueryParseContext.setTypes(request.types());
                context.parsedQuery(indexService.queryParserService().parseQuery(source));
            } finally {
                QueryParseContext.removeTypes();
            }
        }
        context.preProcess();
        try {
            boolean exists;
            try {
                exists = Lucene.exists(context.searcher(), context.query());
            } finally {
                context.clearReleasables(SearchContext.Lifetime.COLLECTION);
            }
            return new ShardExistsResponse(request.shardId(), exists);
        } catch (Exception e) {
            throw new QueryPhaseExecutionException(context, "failed to execute exists", e);
        }
    } finally {
        // this will also release the index searcher
        context.close();
        SearchContext.removeCurrent();
    }
}
 
Example 9
Source File: TransportTermsByQueryAction.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * The operation that executes the query and generates a {@link TermsByQueryShardResponse} for each shard.
 */
@Override
protected TermsByQueryShardResponse shardOperation(TermsByQueryShardRequest shardRequest) throws ElasticsearchException {
  IndexService indexService = indicesService.indexServiceSafe(shardRequest.shardId().getIndex());
  IndexShard indexShard = indexService.shardSafe(shardRequest.shardId().id());
  TermsByQueryRequest request = shardRequest.request();
  OrderByShardOperation orderByOperation = OrderByShardOperation.get(request.getOrderBy(), request.maxTermsPerShard());

  SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(),
                                                        shardRequest.shardId().getIndex(),
                                                        shardRequest.shardId().id());

  ShardSearchRequest shardSearchRequest = new ShardSearchLocalRequest(request.types(), request.nowInMillis(),
                                                                      shardRequest.filteringAliases());

  SearchContext context = new DefaultSearchContext(0, shardSearchRequest, shardTarget,
    indexShard.acquireSearcher("termsByQuery"), indexService, indexShard, scriptService,
    pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher,
    SearchService.NO_TIMEOUT);
  SearchContext.setCurrent(context);

  try {
    MappedFieldType fieldType = context.smartNameFieldType(request.field());
    if (fieldType == null) {
      throw new SearchContextException(context, "[termsByQuery] field '" + request.field() +
              "' not found for types " + Arrays.toString(request.types()));
    }

    IndexFieldData indexFieldData = context.fieldData().getForField(fieldType);

    BytesReference querySource = request.querySource();
    if (querySource != null && querySource.length() > 0) {
      XContentParser queryParser = null;
      try {
        queryParser = XContentFactory.xContent(querySource).createParser(querySource);
        QueryParseContext.setTypes(request.types());
        ParsedQuery parsedQuery = orderByOperation.getParsedQuery(queryParser, indexService);
        if (parsedQuery != null) {
          context.parsedQuery(parsedQuery);
        }
      }
      finally {
        QueryParseContext.removeTypes();
        if (queryParser != null) {
          queryParser.close();
        }
      }
    }

    context.preProcess();

    // execute the search only gathering the hit count and bitset for each segment
    logger.debug("{}: Executes search for collecting terms {}", Thread.currentThread().getName(),
      shardRequest.shardId());

    TermsCollector termsCollector = this.getTermsCollector(request.termsEncoding(), indexFieldData, context);
    if (request.expectedTerms() != null) termsCollector.setExpectedTerms(request.expectedTerms());
    if (request.maxTermsPerShard() != null) termsCollector.setMaxTerms(request.maxTermsPerShard());
    HitStream hitStream = orderByOperation.getHitStream(context);
    TermsSet terms = termsCollector.collect(hitStream);

    logger.debug("{}: Returns terms response with {} terms for shard {}", Thread.currentThread().getName(),
      terms.size(), shardRequest.shardId());

    return new TermsByQueryShardResponse(shardRequest.shardId(), terms);
  }
  catch (Throwable e) {
    logger.error("[termsByQuery] Error executing shard operation", e);
    throw new QueryPhaseExecutionException(context, "[termsByQuery] Failed to execute query", e);
  }
  finally {
    // this will also release the index searcher
    context.close();
    SearchContext.removeCurrent();
  }
}