Search in sources :

Example 1 with SuggesterResult

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

the class SuggestComponent method merge.

/** 
   * Given a list of {@link SuggesterResult} and <code>count</code>
   * returns a {@link SuggesterResult} containing <code>count</code>
   * number of {@link LookupResult}, sorted by their associated 
   * weights
   * */
private static SuggesterResult merge(List<SuggesterResult> suggesterResults, int count) {
    SuggesterResult result = new SuggesterResult();
    Set<String> allTokens = new HashSet<>();
    Set<String> suggesterNames = new HashSet<>();
    // collect all tokens
    for (SuggesterResult shardResult : suggesterResults) {
        for (String suggesterName : shardResult.getSuggesterNames()) {
            allTokens.addAll(shardResult.getTokens(suggesterName));
            suggesterNames.add(suggesterName);
        }
    }
    // Get Top N for every token in every shard (using weights)
    for (String suggesterName : suggesterNames) {
        for (String token : allTokens) {
            Lookup.LookupPriorityQueue resultQueue = new Lookup.LookupPriorityQueue(count);
            for (SuggesterResult shardResult : suggesterResults) {
                List<LookupResult> suggests = shardResult.getLookupResult(suggesterName, token);
                if (suggests == null) {
                    continue;
                }
                for (LookupResult res : suggests) {
                    resultQueue.insertWithOverflow(res);
                }
            }
            List<LookupResult> sortedSuggests = new LinkedList<>();
            Collections.addAll(sortedSuggests, resultQueue.getResults());
            result.add(suggesterName, token, sortedSuggests);
        }
    }
    return result;
}
Also used : SuggesterResult(org.apache.solr.spelling.suggest.SuggesterResult) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) Lookup(org.apache.lucene.search.suggest.Lookup) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 2 with SuggesterResult

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

the class SuggestComponent method process.

/** 
   * Responsible for using the specified suggester to get the suggestions 
   * for the query and write the results 
   * */
@Override
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    LOG.info("SuggestComponent process with : " + params);
    if (!params.getBool(COMPONENT_NAME, false) || suggesters.isEmpty()) {
        return;
    }
    boolean buildAll = params.getBool(SUGGEST_BUILD_ALL, false);
    boolean reloadAll = params.getBool(SUGGEST_RELOAD_ALL, false);
    Set<SolrSuggester> querySuggesters;
    try {
        querySuggesters = getSuggesters(params);
    } catch (SolrException ex) {
        if (!buildAll && !reloadAll) {
            throw ex;
        } else {
            querySuggesters = new HashSet<>();
        }
    }
    String query = params.get(SUGGEST_Q);
    if (query == null) {
        query = rb.getQueryString();
        if (query == null) {
            query = params.get(CommonParams.Q);
        }
    }
    if (query != null) {
        int count = params.getInt(SUGGEST_COUNT, 1);
        boolean highlight = params.getBool(SUGGEST_HIGHLIGHT, false);
        boolean allTermsRequired = params.getBool(SUGGEST_ALL_TERMS_REQUIRED, true);
        String contextFilter = params.get(SUGGEST_CONTEXT_FILTER_QUERY);
        if (contextFilter != null) {
            contextFilter = contextFilter.trim();
            if (contextFilter.length() == 0) {
                contextFilter = null;
            }
        }
        SuggesterOptions options = new SuggesterOptions(new CharsRef(query), count, contextFilter, allTermsRequired, highlight);
        Map<String, SimpleOrderedMap<NamedList<Object>>> namedListResults = new HashMap<>();
        for (SolrSuggester suggester : querySuggesters) {
            SuggesterResult suggesterResult = suggester.getSuggestions(options);
            toNamedList(suggesterResult, namedListResults);
        }
        rb.rsp.add(SuggesterResultLabels.SUGGEST, namedListResults);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SuggesterOptions(org.apache.solr.spelling.suggest.SuggesterOptions) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) CharsRef(org.apache.lucene.util.CharsRef) SuggesterResult(org.apache.solr.spelling.suggest.SuggesterResult) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrSuggester(org.apache.solr.spelling.suggest.SolrSuggester) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet)

Example 3 with SuggesterResult

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

the class SuggestComponent method toSuggesterResult.

