Example usage for org.apache.lucene.search.vectorhighlight FastVectorHighlighter setPhraseLimit

List of usage examples for org.apache.lucene.search.vectorhighlight FastVectorHighlighter setPhraseLimit

Introduction

In this page you can find the example usage for org.apache.lucene.search.vectorhighlight FastVectorHighlighter setPhraseLimit.

Prototype

public void setPhraseLimit(int phraseLimit) 

Source Link

Document

set the maximum number of phrases to analyze when searching for the highest-scoring phrase.

Usage

From source file:org.apache.solr.handler.component.AlfrescoSolrHighlighter.java

License:Open Source License

/**
 * Generates a list of Highlighted query fragments for each item in a list
 * of documents, or returns null if highlighting is disabled.
 *
 * @param docs/*from ww w .  j  a  v a  2  s. c o m*/
 *            query results
 * @param query
 *            the query
 * @param req
 *            the current request
 * @param defaultFields
 *            default list of fields to summarize
 *
 * @return NamedList containing a NamedList for each document, which in
 *         turns contains sets (field, summary) pairs.
 */
@Override
@SuppressWarnings("unchecked")
public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest req, String[] defaultFields)
        throws IOException {
    SolrParams params = req.getParams();
    if (!isHighlightingEnabled(params)) // also returns early if no unique
        // key field
        return null;

    boolean rewrite = query != null
            && !(Boolean.valueOf(params.get(HighlightParams.USE_PHRASE_HIGHLIGHTER, "true"))
                    && Boolean.valueOf(params.get(HighlightParams.HIGHLIGHT_MULTI_TERM, "true")));

    if (rewrite) {
        query = query.rewrite(req.getSearcher().getIndexReader());
    }

    SolrIndexSearcher searcher = req.getSearcher();
    IndexSchema schema = searcher.getSchema();

    // fetch unique key if one exists.
    SchemaField keyField = schema.getUniqueKeyField();
    if (keyField == null) {
        return null;// exit early; we need a unique key field to populate
                    // the response
    }

    String[] fieldNames = getHighlightFields(query, req, defaultFields);

    Set<String> preFetchFieldNames = getDocPrefetchFieldNames(fieldNames, req);
    if (preFetchFieldNames != null) {
        preFetchFieldNames.add(keyField.getName());
    }

    FastVectorHighlighter fvh = null; // lazy
    FieldQuery fvhFieldQuery = null; // lazy

    IndexReader reader = new TermVectorReusingLeafReader(req.getSearcher().getSlowAtomicReader()); // SOLR-5855

    // Highlight each document
    NamedList fragments = new SimpleOrderedMap();
    DocIterator iterator = docs.iterator();
    for (int i = 0; i < docs.size(); i++) {
        int docId = iterator.nextDoc();
        Document doc = getDocument(searcher.doc(docId, preFetchFieldNames), req);

        @SuppressWarnings("rawtypes")
        NamedList docHighlights = new SimpleOrderedMap();
        // Highlight per-field
        for (String fieldName : fieldNames) {
            String schemaFieldName = AlfrescoSolrDataModel.getInstance().mapProperty(fieldName,
                    FieldUse.HIGHLIGHT, req);

            // rewrite field specific parameters .....
            SchemaField schemaField = schema.getFieldOrNull(schemaFieldName);
            rewriteRequestParameters(params, fieldName, schemaFieldName, req);

            Object fieldHighlights; // object type allows flexibility for
            // subclassers
            if (schemaField == null) {
                fieldHighlights = null;
            } else if (schemaField.getType() instanceof org.apache.solr.schema.TrieField) {
                // TODO: highlighting numeric fields is broken (Lucene) - so
                // we disable them until fixed (see LUCENE-3080)!
                fieldHighlights = null;
            } else if (useFastVectorHighlighter(req.getParams(), schemaField)) {
                if (fvhFieldQuery == null) {
                    fvh = new FastVectorHighlighter(
                            // FVH cannot process hl.usePhraseHighlighter parameter
                            // per-field basis
                            req.getParams().getBool(HighlightParams.USE_PHRASE_HIGHLIGHTER, true),
                            // FVH cannot process hl.requireFieldMatch
                            // parameter per-field basis
                            req.getParams().getBool(HighlightParams.FIELD_MATCH, false));
                    fvh.setPhraseLimit(req.getParams().getInt(HighlightParams.PHRASE_LIMIT,
                            SolrHighlighter.DEFAULT_PHRASE_LIMIT));
                    fvhFieldQuery = fvh.getFieldQuery(query, reader);
                }
                fieldHighlights = null;

                FvhContainer fvhContainer = new FvhContainer(fvh, fvhFieldQuery);

                fieldHighlights = doHighlightingByFastVectorHighlighter(doc, docId, schemaField, fvhContainer,
                        reader, req);
            } else { // standard/default highlighter
                fieldHighlights = doHighlightingByHighlighter(doc, docId, schemaField, query, reader, req);
                // Fall back to the best FTS field if highlight fails
                if (fieldHighlights == null) {
                    schemaFieldName = AlfrescoSolrDataModel.getInstance().mapProperty(fieldName,
                            FieldUse.HIGHLIGHT, req, 1);
                    if (schemaField != null) {
                        schemaField = schema.getFieldOrNull(schemaFieldName);
                        rewriteRequestParameters(params, fieldName, schemaFieldName, req);
                        fieldHighlights = doHighlightingByHighlighter(doc, docId, schemaField, query, reader,
                                req);
                    }
                }
            }

            if (fieldHighlights == null) {
                // no summaries made; copy text from alternate field
                fieldHighlights = alternateField(doc, fieldName, req);
            }

            if (fieldHighlights != null) {
                docHighlights.add(fieldName, fieldHighlights);
            }
        } // for each field
        if (doc.get("DBID") != null) {
            docHighlights.add("DBID", doc.get("DBID"));
        }
        fragments.add(schema.printableUniqueKey(doc), docHighlights);
    } // for each doc
    return fragments;
}

