Solr Update外掛程式自訂Update Chain按條件更新索引

來源:互聯網
上載者:User

標籤: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按條件更新索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.