Search in sources :

Example 1 with UnifiedHighlighter

use of org.apache.lucene.search.uhighlight.UnifiedHighlighter in project lucene-solr by apache.

the class TestUnifiedHighlighterExtensibility method testUnifiedHighlighterExtensibility.

/**
   * This test is for maintaining the extensibility of the UnifiedHighlighter
   * for customizations out of package.
   */
@Test
public void testUnifiedHighlighterExtensibility() {
    final int maxLength = 1000;
    UnifiedHighlighter uh = new UnifiedHighlighter(null, new MockAnalyzer(random())) {

        @Override
        protected Map<String, Object[]> highlightFieldsAsObjects(String[] fieldsIn, Query query, int[] docIdsIn, int[] maxPassagesIn) throws IOException {
            return super.highlightFieldsAsObjects(fieldsIn, query, docIdsIn, maxPassagesIn);
        }

        @Override
        protected OffsetSource getOffsetSource(String field) {
            return super.getOffsetSource(field);
        }

        @Override
        protected BreakIterator getBreakIterator(String field) {
            return super.getBreakIterator(field);
        }

        @Override
        protected PassageScorer getScorer(String field) {
            return super.getScorer(field);
        }

        @Override
        protected PassageFormatter getFormatter(String field) {
            return super.getFormatter(field);
        }

        @Override
        public Analyzer getIndexAnalyzer() {
            return super.getIndexAnalyzer();
        }

        @Override
        public IndexSearcher getIndexSearcher() {
            return super.getIndexSearcher();
        }

        @Override
        protected int getMaxNoHighlightPassages(String field) {
            return super.getMaxNoHighlightPassages(field);
        }

        @Override
        protected Boolean requiresRewrite(SpanQuery spanQuery) {
            return super.requiresRewrite(spanQuery);
        }

        @Override
        protected LimitedStoredFieldVisitor newLimitedStoredFieldsVisitor(String[] fields) {
            return super.newLimitedStoredFieldsVisitor(fields);
        }

        @Override
        protected List<CharSequence[]> loadFieldValues(String[] fields, DocIdSetIterator docIter, int cacheCharsThreshold) throws IOException {
            return super.loadFieldValues(fields, docIter, cacheCharsThreshold);
        }

        @Override
        protected FieldHighlighter getFieldHighlighter(String field, Query query, Set<Term> allTerms, int maxPassages) {
            // THIS IS A COPY of the superclass impl; but use CustomFieldHighlighter
            BytesRef[] terms = filterExtractedTerms(getFieldMatcher(field), allTerms);
            Set<HighlightFlag> highlightFlags = getFlags(field);
            PhraseHelper phraseHelper = getPhraseHelper(field, query, highlightFlags);
            CharacterRunAutomaton[] automata = getAutomata(field, query, highlightFlags);
            OffsetSource offsetSource = getOptimizedOffsetSource(field, terms, phraseHelper, automata);
            return new CustomFieldHighlighter(field, getOffsetStrategy(offsetSource, field, terms, phraseHelper, automata, highlightFlags), new SplittingBreakIterator(getBreakIterator(field), UnifiedHighlighter.MULTIVAL_SEP_CHAR), getScorer(field), maxPassages, getMaxNoHighlightPassages(field), getFormatter(field));
        }

        @Override
        protected FieldOffsetStrategy getOffsetStrategy(OffsetSource offsetSource, String field, BytesRef[] terms, PhraseHelper phraseHelper, CharacterRunAutomaton[] automata, Set<HighlightFlag> highlightFlags) {
            return super.getOffsetStrategy(offsetSource, field, terms, phraseHelper, automata, highlightFlags);
        }

        @Override
        public int getMaxLength() {
            return maxLength;
        }
    };
    assertEquals(uh.getMaxLength(), maxLength);
}
Also used : Set(java.util.Set) Query(org.apache.lucene.search.Query) SpanQuery(org.apache.lucene.search.spans.SpanQuery) CharacterRunAutomaton(org.apache.lucene.util.automaton.CharacterRunAutomaton) PhraseHelper(org.apache.lucene.search.uhighlight.PhraseHelper) SpanQuery(org.apache.lucene.search.spans.SpanQuery) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) UnifiedHighlighter(org.apache.lucene.search.uhighlight.UnifiedHighlighter) SplittingBreakIterator(org.apache.lucene.search.uhighlight.SplittingBreakIterator) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 2 with UnifiedHighlighter

use of org.apache.lucene.search.uhighlight.UnifiedHighlighter in project lucene-solr by apache.

the class UnifiedSolrHighlighter method doHighlighting.

@Override
public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest req, String[] defaultFields) throws IOException {
    final SolrParams params = req.getParams();
    // if highlighting isn't enabled, then why call doHighlighting?
    if (!isHighlightingEnabled(params))
        return null;
    int[] docIDs = toDocIDs(docs);
    // fetch the unique keys
    String[] keys = getUniqueKeys(req.getSearcher(), docIDs);
    // query-time parameters
    String[] fieldNames = getHighlightFields(query, req, defaultFields);
    int[] maxPassages = new int[fieldNames.length];
    for (int i = 0; i < fieldNames.length; i++) {
        maxPassages[i] = params.getFieldInt(fieldNames[i], HighlightParams.SNIPPETS, 1);
    }
    UnifiedHighlighter highlighter = getHighlighter(req);
    Map<String, String[]> snippets = highlighter.highlightFields(fieldNames, query, docIDs, maxPassages);
    return encodeSnippets(keys, fieldNames, snippets);
}
Also used : UnifiedHighlighter(org.apache.lucene.search.uhighlight.UnifiedHighlighter) SolrParams(org.apache.solr.common.params.SolrParams)

Aggregations

UnifiedHighlighter (org.apache.lucene.search.uhighlight.UnifiedHighlighter)2 Set (java.util.Set)1 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 Query (org.apache.lucene.search.Query)1 SpanQuery (org.apache.lucene.search.spans.SpanQuery)1 PhraseHelper (org.apache.lucene.search.uhighlight.PhraseHelper)1 SplittingBreakIterator (org.apache.lucene.search.uhighlight.SplittingBreakIterator)1 BytesRef (org.apache.lucene.util.BytesRef)1 CharacterRunAutomaton (org.apache.lucene.util.automaton.CharacterRunAutomaton)1 SolrParams (org.apache.solr.common.params.SolrParams)1 Test (org.junit.Test)1