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;
}
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);
}
}
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;
}
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);
}
Aggregations