org.apache.solr.handler.component.SuggestComponent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-core Show documentation
Show all versions of solr-core Show documentation
Apache Solr (module: core)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.handler.component;
import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.lucene.search.suggest.Lookup;
import org.apache.lucene.search.suggest.Lookup.LookupResult;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.Accountables;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.CharsRef;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrEventListener;
import org.apache.solr.metrics.MetricsMap;
import org.apache.solr.metrics.SolrMetricProducer;
import org.apache.solr.metrics.SolrMetricsContext;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.spelling.suggest.SolrSuggester;
import org.apache.solr.spelling.suggest.SuggesterOptions;
import org.apache.solr.spelling.suggest.SuggesterParams;
import org.apache.solr.spelling.suggest.SuggesterResult;
import org.apache.solr.util.plugin.SolrCoreAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* SuggestComponent: interacts with multiple {@link SolrSuggester} to serve up suggestions
* Responsible for routing commands and queries to the appropriate {@link SolrSuggester}
* and for initializing them as specified by SolrConfig
*/
public class SuggestComponent extends SearchComponent implements SolrCoreAware, SuggesterParams, Accountable, SolrMetricProducer {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
/** Name used to identify whether the user query concerns this component */
public static final String COMPONENT_NAME = "suggest";
/** Name assigned to an unnamed suggester (at most one suggester) can be unnamed */
private static final String DEFAULT_DICT_NAME = SolrSuggester.DEFAULT_DICT_NAME;
/** SolrConfig label to identify Config time settings */
private static final String CONFIG_PARAM_LABEL = "suggester";
/** SolrConfig label to identify boolean value to build suggesters on commit */
private static final String BUILD_ON_COMMIT_LABEL = "buildOnCommit";
/** SolrConfig label to identify boolean value to build suggesters on optimize */
private static final String BUILD_ON_OPTIMIZE_LABEL = "buildOnOptimize";
/** SolrConfig label to identify boolean value to build suggesters on startup */
private static final String BUILD_ON_STARTUP_LABEL = "buildOnStartup";
@SuppressWarnings({"rawtypes"})
protected NamedList initParams;
protected SolrMetricsContext metricsContext;
/**
* Key is the dictionary name used in SolrConfig, value is the corresponding {@link SolrSuggester}
*/
protected Map suggesters = new ConcurrentHashMap<>();
/** Container for various labels used in the responses generated by this component */
private static class SuggesterResultLabels {
static final String SUGGEST = "suggest";
static final String SUGGESTIONS = "suggestions";
static final String SUGGESTION_NUM_FOUND = "numFound";
static final String SUGGESTION_TERM = "term";
static final String SUGGESTION_WEIGHT = "weight";
static final String SUGGESTION_PAYLOAD = "payload";
}
@Override
@SuppressWarnings("unchecked")
public void init(@SuppressWarnings({"rawtypes"})NamedList args) {
super.init(args);
this.initParams = args;
}
@Override
public void inform(SolrCore core) {
if (initParams != null) {
log.info("Initializing SuggestComponent");
boolean hasDefault = false;
for (int i = 0; i < initParams.size(); i++) {
if (initParams.getName(i).equals(CONFIG_PARAM_LABEL)) {
@SuppressWarnings({"rawtypes"})
NamedList suggesterParams = (NamedList) initParams.getVal(i);
SolrSuggester suggester = new SolrSuggester();
String dictionary = suggester.init(suggesterParams, core);
if (dictionary != null) {
boolean isDefault = dictionary.equals(DEFAULT_DICT_NAME);
if (isDefault && !hasDefault) {
hasDefault = true;
} else if (isDefault){
throw new RuntimeException("More than one dictionary is missing name.");
}
suggesters.put(dictionary, suggester);
} else {
if (!hasDefault){
suggesters.put(DEFAULT_DICT_NAME, suggester);
hasDefault = true;
} else {
throw new RuntimeException("More than one dictionary is missing name.");
}
}
boolean buildOnStartup;
Object buildOnStartupObj = suggesterParams.get(BUILD_ON_STARTUP_LABEL);
if (buildOnStartupObj == null) {
File storeFile = suggester.getStoreFile();
buildOnStartup = storeFile == null || !storeFile.exists();
} else {
buildOnStartup = Boolean.parseBoolean((String) buildOnStartupObj);
}
boolean buildOnCommit = Boolean.parseBoolean((String) suggesterParams.get(BUILD_ON_COMMIT_LABEL));
boolean buildOnOptimize = Boolean.parseBoolean((String) suggesterParams.get(BUILD_ON_OPTIMIZE_LABEL));
if (buildOnCommit || buildOnOptimize || buildOnStartup) {
SuggesterListener listener = new SuggesterListener(core, suggester, buildOnCommit, buildOnOptimize, buildOnStartup, core.isReloaded());
if (log.isInfoEnabled()) {
log.info("Registering searcher listener for suggester: {} = {}", suggester.getName(), listener);
}
core.registerFirstSearcherListener(listener);
core.registerNewSearcherListener(listener);
}
}
}
}
}
/** Responsible for issuing build and rebuild command to the specified {@link SolrSuggester} */
@Override
public void prepare(ResponseBuilder rb) throws IOException {
SolrParams params = rb.req.getParams();
log.info("SuggestComponent prepare with : {}", params);
if (!params.getBool(COMPONENT_NAME, false)) {
return;
}
boolean buildAll = params.getBool(SUGGEST_BUILD_ALL, false);
boolean reloadAll = params.getBool(SUGGEST_RELOAD_ALL, false);
final Collection querysuggesters;
if (buildAll || reloadAll) {
querysuggesters = suggesters.values();
} else {
querysuggesters = getSuggesters(params);
}
if (params.getBool(SUGGEST_BUILD, false) || buildAll) {
for (SolrSuggester suggester : querysuggesters) {
suggester.build(rb.req.getCore(), rb.req.getSearcher());
}
rb.rsp.add("command", (!buildAll) ? "build" : "buildAll");
} else if (params.getBool(SUGGEST_RELOAD, false) || reloadAll) {
for (SolrSuggester suggester : querysuggesters) {
suggester.reload(rb.req.getCore(), rb.req.getSearcher());
}
rb.rsp.add("command", (!reloadAll) ? "reload" : "reloadAll");
}
}
/** Dispatch shard request in STAGE_EXECUTE_QUERY
stage */
@Override
public int distributedProcess(ResponseBuilder rb) {
SolrParams params = rb.req.getParams();
log.info("SuggestComponent distributedProcess with : {}", params);
if (rb.stage < ResponseBuilder.STAGE_EXECUTE_QUERY)
return ResponseBuilder.STAGE_EXECUTE_QUERY;
if (rb.stage == ResponseBuilder.STAGE_EXECUTE_QUERY) {
ShardRequest sreq = new ShardRequest();
sreq.purpose = ShardRequest.PURPOSE_GET_TOP_IDS;
sreq.params = new ModifiableSolrParams(rb.req.getParams());
sreq.params.remove(ShardParams.SHARDS);
rb.addRequest(this, sreq);
return ResponseBuilder.STAGE_GET_FIELDS;
}
return ResponseBuilder.STAGE_DONE;
}
/**
* 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 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>> namedListResults =
new HashMap<>();
for (SolrSuggester suggester : querySuggesters) {
SuggesterResult suggesterResult = suggester.getSuggestions(options);
toNamedList(suggesterResult, namedListResults);
}
rb.rsp.add(SuggesterResultLabels.SUGGEST, namedListResults);
}
}
/**
* 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 suggesterResults = new ArrayList<>();
// Collect Shard responses
for (ShardRequest sreq : rb.finished) {
for (ShardResponse srsp : sreq.responses) {
NamedList