org.elasticsearch.search.fetch.FetchSubPhase Java Examples

The following examples show how to use org.elasticsearch.search.fetch.FetchSubPhase. 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: HighlightUtils.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
static List<Object> loadFieldValues(SearchContextHighlight.Field field, FieldMapper mapper, SearchContext searchContext, FetchSubPhase.HitContext hitContext) throws IOException {
    //percolator needs to always load from source, thus it sets the global force source to true
    boolean forceSource = searchContext.highlight().forceSource(field);
    List<Object> textsToHighlight;
    if (!forceSource && mapper.fieldType().stored()) {
        CustomFieldsVisitor fieldVisitor = new CustomFieldsVisitor(ImmutableSet.of(mapper.fieldType().names().indexName()), false);
        hitContext.reader().document(hitContext.docId(), fieldVisitor);
        textsToHighlight = fieldVisitor.fields().get(mapper.fieldType().names().indexName());
        if (textsToHighlight == null) {
            // Can happen if the document doesn't have the field to highlight
            textsToHighlight = Collections.emptyList();
        }
    } else {
        SourceLookup sourceLookup = searchContext.lookup().source();
        sourceLookup.setSegmentAndDocument(hitContext.readerContext(), hitContext.docId());
        textsToHighlight = sourceLookup.extractRawValues(hitContext.getSourcePath(mapper.fieldType().names().fullName()));
    }
    assert textsToHighlight != null;
    return textsToHighlight;
}
 
Example #2
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public <SubPhaseContext extends FetchSubPhaseContext> SubPhaseContext getFetchSubPhaseContext(FetchSubPhase.ContextFactory<SubPhaseContext> contextFactory) {
    String subPhaseName = contextFactory.getName();
    if (subPhaseContexts.get(subPhaseName) == null) {
        subPhaseContexts.put(subPhaseName, contextFactory.newContextInstance());
    }
    return (SubPhaseContext) subPhaseContexts.get(subPhaseName);
}
 
Example #3
Source File: Exporter.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Inject
public Exporter(VersionFetchSubPhase versionPhase, Injector injector,
        SettingsFilter settingsFilter) {
    this.fetchSubPhases = new FetchSubPhase[]{versionPhase};
    this.injector = injector;
    this.settingsFilter = settingsFilter;
}
 
Example #4
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContext) throws IOException {
    Query rawParentFilter;
    if (parentObjectMapper == null) {
        rawParentFilter = Queries.newNonNestedFilter();
    } else {
        rawParentFilter = parentObjectMapper.nestedTypeFilter();
    }
    BitSetProducer parentFilter = context.bitsetFilterCache().getBitSetProducer(rawParentFilter);
    Query childFilter = childObjectMapper.nestedTypeFilter();
    Query q = Queries.filtered(query.query(), new NestedChildrenQuery(parentFilter, childFilter, hitContext));

    if (size() == 0) {
        return new TopDocs(context.searcher().count(q), Lucene.EMPTY_SCORE_DOCS, 0);
    } else {
        int topN = Math.min(from() + size(), context.searcher().getIndexReader().maxDoc());
        TopDocsCollector topDocsCollector;
        if (sort() != null) {
            try {
                topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
            } catch (IOException e) {
                throw ExceptionsHelper.convertToElastic(e);
            }
        } else {
            topDocsCollector = TopScoreDocCollector.create(topN);
        }
        try {
            context.searcher().search(q, topDocsCollector);
        } finally {
            clearReleasables(Lifetime.COLLECTION);
        }
        return topDocsCollector.topDocs(from(), size());
    }
}
 
Example #5
Source File: BulkWriterCollector.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Inject
public BulkWriterCollector(@Assisted SearchIntoContext context,
        Client client, ScriptFieldsFetchSubPhase scriptFieldsPhase,
        VersionFetchSubPhase versionFetchSubPhase) {
    super(context,
            new FetchSubPhase[]{versionFetchSubPhase, scriptFieldsPhase});
    this.client = client;
}
 
Example #6
Source File: DefaultSearchContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public <SubPhaseContext extends FetchSubPhaseContext> SubPhaseContext getFetchSubPhaseContext(FetchSubPhase.ContextFactory<SubPhaseContext> contextFactory) {
    String subPhaseName = contextFactory.getName();
    if (subPhaseContexts.get(subPhaseName) == null) {
        subPhaseContexts.put(subPhaseName, contextFactory.newContextInstance());
    }
    return (SubPhaseContext) subPhaseContexts.get(subPhaseName);
}
 
Example #7
Source File: HighlighterContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public HighlighterContext(String fieldName, SearchContextHighlight.Field field, FieldMapper mapper, SearchContext context,
        FetchSubPhase.HitContext hitContext, Query query) {
    this.fieldName = fieldName;
    this.field = field;
    this.mapper = mapper;
    this.context = context;
    this.hitContext = hitContext;
    this.query = query;
}
 
