Solr是一个拥有象WebService一样接口的独立运行的搜索服务器。它基于restful风格,并且扩展了Lucene.能够通过HTTP协议以XML格式将文档放入搜索服务器(索引),你能够通过HTTP协议的GET来查询搜索服务器并且得到XML格式的结果。
REST并不是一个协议或技术;它是一种体系结构风格。
REST 是 SOAP 的轻量型替代品,它是面向资源的,而不是面向操作的。它常常被归结为远程过程使用 HTTP 调用 GET、POST、PUT 和 DELETE 语句。
REST比较简单朴素,在安全性,可靠消息传输,或标准化的业务过程自动化上还没有定义。
一、通过http传递xml格式数据
<add>
<doc>
<field name="employeeId">05991</field>
<field name="office">Bridgewater</field>
<field name="skills">Perl</field>
<field name="skills">Java</field>
</doc>
[<doc> ... </doc>[<doc> ... </doc>]]
</add>
<delete><id>05991</id></delete>
<delete><query>office:Bridgewater</query></delete>
http://localhost:8983/solr/update?commit=true -H "Content-Type: text/xml" --data-binary '<add><doc><field name="id">testdoc</field></doc></add>'
http://localhost:8081/solr/update/?stream.body=<add><doc><field name="id">1</field><field name="author">bbb</field></doc></add>&commit=true
二、通过http传递json格式数据
{
add: {
"doc": {
"id": "DOC1",
"my_boosted_field": { /* use a map with boost/value for a boosted field */
"boost": 2.3,
"value": "test"
},
"my_multivalued_field": [ "aaa", "bbb" ] /* use an array for a multi-valued field */
}
},
add: {
"commitWithin": 5000, /* commit this document within 5 seconds */
"overwrite": false, /* don't check for existing documents with the same uniqueKey */
"boost": 3.45, /* a document boost */
"doc": {
"f1": "v1",
"f1": "v2"
}
},
delete: { "id":"ID" }, /* delete by ID */
delete: { "query":"QUERY" } /* delete by query */
delete: { "query":"QUERY", 'commitWithin':'500' } /* delete by query, commit within 500ms */
}
http://localhost:8983/solr/update -H 'Content-type:application/json' -d '
{
"id" : "TestDoc1",
"title" : {"set":"test1"},
"revision" : {"inc":3},
"publisher" : {"add":"TestPublisher"}
}'
http://localhost:8081/solr/update/?stream.body={"add": {"doc":{"id":1, "author":"ccc"}}}&commit=true
查询
http://localhost:8983/solr/select?q=name:monsters&wt=json&indent=true
{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"indent":"true",
"wt":"json",
"q":"title:monsters"}},
"response":{"numFound":1,"start":0,"docs":[
{
"id":"978-1423103349",
"author":"Rick Riordan",
"series_t":"Percy Jackson and the Olympians",
"sequence_i":2,
"genre_s":"fantasy",
"inStock":true,
"price":6.49,
"pages_i":304,
"name":[
"The Sea of Monsters"],
"cat":["book","paperback"]}]
}}
三、通过java API(Solrj)访问
1、建立与Solr服务的连接
String url = "http://localhost:8983/solr";
CommonsHttpSolrServer server = new CommonsHttpSolrServer(url);
server.setSoTimeout(3000); // socket read timeout
server.setConnectionTimeout(1000);
server.setDefaultMaxConnectionsPerHost(1000);
server.setMaxTotalConnections(10);
server.setFollowRedirects(false); // defaults to false
server.setAllowCompression(true);
server.setMaxRetries(1);
2、搜索条件的设置
SolrQuery query = new SolrQuery();
query.setQuery("tags:t5 AND t7");
query.setStart(0);
query.setRows(4);
query.addSortField("auction_id", SolrQuery.ORDER.desc);
query.addSortField("auction_point", SolrQuery.ORDER.asc);
3、结果集的获取
Solrj提供的注解的方式,注入pojo
import org.apache.solr.client.solrj.beans.Field;
public class Item {
@Field
String id;
@Field("tags")
String[] tag;
@Field
List<String> features;
}
调用
查询
QueryResponse qrsp = solrServer.query(query);
List<Item> productIds = qrsp.getBeans(Item.class);
更新索引
solrServer.addBean(obj);
PS. 内容不记得从哪里拷的了~~