我确实实现了一个 Solr 插件,目的是更新 Solr 事件(提交、优化等)的某些字段。我的问题是更新的文档不会影响 Solr 搜索,直到我手动尝试重新启动 Solr。请您看看我的代码并告诉我是什么部分导致了这个问题?
问候。
boolean success = false;
NamedList response = new SimpleOrderedMap();
keyword.init(searcher, params);
BooleanQuery query = new BooleanQuery();
for (String fieldName : keywordSourceFields) {
TermQuery termQuery = new TermQuery(new Term(fieldName, "noval"));
query.add(termQuery, Occur.MUST_NOT);
}
TermQuery termQuery = new TermQuery(new Term(keywordField, "noval"));
query.add(termQuery, Occur.MUST);
RefCounted<IndexWriter> iw = null;
IndexWriter writer = null;
try {
TopDocs results = searcher.search(query, maxNumDocs);
ScoreDoc[] hits = results.scoreDocs;
iw = solrCoreState.getIndexWriter(core);
writer = iw.get();
FieldType type = new FieldType(StringField.TYPE_STORED);
for (int i = 0; i < hits.length; i++) {
Document document = searcher.doc(hits[i].doc);
List<String> keywords = keyword.getKeywords(hits[i].doc);
if (keywords.size() > 0) document.removeFields(keywordField);
for (String word : keywords) {
document.add(new Field(keywordField, word, type));
}
String uniqueKey = searcher.getSchema().getUniqueKeyField().getName();
writer.updateDocument(new Term(uniqueKey, document.get(uniqueKey)),
document);
}
response.add("Number of Selected Docs", results.totalHits);
writer.commit();
} catch (IOException | SyntaxError e) {
throw new RuntimeException();
} finally {
if (iw != null) {
iw.decref();
}
}