From source file:org.apache.solr.highlight.DefaultSolrHighlighter.java

License:Apache License

/**
 * Generates a list of Highlighted query fragments for each item in a list
 * of documents, or returns null if highlighting is disabled.
 *
 * @param docs query results//from w  w  w . jav a 2  s. c  o m
 * @param query the query
 * @param req the current request
 * @param defaultFields default list of fields to summarize
 *
 * @return NamedList containing a NamedList for each document, which in 
 * turns contains sets (field, summary) pairs.
 */
@Override
@SuppressWarnings("unchecked")
public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest req, String[] defaultFields)
        throws IOException {
    SolrParams params = req.getParams();
    if (!isHighlightingEnabled(params))
        return null;

    SolrIndexSearcher searcher = req.getSearcher();
    IndexSchema schema = searcher.getSchema();
    NamedList fragments = new SimpleOrderedMap();
    String[] fieldNames = getHighlightFields(query, req, defaultFields);
    Set<String> fset = new HashSet<String>();

    {
        // pre-fetch documents using the Searcher's doc cache
        for (String f : fieldNames) {
            fset.add(f);
        }
        // fetch unique key if one exists.
        SchemaField keyField = schema.getUniqueKeyField();
        if (null != keyField)
            fset.add(keyField.getName());
    }

    // get FastVectorHighlighter instance out of the processing loop
    FastVectorHighlighter fvh = new FastVectorHighlighter(
            // FVH cannot process hl.usePhraseHighlighter parameter per-field basis
            params.getBool(HighlightParams.USE_PHRASE_HIGHLIGHTER, true),
            // FVH cannot process hl.requireFieldMatch parameter per-field basis
            params.getBool(HighlightParams.FIELD_MATCH, false));
    fvh.setPhraseLimit(params.getInt(HighlightParams.PHRASE_LIMIT, Integer.MAX_VALUE));
    FieldQuery fieldQuery = fvh.getFieldQuery(query, searcher.getIndexReader());

    // Highlight each document
    DocIterator iterator = docs.iterator();
    for (int i = 0; i < docs.size(); i++) {
        int docId = iterator.nextDoc();
        Document doc = searcher.doc(docId, fset);
        NamedList docSummaries = new SimpleOrderedMap();
        for (String fieldName : fieldNames) {
            fieldName = fieldName.trim();
            if (useFastVectorHighlighter(params, schema, fieldName))
                doHighlightingByFastVectorHighlighter(fvh, fieldQuery, req, docSummaries, docId, doc,
                        fieldName);
            else
                doHighlightingByHighlighter(query, req, docSummaries, docId, doc, fieldName);
        }
        String printId = schema.printableUniqueKey(doc);
        fragments.add(printId == null ? null : printId, docSummaries);
    }
    return fragments;
}