Example usage for org.apache.solr.spelling SolrSpellChecker mergeSuggestions

List of usage examples for org.apache.solr.spelling SolrSpellChecker mergeSuggestions

Introduction

In this page you can find the example usage for org.apache.solr.spelling SolrSpellChecker mergeSuggestions.

Prototype

public SpellingResult mergeSuggestions(SpellCheckMergeData mergeData, int numSug, int count,
        boolean extendedResults) 

Source Link

Document

Integrate spelling suggestions from the various shards in a distributed environment.

Usage

From source file:org.dice.solrenhancements.spellchecker.DiceSpellCheckComponent.java

License:Apache License

@Override
@SuppressWarnings({ "unchecked", "deprecation" })
public void finishStage(ResponseBuilder rb) {
    SolrParams params = rb.req.getParams();
    if (!params.getBool(COMPONENT_NAME, false) || rb.stage != ResponseBuilder.STAGE_GET_FIELDS)
        return;/*  www.j a  va2 s.  com*/

    boolean extendedResults = params.getBool(SPELLCHECK_EXTENDED_RESULTS, false);
    boolean collate = params.getBool(SPELLCHECK_COLLATE, false);
    boolean collationExtendedResults = params.getBool(SPELLCHECK_COLLATE_EXTENDED_RESULTS, false);
    int maxCollationTries = params.getInt(SPELLCHECK_MAX_COLLATION_TRIES, 0);
    int maxCollations = params.getInt(SPELLCHECK_MAX_COLLATIONS, 1);
    Integer maxResultsForSuggest = params.getInt(SpellingParams.SPELLCHECK_MAX_RESULTS_FOR_SUGGEST);
    int count = rb.req.getParams().getInt(SPELLCHECK_COUNT, 1);
    int numSug = Math.max(count, AbstractLuceneSpellChecker.DEFAULT_SUGGESTION_COUNT);

    String origQuery = params.get(SPELLCHECK_Q);
    if (origQuery == null) {
        origQuery = rb.getQueryString();
        if (origQuery == null) {
            origQuery = params.get(CommonParams.Q);
        }
    }

    long hits = rb.grouping() ? rb.totalHitCount : rb.getNumberDocumentsFound();
    boolean isCorrectlySpelled = hits > (maxResultsForSuggest == null ? 0 : maxResultsForSuggest);

    SpellCheckMergeData mergeData = new SpellCheckMergeData();
    if (maxResultsForSuggest == null || !isCorrectlySpelled) {
        for (ShardRequest sreq : rb.finished) {
            for (ShardResponse srsp : sreq.responses) {
                NamedList nl = (NamedList) srsp.getSolrResponse().getResponse().get("spellcheck");
                LOG.info(srsp.getShard() + " " + nl);
                if (nl != null) {
                    mergeData.totalNumberShardResponses++;
                    collectShardSuggestions(nl, mergeData);
                    collectShardCollations(mergeData, nl, maxCollationTries);
                }
            }
        }
    }

    // all shard responses have been collected
    // create token and get top suggestions
    SolrSpellChecker checker = getSpellChecker(rb.req.getParams());
    SpellingResult result = checker.mergeSuggestions(mergeData, numSug, count, extendedResults);

    NamedList response = new SimpleOrderedMap();
    NamedList suggestions = toNamedList(false, result, origQuery, extendedResults, collate, isCorrectlySpelled);
    if (collate) {
        SpellCheckCollation[] sortedCollations = mergeData.collations.values()
                .toArray(new SpellCheckCollation[mergeData.collations.size()]);
        Arrays.sort(sortedCollations);
        int i = 0;
        while (i < maxCollations && i < sortedCollations.length) {
            SpellCheckCollation collation = sortedCollations[i];
            i++;
            if (collationExtendedResults) {
                NamedList extendedResult = new NamedList();
                extendedResult.add("collationQuery", collation.getCollationQuery());
                extendedResult.add("hits", collation.getHits());
                extendedResult.add("misspellingsAndCorrections", collation.getMisspellingsAndCorrections());
                suggestions.add("collation", extendedResult);
            } else {
                suggestions.add("collation", collation.getCollationQuery());
            }
        }
    }

    response.add("suggestions", suggestions);
    rb.rsp.add("spellcheck", response);
}