下面列出了org.apache.lucene.search.spell.SuggestMode#org.apache.solr.core.SolrCore 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void inform(SolrCore core) {
if (initParams == null) {
return;
}
log.info("Initializing spell checkers");
if (initParams.getName(0).equals("spellcheckers")) {
Object cfg = initParams.getVal(0);
if (cfg instanceof NamedList) {
addSpellChecker(core, (NamedList) cfg);
} else if (cfg instanceof Map) {
addSpellChecker(core, new NamedList((Map) cfg));
} else if (cfg instanceof List) {
for (Object o : (List) cfg) {
if (o instanceof Map) {
addSpellChecker(core, new NamedList((Map) o));
}
}
}
}
log.info("Spell checker Initialization completed");
}
private String getCollectionFromCoreName(String coreName) {
SolrCore solrCore = null;
try {
if (coreName != null && !coreName.equals("")) {
solrCore = coreContainer.getCore(coreName);
if (solrCore != null) {
return solrCore.getCoreDescriptor().getCloudDescriptor().getCollectionName();
}
}
} finally {
if (solrCore != null) {
solrCore.close();
}
}
return null;
}
final private void cloudDebugLog(SolrCore core, String op) {
if (!log.isDebugEnabled()) {
return;
}
try {
RefCounted<SolrIndexSearcher> searchHolder = core.getNewestSearcher(false);
SolrIndexSearcher searcher = searchHolder.get();
try {
final int totalHits = searcher.count(new MatchAllDocsQuery());
final String nodeName = core.getCoreContainer().getZkController().getNodeName();
log.debug("[{}] {} [{} total hits]", nodeName, op, totalHits);
} finally {
searchHolder.decref();
}
} catch (Exception e) {
log.debug("Error in solrcloud_debug block", e);
}
}
private void loadDefault(String unigramsFile, String bigramsFile, String exclusionsFile,
SpellChecker spellChecker,
SolrCore core, String exclusionListSperatorRegex) {
try {
if (!StringUtils.isEmpty(unigramsFile)) {
loadUniGramFile(core.getResourceLoader().openResource(unigramsFile),
spellChecker.getDataHolder());
}
if (!StringUtils.isEmpty(bigramsFile)) {
loadBiGramFile(core.getResourceLoader().openResource(bigramsFile),
spellChecker.getDataHolder());
}
if (!StringUtils.isEmpty(exclusionsFile)) {
loadExclusions(core.getResourceLoader().openResource(exclusionsFile),
spellChecker.getDataHolder(), exclusionListSperatorRegex);
}
} catch (SpellCheckException | IOException ex) {
log.error("Error occured while loading default Configs for Spellcheck");
}
}
/**
* When [shard] is not in the field list, the [shard] field should not be returned, even
* if score is in the field list.
*/
@Test
public void testNotWantsShardWithScore() throws Exception {
try (SolrCore core = h.getCoreContainer().getCore("merge")) {
ModifiableSolrParams params = new ModifiableSolrParams();
params.add("q", "*:*");
params.add("sort", "letter asc");
params.add("fl", "*,score");
SolrDocumentList docs = queryDocs(core, "merge", params);
assertEquals(3, docs.size());
for (SolrDocument doc : docs) {
assertNull(doc.getFieldValue("[shard]"));
assertEquals(1.0f, doc.getFieldValue("score"));
}
}
}
@Test
public void testBadRequest_missingLocalParams() {
SolrCore core = h.getCore();
ModifiableSolrParams params = new ModifiableSolrParams();
params.add("q", "*:*");
params.add("facet", "true");
params.add("facet.tree", "true");
params.add("facet.tree.field", "node_id");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.add("responseHeader", new SimpleOrderedMap<>());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
SolrRequestHandler handler = core.getRequestHandler(requestHandler);
handler.handleRequest(req, rsp);
req.close();
assertNotNull(rsp.getException());
}
/**
* analyzes string like the given field
* @param field the name of the field
* @param value the string to analyze
* @return the analyzed string
*/
public static String analyzeString(SolrCore core, String field, String value) {
try {
StringBuilder b = new StringBuilder();
try (TokenStream ts = core.getLatestSchema().getFieldType(field).getQueryAnalyzer().tokenStream(field, new StringReader(value))) {
ts.reset();
while (ts.incrementToken()) {
b.append(" ");
CharTermAttribute attr = ts.getAttribute(CharTermAttribute.class);
b.append(attr);
}
}
return b.toString().trim();
} catch (IOException e) {
//FIXME: This error should be properly logged!
e.printStackTrace();
return value;
}
}
public PeerSync.PeerSyncResult sync(ZkController zkController, SolrCore core, ZkNodeProps leaderProps,
boolean peerSyncOnlyWithActive) {
if (SKIP_AUTO_RECOVERY) {
return PeerSync.PeerSyncResult.success();
}
if (isClosed) {
log.warn("Closed, skipping sync up.");
return PeerSync.PeerSyncResult.failure();
}
recoveryRequests.clear();
if (log.isInfoEnabled()) {
log.info("Sync replicas to {}", ZkCoreNodeProps.getCoreUrl(leaderProps));
}
if (core.getUpdateHandler().getUpdateLog() == null) {
log.error("No UpdateLog found - cannot sync");
return PeerSync.PeerSyncResult.failure();
}
return syncReplicas(zkController, core, leaderProps, peerSyncOnlyWithActive);
}
@Before
public void createMonitor() throws Exception {
assumeFalse("This test is not working on Windows (or maybe machines with only 2 CPUs)",
Constants.WINDOWS);
SolrCore core = h.getCore();
updater = (DirectUpdateHandler2) core.getUpdateHandler();
updater.setCommitWithinSoftCommit(true); // foce to default, let tests change as needed
monitor = new MockEventListener();
core.registerNewSearcherListener(monitor);
updater.registerSoftCommitCallback(monitor);
updater.registerCommitCallback(monitor);
// isolate searcher getting ready from this test
monitor.searcher.poll(5000, MILLISECONDS);
}
@Override
public RandomTestDictionary create(SolrCore core, SolrIndexSearcher searcher) {
if(params == null) {
// should not happen; implies setParams was not called
throw new IllegalStateException("Value of params not set");
}
String name = (String)params.get(CommonParams.NAME);
if (name == null) { // Shouldn't happen since this is the component name
throw new IllegalArgumentException(CommonParams.NAME + " is a mandatory parameter");
}
long maxItems = DEFAULT_MAX_ITEMS;
Object specifiedMaxItems = params.get(RAND_DICT_MAX_ITEMS);
if (specifiedMaxItems != null) {
maxItems = Long.parseLong(specifiedMaxItems.toString());
}
return new RandomTestDictionary(name, maxItems);
}
@SuppressWarnings({"rawtypes"})
public void inform(SolrCore core) {
String defaultCollection;
String defaultZkhost;
CoreContainer coreContainer = core.getCoreContainer();
this.solrClientCache = coreContainer.getSolrClientCache();
this.coreName = core.getName();
String cacheKey = this.getClass().getName() + "_" + coreName + "_";
this.objectCache = coreContainer.getObjectCache().computeIfAbsent(cacheKey + "objectCache",
ConcurrentHashMap.class, k-> new ConcurrentHashMap());
if (coreContainer.isZooKeeperAware()) {
defaultCollection = core.getCoreDescriptor().getCollectionName();
defaultZkhost = core.getCoreContainer().getZkController().getZkServerAddress();
streamFactory.withCollectionZkHost(defaultCollection, defaultZkhost);
streamFactory.withDefaultZkHost(defaultZkhost);
modelCache = coreContainer.getObjectCache().computeIfAbsent(cacheKey + "modelCache",
ModelCache.class,
k -> new ModelCache(250, defaultZkhost, solrClientCache));
}
streamFactory.withSolrResourceLoader(core.getResourceLoader());
// This pulls all the overrides and additions from the config
addExpressiblePlugins(streamFactory, core);
}
private void reloadCore(boolean createNewCore) throws Exception {
if (createNewCore) {
CoreContainer cores = h.getCoreContainer();
SolrCore core = h.getCore();
String dataDir1 = core.getDataDir();
CoreDescriptor cd = core.getCoreDescriptor();
h.close();
createCore();
SolrCore createdCore = h.getCore();
assertEquals(dataDir1, createdCore.getDataDir());
assertEquals(createdCore, h.getCore());
} else {
h.reload();
// On regular reloading, wait until the new searcher is registered
waitForWarming();
}
assertQ(req("qt", "/select",
"q", "*:*"),
"//*[@numFound='11']"
);
}
@Override
public Dictionary create(SolrCore core, SolrIndexSearcher searcher) {
if(params == null) {
// should not happen; implies setParams was not called
throw new IllegalStateException("Value of params not set");
}
String field = (String)params.get(SolrSpellChecker.FIELD);
if (field == null) {
throw new IllegalArgumentException(SolrSpellChecker.FIELD + " is a mandatory parameter");
}
float threshold = params.get(THRESHOLD_TOKEN_FREQUENCY) == null ? 0.0f
: (Float)params.get(THRESHOLD_TOKEN_FREQUENCY);
return new HighFrequencyDictionary(searcher.getIndexReader(), field, threshold);
}
private void waitForReplicasCatchUp(int numTry) throws IOException, InterruptedException {
String leaderTimeCommit = getSolrCore(true).get(0).getDeletionPolicy().getLatestCommit().getUserData().get(SolrIndexWriter.COMMIT_TIME_MSEC_KEY);
if (leaderTimeCommit == null) return;
for (int i = 0; i < numTry; i++) {
boolean inSync = true;
for (SolrCore solrCore : getSolrCore(false)) {
String replicateTimeCommit = solrCore.getDeletionPolicy().getLatestCommit().getUserData().get(SolrIndexWriter.COMMIT_TIME_MSEC_KEY);
if (!leaderTimeCommit.equals(replicateTimeCommit)) {
inSync = false;
Thread.sleep(500);
break;
}
}
if (inSync) return;
}
fail("Some replicas are not in sync with leader");
}
protected ReplicaListTransformer getReplicaListTransformer(final SolrQueryRequest req) {
final SolrParams params = req.getParams();
final SolrCore core = req.getCore(); // explicit check for null core (temporary?, for tests)
@SuppressWarnings("resource")
ZkController zkController = core == null ? null : core.getCoreContainer().getZkController();
if (zkController != null) {
return requestReplicaListTransformerGenerator.getReplicaListTransformer(
params,
zkController.getZkStateReader().getClusterProperties()
.getOrDefault(ZkStateReader.DEFAULT_SHARD_PREFERENCES, "")
.toString(),
zkController.getNodeName(),
zkController.getBaseUrl(),
zkController.getSysPropsCacher()
);
} else {
return requestReplicaListTransformerGenerator.getReplicaListTransformer(params);
}
}
protected void processDeleteById(final String chain, String id) throws IOException {
SolrCore core = h.getCore();
UpdateRequestProcessorChain pc = core.getUpdateProcessingChain(chain);
assertNotNull("No Chain named: " + chain, pc);
SolrQueryResponse rsp = new SolrQueryResponse();
SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());
DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
cmd.setId(id);
UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
try {
processor.processDelete(cmd);
} finally {
req.close();
}
}
private LocalSolrQueryRequest makeRequest(SolrCore core, String solrQuery,
List<String> fqs, String df) {
final NamedList<String> returnList = new NamedList<String>();
if ((solrQuery != null) && !solrQuery.isEmpty()) {
returnList.add(CommonParams.Q, solrQuery);
}
if (fqs != null) {
for (final String fq : fqs) {
returnList.add(CommonParams.FQ, fq);
}
}
if ((df != null) && !df.isEmpty()) {
returnList.add(CommonParams.DF, df);
}
if (returnList.size() > 0) {
return new LocalSolrQueryRequest(core, returnList);
} else {
return null;
}
}
@Test
public void testFailNonIndexedSigWithOverwriteDupes() throws Exception {
SolrCore core = h.getCore();
SignatureUpdateProcessorFactory f = new SignatureUpdateProcessorFactory();
NamedList<String> initArgs = new NamedList<>();
initArgs.add("overwriteDupes", "true");
initArgs.add("signatureField", "signatureField_sS");
f.init(initArgs);
boolean exception_ok = false;
try {
f.inform(core);
} catch (Exception e) {
exception_ok = true;
}
assertTrue("Should have gotten an exception from inform(SolrCore)",
exception_ok);
}
@Override
public void tearDown() throws Exception {
String configDir;
try (SolrCore core = h.getCoreContainer().getCore(collection)) {
configDir = core.getResourceLoader().getConfigDir();
}
super.tearDown();
if (new File(configDir, "stopwords.txt.bak").exists()) {
FileUtils.deleteQuietly(new File(configDir, "stopwords.txt"));
FileUtils.moveFile(new File(configDir, "stopwords.txt.bak"), new File(configDir, "stopwords.txt"));
}
}
private void parseFieldType(SolrCore core) throws InitializationException {
String a = initArgs.get(FIELD_TYPE);
if (a != null) {
FieldType ft = core.getLatestSchema().getFieldTypes().get(a);
if (ft == null) {
throw new InitializationException("Parameter " + FIELD_TYPE + " defines an unknown field type \"" + a + "\"", InitializationExceptionCause.UNKNOWN_FIELD_TYPE);
}
queryAnalyzer = ft.getQueryAnalyzer();
}
}
/**
* Code copied from {@link org.apache.solr.util.TestHarness#query(String, SolrQueryRequest)} because it is not
* <code>static</code> there (it could have been) and we do not have an instance of {@link org.apache.solr.util.TestHarness}.
*/
private static String localQuery(String handler, SolrQueryRequest req) throws Exception {
try {
SolrCore core = req.getCore();
SolrQueryResponse rsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
core.execute(core.getRequestHandler(handler),req,rsp); // TODO the core doesn't have the request handler
if (rsp.getException() != null) {
throw rsp.getException();
}
QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
if (responseWriter instanceof BinaryQueryResponseWriter) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(32000);
BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) responseWriter;
writer.write(byteArrayOutputStream, req, rsp);
return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
} else {
StringWriter sw = new StringWriter(32000);
responseWriter.write(sw,req,rsp);
return sw.toString();
}
} finally {
req.close();
SolrRequestInfo.clearRequestInfo();
}
}
@Override
public void inform(SolrCore core) {
solrResourceLoader = core.getResourceLoader();
for (TypeMapping typeMapping : typeMappings) {
typeMapping.populateValueClasses(core);
}
}
private void openNewSearcherAndUpdateCommitPoint() throws IOException {
RefCounted<SolrIndexSearcher> searcher = null;
IndexCommit commitPoint;
// must get the latest solrCore object because the one we have might be closed because of a reload
// todo stop keeping solrCore around
SolrCore core = solrCore.getCoreContainer().getCore(solrCore.getName());
try {
@SuppressWarnings({"rawtypes"})
Future[] waitSearcher = new Future[1];
searcher = core.getSearcher(true, true, waitSearcher, true);
if (waitSearcher[0] != null) {
try {
waitSearcher[0].get();
} catch (InterruptedException | ExecutionException e) {
SolrException.log(log, e);
}
}
commitPoint = searcher.get().getIndexReader().getIndexCommit();
} finally {
if (searcher != null) {
searcher.decref();
}
core.close();
}
// update the commit point in replication handler
replicationHandler.indexCommitPoint = commitPoint;
}
@Override
public void inform(final SolrCore core) {
selector =
FieldMutatingUpdateProcessor.createFieldNameSelector
(core.getResourceLoader(), core, inclusions, getDefaultSelector(core));
for (SelectorParams exc : exclusions) {
selector = FieldMutatingUpdateProcessor.wrap
(selector,
FieldMutatingUpdateProcessor.createFieldNameSelector
(core.getResourceLoader(), core, exc, FieldMutatingUpdateProcessor.SELECT_NO_FIELDS));
}
}
private void openNewSearcher(SolrCore core) throws Exception {
@SuppressWarnings({"rawtypes"})
Future[] waitSearcher = new Future[1];
core.getSearcher(true, false, waitSearcher, true);
if (waitSearcher[0] != null) {
waitSearcher[0].get();
}
}
private void reloadSuggester(SolrSuggester suggester, SolrCore core,
SolrIndexSearcher searcher) throws IOException {
File storeFile = suggester.getStoreFile();
if (storeFile == null || !storeFile.exists()) {
suggester.build(core, searcher);
} else {
suggester.reload(core, searcher);
}
}
@Override
public List<JATETerm> extract(SolrCore core, String jatePropertyFile)
throws IOException, JATEException {
JATEProperties properties = getJateProperties(jatePropertyFile);
return extract(core, properties);
}
@Override
public Lookup create(@SuppressWarnings({"rawtypes"})NamedList params, SolrCore core) {
boolean exactMatchFirst = params.get(EXACT_MATCH_FIRST) != null
? Boolean.valueOf(params.get(EXACT_MATCH_FIRST).toString())
: true;
return new WFSTCompletionLookup(getTempDir(), "suggester", exactMatchFirst);
}
public SuggesterListener(SolrCore core, SolrSuggester checker, boolean buildOnCommit, boolean buildOnOptimize, boolean buildOnStartup, boolean isCoreReload) {
this.core = core;
this.suggester = checker;
this.buildOnCommit = buildOnCommit;
this.buildOnOptimize = buildOnOptimize;
this.buildOnStartup = buildOnStartup;
this.isCoreReload = isCoreReload;
}
@Override
public List<JATETerm> rankingAndFiltering(SolrCore core, String jatePropertyFile, Map<String, String> params,
Algorithm algorithm) throws IOException, JATEException {
for (TermRecognitionProcessor termRecognitionProcessor : processors) {
List<JATETerm> terms = termRecognitionProcessor.rankingAndFiltering(core, jatePropertyFile, params, algorithm);
if (terms != null) {
return terms;
}
}
return null;
}