Solr Update插件自定义Update Chain按条件更新索引
背景:基于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
2017.01.12优化:
如下场景时,上面代码会出现问题:
老的数据没有立即commit,还保存在TLog中,此时RealTimeGetComponet.getInputDocument方法获取不到老数据,导致处理逻辑不符合期望,来源时间不正确。
代码优化如下:
SolrInputDocument oldDoc = RealTimeGetComponent.getInputDocumentFromTlog(core, indexedId);
if (oldDoc == null)
{
oldDoc = RealTimeGetComponent.getInputDocument(core, indexedId);
}
Solr Update插件自定义Update Chain按条件更新索引的更多相关文章
- [jQuery]jQuery DataTables插件自定义Ajax分页实现
前言 昨天在博客园的博问上帮一位园友解决了一个问题,我觉得有必要记录一下,万一有人也遇上了呢. 问题描述 园友是做前端的,产品经理要求他使用jQuery DataTables插件显示一个列表,要实现分 ...
- 【JAVA】FOR UPDATE 和 FOR UPDATE NOWAIT 区别 (转)
1.for update 和 for update nowait 的区别:首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何限 ...
- [转]oracle for update和for update nowait的区别
1概念小结:(针对以下引用区域内容) 1.1 普通select语句不加锁. 1.2 for update和for update nowait都试图将符合条件的数据加上行级锁.用于排斥其他针对这个表的写 ...
- Oracle 中 for update 和 for update nowait 的区别
原文出处http://bijian1013.iteye.com/blog/1895412 一.for update 和 for update nowait 的区别 首先一点,如果只是select 的话 ...
- oracle for update和for update nowait
原文地址:http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowa ...
- sql: oracle, for update和for update nowait的区别
1. oracle for update和for update nowait的区别 http://www.cnblogs.com/quanweiru/archive/2012/11/09/276222 ...
- oracle for update和for update nowait(for update wait)的区别
1.for update 和 for update nowait 的区别: 1.oracle 中执行select 操作读取数据不会有任何限制,当另外一个进程在修改表中的数据,但是并没有commit,所 ...
- oracle for update和for update nowait的区别 - 转
1.for update 和 for update nowait 的区别: 首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何 ...
- oracle for update和for update nowait 的区别
原文地址:http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowa ...
随机推荐
- Java 判断中文字符
Java判断一个字符串中是否有中文字符有两种方法,但是原理都一样,就是通过Unicode编码来判断,因为中文在Unicode中的编码区间为:0x4e00--0x9fa5 第一种: String chi ...
- Linux 中的键盘映射【转】
本文转载自:http://hessian.cn/p/144.html [转]Linux 中的键盘映射 原文地址:http://www.linuxidc.com/Linux/2011-04/35197. ...
- bzoj3047:Freda的传呼机&&bzoj2125: 最短路
完结撒花!!!!!!!!!!! 最后一题填坑1A仙人掌WWWWWWW我真流弊 首先把环拆开,环中每一个点连向环的根,然后搞LCA,答案就是套路的d[x]+d[y]-d[lca]*2 然后就可以发现,其 ...
- ListView实现简单列表
ListView实现简单列表 效果图: 啥也没干的ListView张这样: fry.Activity01 package fry; import com.example.ListView.R; imp ...
- Gym - 101981M The 2018 ICPC Asia Nanjing Regional Contest M.Mediocre String Problem Manacher+扩增KMP
题面 题意:给你2个串(长度1e6),在第一个串里找“s1s2s3”,第二个串里找“s4”,拼接后,是一个回文串,求方案数 题解:知道s1和s4回文,s2和s3回文,所以我们枚举s1的右端点,s1的长 ...
- Redis的事务讲解
1. Redis事务的概念 是什么: 可以一次执行多个命令,本质是一组命令的集合.一个事务中的所有命令都会序列化,按顺序串行化的执行而不会被其他命令插入 能干嘛:一个队列中,一次性.顺序性.排他性的执 ...
- 【BZOJ1306】match循环赛
预先警告:我的做法代码量比较大 看完题目后看到数据n<=8, 不难想到这题可以写深搜来做 分析 比如说以数据: 3 3 3 3 为例子, 进行了三场比赛:AB AC BC: 我们只要搜索每场比赛 ...
- UOJ 129/BZOJ 4197 寿司晚宴 状压DP
//By SiriusRen #include <cstdio> #include <algorithm> using namespace std; ; struct Node ...
- 微信图片不可显示java解决方法
先看知乎:https://www.zhihu.com/question/35044484 场景: 微信上传了图片素材,返回了图片url,然后不能在img标签中正常显示. 原因是微信做了图片防盗连接. ...
- 如何写出高性能SQL语句(文章摘自web开发者)
(声明:本文内容摘自web开发者,仅供收藏学习之用,如有侵权请作者联系博主,博主将在第一时间删除) 原文地址:http://www.admin10000.com/document/484.html 1 ...