Search in sources :

Example 1 with CheckedSupplier

use of org.opensearch.common.CheckedSupplier in project OpenSearch by opensearch-project.

the class UnifiedHighlighter method highlight.

@Override
public HighlightField highlight(FieldHighlightContext fieldContext) throws IOException {
    @SuppressWarnings("unchecked") Map<String, CustomUnifiedHighlighter> cache = (Map<String, CustomUnifiedHighlighter>) fieldContext.cache.computeIfAbsent(UnifiedHighlighter.class.getName(), k -> new HashMap<>());
    if (cache.containsKey(fieldContext.fieldName) == false) {
        cache.put(fieldContext.fieldName, buildHighlighter(fieldContext));
    }
    CustomUnifiedHighlighter highlighter = cache.get(fieldContext.fieldName);
    MappedFieldType fieldType = fieldContext.fieldType;
    SearchHighlightContext.Field field = fieldContext.field;
    FetchSubPhase.HitContext hitContext = fieldContext.hitContext;
    CheckedSupplier<String, IOException> loadFieldValues = () -> {
        List<Object> fieldValues = loadFieldValues(highlighter, fieldContext.context.getQueryShardContext(), fieldType, hitContext, fieldContext.forceSource);
        if (fieldValues.size() == 0) {
            return null;
        }
        return mergeFieldValues(fieldValues, MULTIVAL_SEP_CHAR);
    };
    Snippet[] fieldSnippets = highlighter.highlightField(hitContext.reader(), hitContext.docId(), loadFieldValues);
    if (fieldSnippets == null || fieldSnippets.length == 0) {
        return null;
    }
    List<Snippet> snippets = new ArrayList<>(fieldSnippets.length);
    for (Snippet fieldSnippet : fieldSnippets) {
        if (Strings.hasText(fieldSnippet.getText())) {
            snippets.add(fieldSnippet);
        }
    }
    if (snippets.isEmpty()) {
        return null;
    }
    if (field.fieldOptions().scoreOrdered()) {
        // let's sort the snippets by score if needed
        CollectionUtil.introSort(snippets, (o1, o2) -> Double.compare(o2.getScore(), o1.getScore()));
    }
    String[] fragments = new String[snippets.size()];
    for (int i = 0; i < fragments.length; i++) {
        fragments[i] = snippets.get(i).getText();
    }
    return new HighlightField(fieldContext.fieldName, Text.convertFromStringArray(fragments));
}
Also used : IdFieldMapper(org.opensearch.index.mapper.IdFieldMapper) HashMap(java.util.HashMap) CustomPassageFormatter(org.apache.lucene.search.uhighlight.CustomPassageFormatter) Strings(org.opensearch.common.Strings) ArrayList(java.util.ArrayList) CheckedSupplier(org.opensearch.common.CheckedSupplier) Locale(java.util.Locale) Snippet(org.apache.lucene.search.uhighlight.Snippet) Map(java.util.Map) CustomUnifiedHighlighter(org.apache.lucene.search.uhighlight.CustomUnifiedHighlighter) BoundedBreakIteratorScanner(org.apache.lucene.search.uhighlight.BoundedBreakIteratorScanner) CustomSeparatorBreakIterator(org.apache.lucene.search.uhighlight.CustomSeparatorBreakIterator) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) HitContext(org.opensearch.search.fetch.FetchSubPhase.HitContext) MULTIVAL_SEP_CHAR(org.apache.lucene.search.uhighlight.CustomUnifiedHighlighter.MULTIVAL_SEP_CHAR) FetchSubPhase(org.opensearch.search.fetch.FetchSubPhase) OffsetSource(org.apache.lucene.search.uhighlight.UnifiedHighlighter.OffsetSource) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Predicate(java.util.function.Predicate) Analyzer(org.apache.lucene.analysis.Analyzer) TextSearchInfo(org.opensearch.index.mapper.TextSearchInfo) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) CollectionUtil(org.apache.lucene.util.CollectionUtil) List(java.util.List) Encoder(org.apache.lucene.search.highlight.Encoder) BreakIterator(java.text.BreakIterator) QueryShardContext(org.opensearch.index.query.QueryShardContext) PassageFormatter(org.apache.lucene.search.uhighlight.PassageFormatter) Text(org.opensearch.common.text.Text) IndexSearcher(org.apache.lucene.search.IndexSearcher) HashMap(java.util.HashMap) CustomUnifiedHighlighter(org.apache.lucene.search.uhighlight.CustomUnifiedHighlighter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Snippet(org.apache.lucene.search.uhighlight.Snippet) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) FetchSubPhase(org.opensearch.search.fetch.FetchSubPhase) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HitContext(org.opensearch.search.fetch.FetchSubPhase.HitContext)

Aggregations

IOException (java.io.IOException)1 BreakIterator (java.text.BreakIterator)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Encoder (org.apache.lucene.search.highlight.Encoder)1 BoundedBreakIteratorScanner (org.apache.lucene.search.uhighlight.BoundedBreakIteratorScanner)1 CustomPassageFormatter (org.apache.lucene.search.uhighlight.CustomPassageFormatter)1 CustomSeparatorBreakIterator (org.apache.lucene.search.uhighlight.CustomSeparatorBreakIterator)1 CustomUnifiedHighlighter (org.apache.lucene.search.uhighlight.CustomUnifiedHighlighter)1 MULTIVAL_SEP_CHAR (org.apache.lucene.search.uhighlight.CustomUnifiedHighlighter.MULTIVAL_SEP_CHAR)1 PassageFormatter (org.apache.lucene.search.uhighlight.PassageFormatter)1 Snippet (org.apache.lucene.search.uhighlight.Snippet)1 OffsetSource (org.apache.lucene.search.uhighlight.UnifiedHighlighter.OffsetSource)1