solr4.x 实时更新
来源:互联网 发布:主题医院 mac 编辑:程序博客网 时间:2024/05/16 04:42
在新版的solr中实时更新功能较之前更加强大。
demo 测试版本为:
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-core</artifactId> <version>4.2.1</version></dependency>
solrConfig配置文件中配置:
<updateHandler class="solr.DirectUpdateHandler2"> <!-- <updateLog class="solr.FSUpdateLog"> --> <updateLog> <str name="dir">${solr.core1.data.dir:}</str> </updateLog> </updateHandler> <requestHandler name="/get" class="solr.RealTimeGetHandler"> </requestHandler> <requestHandler name="/update" class="solr.UpdateRequestHandler" />
一、
其中,_version_ 字段是关键所在,在新添加的doc中如果:
A、 添加一个新doc
Administrator@JJRV1KWGH4PCCW2 /cygdrive/d$ curl '127.0.0.1:8989/auction/update ' -H 'Content-type:application/json' -d '[{ "auction_id" : "6000", "title" : " test realtime add ", "b_id" : "23", "_version_" : "-1"}]'{"responseHeader":{"status":0,"QTime":0}}查询:
http://127.0.0.1:8989/auction/get?id=6000&wt=xml&indent=true
返回结果:
使用一般的查询:
http://127.0.0.1:8989/auction/select?q=auction_id:6000&wt=xml&indent=true这个查询是没有结果的,因为新加的索引是在内存中,没有添加到旧的索引中。
B、更新一个旧的doc(更新b_id字段)
Administrator@JJRV1KWGH4PCCW2 /cygdrive/d$ curl '127.0.0.1:8989/auction/update ' -H 'Content-type:application/json' -d '[{ "auction_id" : "6000", "title" : " test realtime add ", "b_id" : "10", "_version_" : "1441160408488476672"}]'{"responseHeader":{"status":0,"QTime":0}}
查询返回结果:
C、 _version_ 字段设置为 0 (数值零) 或者不设置在任何情况均可成功
Administrator@JJRV1KWGH4PCCW2 /cygdrive/d$ curl '127.0.0.1:8989/auction/update ' -H 'Content-type:application/json' -d '[{ "auction_id" : "6000", "title" : " test realtime add ,test the version==0 ", "b_id" : "10", "_version_" : "0"}]'{"responseHeader":{"status":0,"QTime":0}}Administrator@JJRV1KWGH4PCCW2 /cygdrive/d$ curl '127.0.0.1:8989/auction/update ' -H 'Content-type:application/json' -d '[{ "auction_id" : "6000", "title" : " test realtime add ,test the version==null ", "b_id" : "10"}]'{"responseHeader":{"status":0,"QTime":0}}
二、新版的solr还支持,指定字段更新或者向旧doc中添加新的字段(对应字段必须是schema文件中定义的)
syntaxdescribeset新加字段值add添加新的值,到一个多值字段inc 数值字段增加例子如下:
b_id字段值增加 20 , 新加字段 quantity,
$ curl '127.0.0.1:8989/auction/update ' -H 'Content-type:application/json' -d '> [{> "auction_id" : "6000",> "title" : " test realtime add ,test change filed And add new filed ",> "b_id" : {"inc":20},> "quantity" : {"set":100},> "_version_" : "0"> }]'{"responseHeader":{"status":0,"QTime":0}}
添加一个多值字段front_category_1:
$ curl '127.0.0.1:8989/auction/update ' -H 'Content-type:application/json' -d '[{ "auction_id" : "6000", "front_category_1" : {"set":10000}}]'{"responseHeader":{"status":0,"QTime":0}}
添加新的值,到一个多值字段front_category_1:
Administrator@JJRV1KWGH4PCCW2 /cygdrive/d$ curl '127.0.0.1:8989/auction/update ' -H 'Content-type:application/json' -d '[{ "auction_id" : "6000", "front_category_1" : {"add":20000}}]'{"responseHeader":{"status":0,"QTime":0}}
最后的搜索结果如下:
http://127.0.0.1:8989/auction/get?id=6000&wt=xml&indent=true
http://127.0.0.1:8989/auction/update?optimize=true&maxSegments=10&waitFlush=false
将实时写的索引合并到旧的索引中。
x写索引并立即提交:
$ curl '127.0.0.1:8989/realtime/update?commit=true ' -H 'Content-type:application/json' -d '[{ "auction_id" : "99978", "b_id" : "22", "_version_" : "-1", "title" : "testing"}]'
使用 commitWithin=10000 写索引,过10秒钟再提交:
$ curl '127.0.0.1:8989/realtime/update?commitWithin=1000 ' -H 'Content-type:application/json' -d '[{ "auction_id" : "9966", "b_id" : "22", "_version_" : "-1", "title" : "testing"}]'
http://127.0.0.1:8989/realtime/select?q=*:*&indent=on
java演示程序
package com.netboy.demo.client;import java.io.IOException;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.common.SolrInputDocument;public class AddDocDemo {SolrServer solrServer ;public static void main(String[] args) {SolrServer solr = new HttpSolrServer("http://localhost:8989/ram_1");AddDocDemo demo = new AddDocDemo();demo.setSolrServer(solr);try {demo.addDoc(33L, 33.2,0, "测试添加--3");} catch (Exception e) {e.printStackTrace();}System.out.println("add doc over...");demo.close();System.exit(0);}public SolrInputDocument createDoc(long auction_id, double discount, long _version_, String title) {SolrInputDocument vid = new SolrInputDocument(); vid.addField("auction_id", auction_id); vid.addField("discount", discount); vid.addField("_version_", _version_); vid.addField("title", title); return vid; } public void addDoc( long auction_id, double discount, long _version_, String title) throws Exception { solrServer.add(createDoc(auction_id, discount, _version_, title)); Thread.sleep(200); } public void close(){ try {solrServer.commit();solrServer.shutdown();} catch (SolrServerException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }public SolrServer getSolrServer() {return solrServer;}public void setSolrServer(SolrServer solrServer) {this.solrServer = solrServer;}}
- solr4.x 实时更新
- solr4.x之原子更新
- Solrcloud/solr4.0/solr4.2/solr4.3/solr4.x (实时搜索)NRT及commit 相关问题
- solr4.7.1定时实时重建索引和增量更新
- Tomcat整合配置Solr4.x
- solr4.x+tomcat+中文分词
- 我所理解的Cocos2d-x 实时更新游戏对象
- solr4.9原子更新solrj例子
- solr4.9原子更新solrj例子
- SolrPhpClient 支持solr4.x 的问题
- solr4.x设置默认查询字段
- Solr4.X的安装和使用
- lucene的实时更新
- 时间实时更新
- E文(实时更新)
- 实时更新异地数据库
- 实时更新 (网络数据)
- datagridview Checkbox实时更新
- 面试题16:树的子结构
- Ubuntu12.04安装进固态硬盘SSD中,体验神速启动
- Linux下的python开发
- Linux下的动态共享库(so)开发调用
- java异常在业务中的处理
- solr4.x 实时更新
- 单词缩写
- 面试题17:二叉树的镜像
- 【算法或技巧】两数交换--不用第三个数
- 浅析人脸检测之Haar分类器方法
- Android Textbox Example--EditText 文本编辑框
- 面试题18:顺时针打印矩阵
- MySQL数据导出导入的文章
- 7.21----html