Example #8
Source File: SearchModule.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected void configureFetchSubPhase() {
    Multibinder<FetchSubPhase> fetchSubPhaseMultibinder = Multibinder.newSetBinder(binder(), FetchSubPhase.class);
    fetchSubPhaseMultibinder.addBinding().to(ExplainFetchSubPhase.class);
    fetchSubPhaseMultibinder.addBinding().to(FieldDataFieldsFetchSubPhase.class);
    fetchSubPhaseMultibinder.addBinding().to(ScriptFieldsFetchSubPhase.class);
    fetchSubPhaseMultibinder.addBinding().to(FetchSourceSubPhase.class);
    fetchSubPhaseMultibinder.addBinding().to(VersionFetchSubPhase.class);
    fetchSubPhaseMultibinder.addBinding().to(MatchedQueriesFetchSubPhase.class);
    fetchSubPhaseMultibinder.addBinding().to(HighlightPhase.class);
    for (Class<? extends FetchSubPhase> clazz : fetchSubPhases) {
        fetchSubPhaseMultibinder.addBinding().to(clazz);
    }
    bind(InnerHitsFetchSubPhase.class).asEagerSingleton();
}
 
Example #9
Source File: SourceScoreOrderFragmentsBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public SourceScoreOrderFragmentsBuilder(FieldMapper mapper, SearchContext searchContext,
                                        FetchSubPhase.HitContext hitContext, String[] preTags, String[] postTags, BoundaryScanner boundaryScanner) {
    super(preTags, postTags, boundaryScanner);
    this.mapper = mapper;
    this.searchContext = searchContext;
    this.hitContext = hitContext;
}
 
Example #10
Source File: ExportCollector.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
    fieldsVisitor.reset();
    currentReader.document(doc, fieldsVisitor);

    Map<String, SearchHitField> searchFields = null;
    if (fieldsVisitor.fields() != null) {
        searchFields = new HashMap<String, SearchHitField>(fieldsVisitor.fields().size());
        for (Map.Entry<String, List<Object>> entry : fieldsVisitor.fields().entrySet()) {
            searchFields.put(entry.getKey(), new InternalSearchHitField(entry.getKey(), entry.getValue()));
        }
    }

    DocumentMapper documentMapper = context.mapperService()
            .documentMapper(fieldsVisitor.uid().type());
    Text typeText;
    if (documentMapper == null) {
        typeText = new StringAndBytesText(fieldsVisitor.uid().type());
    } else {
        typeText = documentMapper.typeText();
    }

    InternalSearchHit searchHit = new InternalSearchHit(doc,
            fieldsVisitor.uid().id(), typeText,
            sourceRequested ? fieldsVisitor.source() : null,
            searchFields);

    for (FetchSubPhase fetchSubPhase : fetchSubPhases) {
        FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
        if (fetchSubPhase.hitExecutionNeeded(context)) {
            hitContext.reset(searchHit, arc, doc, context.searcher().getIndexReader(), doc, fieldsVisitor);
            fetchSubPhase.hitExecute(context, hitContext);
        }
    }

    searchHit.shardTarget(context.shardTarget());
    exportFields.hit(searchHit);
    BytesStreamOutput os = new BytesStreamOutput();
    XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(XContentType.JSON), os);
    exportFields.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.flush();
    BytesReference bytes = os.bytes();
    out.write(bytes.array(), bytes.arrayOffset(), bytes.length());
    out.write('\n');
    out.flush();
    numExported++;
}
 
Example #11
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public FetchSubPhase.HitContext hitContext() {
    if (hitContext == null) {
        hitContext = new FetchSubPhase.HitContext();
    }
    return hitContext;
}
 
Example #12
Source File: WriterCollector.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
    fieldsVisitor.reset();
    currentReader.document(doc, fieldsVisitor);

    Map<String, SearchHitField> searchFields = null;
    if (fieldsVisitor.fields() != null) {
        searchFields = new HashMap<String, SearchHitField>(
                fieldsVisitor.fields().size());
        for (Map.Entry<String, List<Object>> entry : fieldsVisitor
                .fields().entrySet()) {
            searchFields.put(entry.getKey(), new InternalSearchHitField(
                    entry.getKey(), entry.getValue()));
        }
    }

    DocumentMapper documentMapper = context.mapperService().documentMapper(
            fieldsVisitor.uid().type());
    Text typeText;
    if (documentMapper == null) {
        typeText = new StringAndBytesText(fieldsVisitor.uid().type());
    } else {
        typeText = documentMapper.typeText();
    }

    InternalSearchHit searchHit = new InternalSearchHit(doc,
            fieldsVisitor.uid().id(), typeText,
            sourceRequested ? fieldsVisitor.source() : null, searchFields);

    // it looks like it is safe to reuse the HitContext,
    // the cache is only used by the highlighter which we do not use.
    FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
    for (FetchSubPhase fetchSubPhase : fetchSubPhases) {
        if (fetchSubPhase.hitExecutionNeeded(context)) {
            hitContext.reset(searchHit, arc, doc,
                    context.searcher().getIndexReader(), doc,
                    fieldsVisitor);
            fetchSubPhase.hitExecute(context, hitContext);
        }
    }
    searchHit.shardTarget(context.shardTarget());
    collectHit(searchHit);
    numExported++;
}
 
