標籤:next lib ring str jar ati except long efault
背景:基於call客,來電和跟進記錄等多個資料來源的使用者文檔,需要在更新是判斷首來源的時間。
如對電話號碼11xxxx來說,來電時間是今天,call客時間是昨天,而call客資料又可能因為網路原因晚上傳上來,這樣一來11xxxx這個使用者document的來源時間需要更新成昨天。
分析:solr的預設update沒有辦法匹配業務的靈活的更新邏輯。更新邏輯如下,當更新來源時間的時候,如果新的來源時間比之前的來源時間晚,則保持之前的來源時間。
代碼實現:
package custom.solr;import java.io.IOException;import org.apache.lucene.util.BytesRef;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.core.SolrCore;import org.apache.solr.handler.component.RealTimeGetComponent;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.SolrQueryResponse;import org.apache.solr.search.SolrIndexSearcher;import org.apache.solr.update.AddUpdateCommand;import org.apache.solr.update.processor.UpdateRequestProcessor;import org.apache.solr.update.processor.UpdateRequestProcessorFactory;import org.apache.solr.util.RefCounted;public class ConditionUpdateProcessFactory extends UpdateRequestProcessorFactory{ @Override public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) { return new ConditionalUpdateProcessor(req, rsp, next); }}class ConditionalUpdateProcessor extends UpdateRequestProcessor{ public static final String ORIGIN_TIMESTAMP = "origin_timestamp"; public ConditionalUpdateProcessor(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) { super(next); core = req.getCore(); } private final SolrCore core; @Override public void processAdd(AddUpdateCommand cmd) throws IOException { SolrInputDocument newDoc = cmd.getSolrInputDocument(); BytesRef indexedId = cmd.getIndexedId(); RefCounted<SolrIndexSearcher> newestSearcher = core.getRealtimeSearcher(); SolrIndexSearcher searcher; long lookup; searcher = (SolrIndexSearcher) newestSearcher.get(); lookup = searcher.lookupId(indexedId); //if not exists if (lookup < 0) { super.processAdd(cmd); } SolrInputDocument oldDoc = RealTimeGetComponent.getInputDocument(core, indexedId); Object newOriginTimestamp = newDoc.getFieldValue(ORIGIN_TIMESTAMP); Object oldOriginTimestamp = oldDoc.getFieldValue(ORIGIN_TIMESTAMP); if (newOriginTimestamp != null && oldOriginTimestamp != null) { if (Long.valueOf(oldOriginTimestamp.toString()) < Long.valueOf(newOriginTimestamp.toString())) { newDoc.setField(ORIGIN_TIMESTAMP, oldOriginTimestamp); } } // pass it up the chain super.processAdd(cmd); }}
1.將該類編譯後產生jar包放到 /var/lib/solr/plugins目錄下,或者你任意指定一個目錄。
2.配置solrconfig.xml載入該jar包。(注意修改jar包或者solrconfig.xml之後要reload collection)
<lib dir="/var/lib/solr/plugins" />
3.配置solrconfig.xml的預設update用哪個chain名字。
<requestHandler name="/update" class="solr.UpdateRequestHandler"> <!-- See below for information on defining updateRequestProcessorChains that can be used by name on each Update Request --> <lst name="defaults"> <str name="update.chain">condition</str> </lst> </requestHandler>
以及solrconfig.xml chain的流程。
<updateRequestProcessorChain name="condition"> <processor class="solr.LogUpdateProcessorFactory" /> <processor class="solr.DistributedUpdateProcessorFactory" /> <processor class="custom.solr.ConditionUpdateProcessFactory" /> <processor class="solr.RunUpdateProcessorFactory" /> </updateRequestProcessorChain>
*關於為什麼放在DistibutedUpdateProcessFactory之後。
https://wiki.apache.org/solr/Atomic_Updates
Solr Update外掛程式自訂Update Chain按條件更新索引