1. 程式人生 > >Solr部署配置(三)實時檢索介面

Solr部署配置(三)實時檢索介面

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. 內容不記得從哪裡拷的了~~