Example #13
Source File: LtrQueryParserPlugin.java    From elasticsearch-learning-to-rank with Apache License 2.0 4 votes vote down vote up
@Override
public List<FetchSubPhase> getFetchSubPhases(FetchPhaseConstructionContext context) {
    return singletonList(new LoggingFetchSubPhase());
}
 
Example #14
Source File: SourceSimpleFragmentsBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public SourceSimpleFragmentsBuilder(FieldMapper mapper, SearchContext searchContext,
                                    FetchSubPhase.HitContext hitContext, String[] preTags, String[] postTags, BoundaryScanner boundaryScanner) {
    super(mapper, preTags, postTags, boundaryScanner);
    this.searchContext = searchContext;
    this.hitContext = hitContext;
}
 
Example #15
Source File: SearchModule.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public void registerFetchSubPhase(Class<? extends FetchSubPhase> subPhase) {
    fetchSubPhases.add(subPhase);
}
 
Example #16
Source File: FilteredSearchContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public <SubPhaseContext extends FetchSubPhaseContext> SubPhaseContext getFetchSubPhaseContext(FetchSubPhase.ContextFactory<SubPhaseContext> contextFactory) {
    return in.getFetchSubPhaseContext(contextFactory);
}
 
Example #17
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContext) throws IOException {
    final String field;
    final String term;
    if (isParentHit(hitContext.hit())) {
        field = ParentFieldMapper.NAME;
        term = Uid.createUid(hitContext.hit().type(), hitContext.hit().id());
    } else if (isChildHit(hitContext.hit())) {
        DocumentMapper hitDocumentMapper = mapperService.documentMapper(hitContext.hit().type());
        final String parentType = hitDocumentMapper.parentFieldMapper().type();
        field = UidFieldMapper.NAME;
        SearchHitField parentField = hitContext.hit().field(ParentFieldMapper.NAME);
        if (parentField == null) {
            throw new IllegalStateException("All children must have a _parent");
        }
        term = Uid.createUid(parentType, (String) parentField.getValue());
    } else {
        return Lucene.EMPTY_TOP_DOCS;
    }

    BooleanQuery q = new BooleanQuery.Builder()
        .add(query.query(), Occur.MUST)
        // Only include docs that have the current hit as parent
        .add(new TermQuery(new Term(field, term)), Occur.MUST)
        // Only include docs that have this inner hits type
        .add(documentMapper.typeFilter(), Occur.MUST)
        .build();
    if (size() == 0) {
        final int count = context.searcher().count(q);
        return new TopDocs(count, Lucene.EMPTY_SCORE_DOCS, 0);
    } else {
        int topN = Math.min(from() + size(), context.searcher().getIndexReader().maxDoc());
        TopDocsCollector topDocsCollector;
        if (sort() != null) {
            topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
        } else {
            topDocsCollector = TopScoreDocCollector.create(topN);
        }
        try {
            context.searcher().search(q, topDocsCollector);
        } finally {
            clearReleasables(Lifetime.COLLECTION);
        }
        return topDocsCollector.topDocs(from(), size());
    }
}
 
Example #18
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
NestedChildrenQuery(BitSetProducer parentFilter, Query childFilter, FetchSubPhase.HitContext hitContext) {
    this.parentFilter = parentFilter;
    this.childFilter = childFilter;
    this.docId = hitContext.docId();
    this.leafReader = hitContext.readerContext().reader();
}
 
Example #19
Source File: FieldDataFieldsParseElement.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected FetchSubPhase.ContextFactory getContextFactory() {
    return FieldDataFieldsFetchSubPhase.CONTEXT_FACTORY;
}
 
Example #20
Source File: SearchContext.java    From Elasticsearch with Apache License 2.0 votes vote down vote up
public abstract  <SubPhaseContext extends FetchSubPhaseContext> SubPhaseContext getFetchSubPhaseContext(FetchSubPhase.ContextFactory<SubPhaseContext> contextFactory); 
Example #21
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 votes vote down vote up
public abstract TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContext) throws IOException;