solr4.x 实时更新

在新版的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中如果:

         

_version_describe
       >  1新添加的doc中的version必须和索引中已有的相同id的doc中的version保持一致,才能添加成功
      == 1索引中必须有相同id的doc
       < 0 索引中必须没有相同id的doc,即这个doc是新的
     == 0 无论如何都会添加成功,并且系统会给新加的doc的_version_字段赋予一个新值


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文件中定义的)

syntaxdescribe
set新加字段值
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 block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	public SolrServer getSolrServer() {
		return solrServer;
	}
	public void setSolrServer(SolrServer solrServer) {
		this.solrServer = solrServer;
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值