Example usage for org.apache.solr.spelling SpellCheckCollation getInternalRank

List of usage examples for org.apache.solr.spelling SpellCheckCollation getInternalRank

Introduction

In this page you can find the example usage for org.apache.solr.spelling SpellCheckCollation getInternalRank.

Prototype

public int getInternalRank() 

Source Link

Usage

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

License:Apache License

@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;//ww  w  . j a  v a2 s .  co m
    }
    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);

    for (SpellCheckCollation collation : collations) {
        if (collationExtendedResults) {
            NamedList extendedResult = new NamedList();
            extendedResult.add("collationQuery", collation.getCollationQuery());
            extendedResult.add("hits", collation.getHits());
            extendedResult.add("misspellingsAndCorrections", collation.getMisspellingsAndCorrections());
            if (maxCollationTries > 0 && shard) {
                extendedResult.add("collationInternalRank", collation.getInternalRank());
            }
            response.add("collation", extendedResult);
        } else {
            response.add("collation", collation.getCollationQuery());
            if (maxCollationTries > 0 && shard) {
                response.add("collationInternalRank", collation.getInternalRank());
            }
        }
    }
}

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

License:Apache License

@SuppressWarnings("unchecked")
private void collectShardCollations(SpellCheckMergeData mergeData, NamedList spellCheckResponse,
        int maxCollationTries) {
    Map<String, SpellCheckCollation> collations = mergeData.collations;
    NamedList suggestions = (NamedList) spellCheckResponse.get("suggestions");
    if (suggestions != null) {
        List<Object> collationList = suggestions.getAll("collation");
        List<Object> collationRankList = suggestions.getAll("collationInternalRank");
        int i = 0;
        if (collationList != null) {
            for (Object o : collationList) {
                if (o instanceof String) {
                    SpellCheckCollation coll = new SpellCheckCollation();
                    coll.setCollationQuery((String) o);
                    if (collationRankList != null && collationRankList.size() > 0) {
                        coll.setInternalRank((Integer) collationRankList.get(i));
                        i++;//from  w  ww.  j a  v a2s.  co  m
                    }
                    SpellCheckCollation priorColl = collations.get(coll.getCollationQuery());
                    if (priorColl != null) {
                        coll.setInternalRank(Math.max(coll.getInternalRank(), priorColl.getInternalRank()));
                    }
                    collations.put(coll.getCollationQuery(), coll);
                } else {
                    NamedList expandedCollation = (NamedList) o;
                    SpellCheckCollation coll = new SpellCheckCollation();
                    coll.setCollationQuery((String) expandedCollation.get("collationQuery"));
                    coll.setHits((Integer) expandedCollation.get("hits"));
                    if (maxCollationTries > 0) {
                        coll.setInternalRank((Integer) expandedCollation.get("collationInternalRank"));
                    }
                    coll.setMisspellingsAndCorrections(
                            (NamedList) expandedCollation.get("misspellingsAndCorrections"));
                    SpellCheckCollation priorColl = collations.get(coll.getCollationQuery());
                    if (priorColl != null) {
                        coll.setHits(coll.getHits() + priorColl.getHits());
                        coll.setInternalRank(Math.max(coll.getInternalRank(), priorColl.getInternalRank()));
                    }
                    collations.put(coll.getCollationQuery(), coll);
                }
            }
        }
    }
}