Search in sources :

Example 1 with TermVectorsFilter

use of org.elasticsearch.action.termvectors.TermVectorsFilter in project elasticsearch by elastic.

the class TermVectorsService method getTermVectors.

static TermVectorsResponse getTermVectors(IndexShard indexShard, TermVectorsRequest request, LongSupplier nanoTimeSupplier) {
    final long startTime = nanoTimeSupplier.getAsLong();
    final TermVectorsResponse termVectorsResponse = new TermVectorsResponse(indexShard.shardId().getIndex().getName(), request.type(), request.id());
    final Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id()));
    Engine.GetResult get = indexShard.get(new Engine.Get(request.realtime(), uidTerm).version(request.version()).versionType(request.versionType()));
    Fields termVectorsByField = null;
    AggregatedDfs dfs = null;
    TermVectorsFilter termVectorsFilter = null;
    /* handle potential wildcards in fields */
    if (request.selectedFields() != null) {
        handleFieldWildcards(indexShard, request);
    }
    final Engine.Searcher searcher = indexShard.acquireSearcher("term_vector");
    try {
        Fields topLevelFields = MultiFields.getFields(get.searcher() != null ? get.searcher().reader() : searcher.reader());
        Versions.DocIdAndVersion docIdAndVersion = get.docIdAndVersion();
        /* from an artificial document */
        if (request.doc() != null) {
            termVectorsByField = generateTermVectorsFromDoc(indexShard, request);
            // if no document indexed in shard, take the queried document itself for stats
            if (topLevelFields == null) {
                topLevelFields = termVectorsByField;
            }
            termVectorsResponse.setArtificial(true);
            termVectorsResponse.setExists(true);
        } else /* or from an existing document */
        if (docIdAndVersion != null) {
            // fields with stored term vectors
            termVectorsByField = docIdAndVersion.context.reader().getTermVectors(docIdAndVersion.docId);
            Set<String> selectedFields = request.selectedFields();
            // generate tvs for fields where analyzer is overridden
            if (selectedFields == null && request.perFieldAnalyzer() != null) {
                selectedFields = getFieldsToGenerate(request.perFieldAnalyzer(), termVectorsByField);
            }
            // fields without term vectors
            if (selectedFields != null) {
                termVectorsByField = addGeneratedTermVectors(indexShard, get, termVectorsByField, request, selectedFields);
            }
            termVectorsResponse.setDocVersion(docIdAndVersion.version);
            termVectorsResponse.setExists(true);
        } else /* no term vectors generated or found */
        {
            termVectorsResponse.setExists(false);
        }
        /* if there are term vectors, optional compute dfs and/or terms filtering */
        if (termVectorsByField != null) {
            if (request.filterSettings() != null) {
                termVectorsFilter = new TermVectorsFilter(termVectorsByField, topLevelFields, request.selectedFields(), dfs);
                termVectorsFilter.setSettings(request.filterSettings());
                try {
                    termVectorsFilter.selectBestTerms();
                } catch (IOException e) {
                    throw new ElasticsearchException("failed to select best terms", e);
                }
            }
            // write term vectors
            termVectorsResponse.setFields(termVectorsByField, request.selectedFields(), request.getFlags(), topLevelFields, dfs, termVectorsFilter);
        }
        termVectorsResponse.setTookInMillis(TimeUnit.NANOSECONDS.toMillis(nanoTimeSupplier.getAsLong() - startTime));
    } catch (Exception ex) {
        throw new ElasticsearchException("failed to execute term vector request", ex);
    } finally {
        searcher.close();
        get.release();
    }
    return termVectorsResponse;
}
Also used : TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) HashSet(java.util.HashSet) Set(java.util.Set) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) TermVectorsFilter(org.elasticsearch.action.termvectors.TermVectorsFilter) Fields(org.apache.lucene.index.Fields) MultiFields(org.apache.lucene.index.MultiFields) Versions(org.elasticsearch.common.lucene.uid.Versions) AggregatedDfs(org.elasticsearch.search.dfs.AggregatedDfs) Engine(org.elasticsearch.index.engine.Engine)

Aggregations

IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Fields (org.apache.lucene.index.Fields)1 MultiFields (org.apache.lucene.index.MultiFields)1 Term (org.apache.lucene.index.Term)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 TermVectorsFilter (org.elasticsearch.action.termvectors.TermVectorsFilter)1 TermVectorsResponse (org.elasticsearch.action.termvectors.TermVectorsResponse)1 Versions (org.elasticsearch.common.lucene.uid.Versions)1 Engine (org.elasticsearch.index.engine.Engine)1 AggregatedDfs (org.elasticsearch.search.dfs.AggregatedDfs)1