/** Convert NamedList (suggester response) to {@link SuggesterResult} */
private SuggesterResult toSuggesterResult(Map<String, SimpleOrderedMap<NamedList<Object>>> suggestionsMap) {
    SuggesterResult result = new SuggesterResult();
    if (suggestionsMap == null) {
        return result;
    }
    // for each token
    for (Map.Entry<String, SimpleOrderedMap<NamedList<Object>>> entry : suggestionsMap.entrySet()) {
        String suggesterName = entry.getKey();
        for (Iterator<Map.Entry<String, NamedList<Object>>> suggestionsIter = entry.getValue().iterator(); suggestionsIter.hasNext(); ) {
            Map.Entry<String, NamedList<Object>> suggestions = suggestionsIter.next();
            String tokenString = suggestions.getKey();
            List<LookupResult> lookupResults = new ArrayList<>();
            NamedList<Object> suggestion = suggestions.getValue();
            // for each suggestion
            for (int j = 0; j < suggestion.size(); j++) {
                String property = suggestion.getName(j);
                if (property.equals(SuggesterResultLabels.SUGGESTIONS)) {
                    @SuppressWarnings("unchecked") List<NamedList<Object>> suggestionEntries = (List<NamedList<Object>>) suggestion.getVal(j);
                    for (NamedList<Object> suggestionEntry : suggestionEntries) {
                        String term = (String) suggestionEntry.get(SuggesterResultLabels.SUGGESTION_TERM);
                        Long weight = (Long) suggestionEntry.get(SuggesterResultLabels.SUGGESTION_WEIGHT);
                        String payload = (String) suggestionEntry.get(SuggesterResultLabels.SUGGESTION_PAYLOAD);
                        LookupResult res = new LookupResult(new CharsRef(term), weight, new BytesRef(payload));
                        lookupResults.add(res);
                    }
                }
                result.add(suggesterName, tokenString, lookupResults);
            }
        }
    }
    return result;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) CharsRef(org.apache.lucene.util.CharsRef) SuggesterResult(org.apache.solr.spelling.suggest.SuggesterResult) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MetricsMap(org.apache.solr.metrics.MetricsMap) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with SuggesterResult

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

the class SuggestComponent method finishStage.

/** 
   * Used in Distributed Search, merges the suggestion results from every shard
   * */
@Override
public void finishStage(ResponseBuilder rb) {
    SolrParams params = rb.req.getParams();
    LOG.info("SuggestComponent finishStage with : " + params);
    if (!params.getBool(COMPONENT_NAME, false) || rb.stage != ResponseBuilder.STAGE_GET_FIELDS)
        return;
    int count = params.getInt(SUGGEST_COUNT, 1);
    List<SuggesterResult> suggesterResults = new ArrayList<>();
    // Collect Shard responses
    for (ShardRequest sreq : rb.finished) {
        for (ShardResponse srsp : sreq.responses) {
            NamedList<Object> resp;
            if ((resp = srsp.getSolrResponse().getResponse()) != null) {
                @SuppressWarnings("unchecked") Map<String, SimpleOrderedMap<NamedList<Object>>> namedList = (Map<String, SimpleOrderedMap<NamedList<Object>>>) resp.get(SuggesterResultLabels.SUGGEST);
                LOG.info(srsp.getShard() + " : " + namedList);
                suggesterResults.add(toSuggesterResult(namedList));
            }
        }
    }
    // Merge Shard responses
    SuggesterResult suggesterResult = merge(suggesterResults, count);
    Map<String, SimpleOrderedMap<NamedList<Object>>> namedListResults = new HashMap<>();
    toNamedList(suggesterResult, namedListResults);
    rb.rsp.add(SuggesterResultLabels.SUGGEST, namedListResults);
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SuggesterResult(org.apache.solr.spelling.suggest.SuggesterResult) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MetricsMap(org.apache.solr.metrics.MetricsMap)

Aggregations

SuggesterResult (org.apache.solr.spelling.suggest.SuggesterResult)4 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 LookupResult (org.apache.lucene.search.suggest.Lookup.LookupResult)2 CharsRef (org.apache.lucene.util.CharsRef)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2 SolrParams (org.apache.solr.common.params.SolrParams)2 NamedList (org.apache.solr.common.util.NamedList)2 MetricsMap (org.apache.solr.metrics.MetricsMap)2 List (java.util.List)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Lookup (org.apache.lucene.search.suggest.Lookup)1 BytesRef (org.apache.lucene.util.BytesRef)1 SolrException (org.apache.solr.common.SolrException)1 SolrSuggester (org.apache.solr.spelling.suggest.SolrSuggester)1