Search in sources :

Example 1 with SpellCheckCollator

use of org.apache.solr.spelling.SpellCheckCollator in project lucene-solr by apache.

the class SpellCheckComponent method addCollationsToResponse.

@SuppressWarnings("unchecked")
protected void addCollationsToResponse(SolrParams params, SpellingResult spellingResult, ResponseBuilder rb, String q, NamedList response, boolean suggestionsMayOverlap) {
    int maxCollations = params.getInt(SPELLCHECK_MAX_COLLATIONS, 1);
    int maxCollationTries = params.getInt(SPELLCHECK_MAX_COLLATION_TRIES, 0);
    int maxCollationEvaluations = params.getInt(SPELLCHECK_MAX_COLLATION_EVALUATIONS, 10000);
    boolean collationExtendedResults = params.getBool(SPELLCHECK_COLLATE_EXTENDED_RESULTS, false);
    int maxCollationCollectDocs = params.getInt(SPELLCHECK_COLLATE_MAX_COLLECT_DOCS, 0);
    // If not reporting hits counts, don't bother collecting more than 1 document per try.
    if (!collationExtendedResults) {
        maxCollationCollectDocs = 1;
    }
    boolean shard = params.getBool(ShardParams.IS_SHARD, false);
    SpellCheckCollator collator = new SpellCheckCollator().setMaxCollations(maxCollations).setMaxCollationTries(maxCollationTries).setMaxCollationEvaluations(maxCollationEvaluations).setSuggestionsMayOverlap(suggestionsMayOverlap).setDocCollectionLimit(maxCollationCollectDocs);
    List<SpellCheckCollation> collations = collator.collate(spellingResult, q, rb);
    //by sorting here we guarantee a non-distributed request returns all 
    //results in the same order as a distributed request would,
    //even in cases when the internal rank is the same.
    Collections.sort(collations);
    NamedList collationList = new NamedList();
    for (SpellCheckCollation collation : collations) {
        if (collationExtendedResults) {
            NamedList extendedResult = new SimpleOrderedMap();
            extendedResult.add("collationQuery", collation.getCollationQuery());
            extendedResult.add("hits", collation.getHits());
            extendedResult.add("misspellingsAndCorrections", collation.getMisspellingsAndCorrections());
            if (maxCollationTries > 0 && shard) {
                extendedResult.add("collationInternalRank", collation.getInternalRank());
            }
            collationList.add("collation", extendedResult);
        } else {
            collationList.add("collation", collation.getCollationQuery());
            if (maxCollationTries > 0 && shard) {
                collationList.add("collationInternalRank", collation.getInternalRank());
            }
        }
    }
    response.add("collations", collationList);
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SpellCheckCollator(org.apache.solr.spelling.SpellCheckCollator) SpellCheckCollation(org.apache.solr.spelling.SpellCheckCollation) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Aggregations

NamedList (org.apache.solr.common.util.NamedList)1 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)1 SpellCheckCollation (org.apache.solr.spelling.SpellCheckCollation)1 SpellCheckCollator (org.apache.solr.spelling.SpellCheckCollator)1