使用solrj操做solr索引庫,solr是lucene服務器

客戶端開發 Solrj

客戶端開發 Solrjhtml

   Solr是搭建好的lucene服務器 固然不可能徹底知足通常的業務需求 可能 要針對各類的架構和業務調整 這裏就須要用到Solrj了java

Solrj是Solr提供的api庫 能夠進行Solr 的二次開發。web

   主要實現原理 服務端利用httpClient發送Solr格式的HTTP請求 到Solr服務器  而後服務器根據請求去找索引( 固然你要先針對數據源建立索引)最後返回SolrDocument. 而後你再進一步處理 高亮 排序 等功能。數據庫

   1. 建立鏈接apache

       HttpSolrServer solrServer  = SolrServiceConfiger.getInstance().getServer();api

   //這裏建立的一個HTTP請求 用的4.1的JAR Solr3.6以前用的CommonHttpSolrServer.新版改爲這個類了 建立一個單例服務。服務器

   2. 建立索引架構

     /***socket

* 建立Solr索引 經過指定的實體ide

    */

public void create(T t) {

SolrEntityBinder binder = new SolrEntityBinder();//這裏這個類是源碼DocumentObjectBinder處理類 這裏爲了實體轉換改寫了裏面一些判斷後面會講到 剩下沒改過

SolrInputDocument doc = binder.toSolrInputDocument(t); //把你的實體對象轉換成Solr輸入文檔對象

try {

solrServer.add(doc);//添加文檔

solrServer.commit();//提交請求

} catch (SolrServerException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

  3 .刪除索引

   /**

* 刪除指定ID的索引

*/

public void delete(PK id) {

try {

solrServer.deleteById((String)id );//直接根據ID刪除 solrServer.deleteByQuery("*:*");刪除全部   

solrServer.commit();

} catch (SolrServerException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

  4.修改索引

   直接先刪除後建立

  5.查詢索引

   public Page<Demo> luceneSearch(String content,Page<Demo> page) {

String id = null;

try {

HttpSolrServer server = SolrServiceConfiger.getInstance().getServer();// 建立服務 */

SolrQuery sQuery = new SolrQuery();  // 建立solr查詢對象

String para="";  

if(StringUtils.isNotEmpty(content)){ // 拼接solr查詢條件

para ="company:"+content;

}

if(!StringUtils.isNotEmpty(para)){  

para="*:*";   

}

sQuery.setQuery(para)

.setStart((page.getPageNo()-1)*page.getPageSize())

.setRows(page.getPageSize());  

//設置高亮  

       sQuery.setHighlight(true); // 開啓高亮組件  

       sQuery.addHighlightField("company");// 高亮字段  

       sQuery.setHighlightSimplePre("<font color='red'>");//標記,高亮關鍵字前綴  

       sQuery.setHighlightSimplePost("</font>");//後綴  

       sQuery.setHighlightSnippets(2);//結果分片數,默認爲1  

       sQuery.setHighlightFragsize(1000);//每一個分片的最大長度,默認爲100  

       sQuery.setFacet(true).setFacetMinCount(1)

                       .setFacetLimit(5)//段  

              .addFacetField("company");//分片字段  

         //返回結果

       QueryResponse response = server.query(sQuery);  

         //獲得文檔列表

       SolrDocumentList list = response.getResults();  

        //獲得總數 設置

                Integer counts=(int) list.getNumFound();               

       page.setTotalCount(counts);

       //獲得高亮數據

       Map<String,Map<String,List<String>>> highlightMap=response.getHighlighting(); //獲得高亮集合

        //建立對象轉換對象

       DocumentObjectBinder binder = new DocumentObjectBinder();  

        //轉換對象

       List<Demo> demoList= binder.getBeans(Demo.class, list);

        //設置高亮結果

       for(Demo d :demoList){

       id = d.getId();

       List<String> companyList = highlightMap.get(id).get("company");         

       if(companyList!=null&&companyList.size()>0){

       d.setCompany(companyList.get(0));

       }        

       }

       //返回結果

       page.setResult(demoList);

      

} catch (Exception e) {

e.printStackTrace();

}  

          return page;

}  

常見問題

1. org.apache.solr.client.solrj.SolrServerException:

   Timeout occured while waiting response from server at: http://localhost/solr請求超時

解決方法:設置setConnectionTimeout和setSoTimeout爲1分鐘

2. org.apache.solr.common.SolrException: undefined field text

解決方法: 設置solrConfig.xml <str name="df">id</str> 默認是text

3. org.apache.solr.common.SolrException: Invalid Date String時間格式化問題

主要是Solr使用的是標準的格林威治(GMT)時間 這種(yyyy-MM-dd'T'HH:mm:ss.SSS'Z')北京在東八區 默認時間會-8小時

因此爲了知足他這個減8 個人作法是在建立索引格式化日期類型的時候 判斷下 把它時間+8 。

因此從新建立一個SolrEntityBinder類 此類是徹底copy的DocumentObjectBinder. 只修改了一處。

修改了類中toSolrInputDocument方法

                               //判斷是不是日期-------------///

if(field.type==Date.class){

Date d = (Date)field.get(obj);

Calendar cal = Calendar.getInstance();

cal.setTimeInMillis(d.getTime());

cal.add(Calendar.HOUR, 8);

d = cal.getTime();

doc.setField(field.name,d,1.0f);

}else{

doc.setField(field.name, field.get(obj), 1.0f);

}

 --------------------------------------------

 (solrj)初次使用solr的開發人員老是很鬱悶,不知道如何去操做solr索引庫,覺得只能用《五分鐘solr4.5教程(搭建、運行)》中講到的用xml文件的形式提交數據到索引庫,其實沒有那麼麻煩,solr配套有好多的客戶端用於操做索引庫,下面咱們來說如何用solrj去操做solr索引庫。

1、認識solrj

solrj是solr的java客戶端,用於訪問solr索引庫。它提供了添加、刪除、查詢、優化等功能。

 

2、下載

          百度、google如下solrj下載,你會發現根本就沒有,那麼咱們該到哪兒下載呢?其實,它是集成到solr壓縮包裏的,解壓文件後,有個目錄/dist/solrj-lib,裏面就存放了solrj所用到的jar,你把這些jar都添加到你的classpath就ok。

若是你是使用Maven來構建項目,添加如下代碼到你的pom.xml配置文件中:

<dependency> 
        <artifactId>solr-solrj</artifactId> 
        <groupId>org.apache.solr</groupId> 
        <version>1.4.0</version> 
        <type>jar</type>
        <scope>compile</scope> 
</dependency>
<dependency> 
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.5.6</version> 
</dependency>

 

3、建立SolrServer

一、HttpSolrServer

solrj是採用http請求的方式去訪問solr索引庫的,首先咱們得先建立一個HttpSolrServer,以下:

String url = "http://localhost:8983/solr";//8983是web服務器的端口號,須要根據狀況進行調整
SolrServer server = new HttpSolrServer( url );

以上使用默認的方式建立server,你也能夠配置其餘的鏈接參數:

String url = "http://localhost:8983/solr" ;
HttpSolrServer server = new HttpSolrServer( url ); server.setMaxRetries(1); // defaults to 0. > 1 not recommended. server.setConnectionTimeout(5000); // 5 seconds to establish TCP 
// Setting the XML response parser is only required for cross
 // version compatibility and only when one side is 1.4.1 or 
// earlier and the other side is 3.1 or later. server.setParser(new XMLResponseParser()); 
// binary parser is used by default 
// The following settings are provided here for completeness. 
// They will not normally be required, and should only be used
 // after consulting javadocs to know whether they are truly required.
server.setSoTimeout(1000); // socket read timeout 
server.setDefaultMaxConnectionsPerHost(100); 
server.setMaxTotalConnections(100); 
server.setFollowRedirects(false); // defaults to false 
// allowCompression defaults to false. 
// Server side must support gzip or deflate for this to have any effect. 
server.setAllowCompression(true);

二、EmbeddedSolrServer

EmbeddedSolrServer提供了和HttpSolrServer同樣的接口,區別是EmbeddedSolrServer沒有經過http鏈接。

//Note that the following property could be set through JVM level arguments too
System.setProperty("solr.solr.home", "d:/solr_home/solr"); //此處配置solr home,根據本身的狀況修改
CoreContainer.Initializer initializer = new CoreContainer.Initializer(); 
CoreContainer coreContainer = initializer.initialize(); 
EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");

固然實際中可能不止一個core,那麼採用以下方式:

File home = new File( "d:/solr_home/solr" );//此處配置solr home,根據本身的狀況修改
File f = new File( home, "solr.xml" );

CoreContainer container = new CoreContainer();

container.load( "d:/solr_home/solr", f );
  EmbeddedSolrServer server = new EmbeddedSolrServer( container, "core name as defined in solr.xml" );//雙引號配置你的core名字 ...

在嵌入式的應用中使用solr,推薦使用EmbeddedSolrServer。

注意:EmbeddedSolrServer使用有個前提條件,須要在solrconfig.xml配置相對應的RequestHandler,如:/update對應更新操做。

 

四:solrj的使用

建立好server之後,咱們就能夠開始solrj操做索引庫了!!

首先建立一個server,最好的方式是採用單例模式,不要重複去建立。

SolrServer server = new HttpSolrServer("http://HOST:8983/solr/");

一、添加文檔到索引庫

//建立一個文檔

SolrInputDocument doc1 = new SolrInputDocument(); 
doc1.addField( "id", "id1", 1.0f ); 
doc1.addField( "name", "doc1", 1.0f );
doc1.addField( "price", 10 );

//再建立一個文檔

SolrInputDocument doc2 = new SolrInputDocument(); 
doc2.addField( "id", "id2", 1.0f );
 doc2.addField( "name", "doc2", 1.0f );
 doc2.addField( "price", 20 );

//建立一個collection,把以上兩個文檔添加進去

Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
 docs.add( doc2 );

//把文檔添加到server中

server.add( docs );

//提交文檔

server.commit();//此處只是提交到索引中,不會出如今搜索結果中;若是想立馬搜索,請使用commit(boolean waitFlush, boolean waitSearcher)方法

若是你想添加到server後想快速提交(commit),能夠採用以下方式:

UpdateRequest req = new UpdateRequest(); 
req.setAction( UpdateRequest.ACTION.COMMIT, false, false ); 
req.add( docs ); 
UpdateResponse rsp = req.process( server );

二、添加pojo到索引庫

除了經過文檔的形式提交之外,solrj還能夠經過pojo的方式提交。使用註解@Field映射到schema.xml中定義的字段中

建立一個javaBean:

import org.apache.solr.client.solrj.beans.Field; 
public class Item { 
    @Field 
    String id; 
    @Field("cat")//自定義字段名
    String[] categories; 
    @Field
    List<String> features;

    public void setId(String id) {
        this.id = id;
    }

    public void getId() {
        return this.id;
    }

    //其餘get、set方法

}

建立JavaBean的實例:

Item item = new Item(); 
item.id = "one";
item.categories = new String[] { "aaa", "bbb", "ccc" };

添加到server中:

server.addBean(item);

提交到索引:

server.commit();

還能夠同時添加多個javaBean:

List<Item> beans ;
//add Item objects to the list  
server.addBeans(beans);

三、刪除索引

server.deleteById(String id);
或者
deleteById(List<String> ids);

四、更新索引

solr索引庫不一樣於數據庫,沒有更新的功能。若是想更新,先經過id刪除對應的文檔,再天新的文檔。

五、查詢索引

SolrQuery query = new SolrQuery();
query.setQuery( "*:*" ); 
query.addSortField( "price", SolrQuery.ORDER.asc );
QueryResponse rsp = server.query( query );

//文檔方式讀取

SolrDocumentList docs = rsp.getResults();//實際項目中若是業務比較複雜,採用這種方式顯得比較靈活

//使用javaBean的方式讀取

List<Item> beans = rsp.getBeans(Item.class);

六、高級使用

SolrServer server = getSolrServer(); 
SolrQuery solrQuery = new SolrQuery()
                      . setQuery("ipod")
                      . setFacet(true)
                      . setFacetMinCount(1)
                      . setFacetLimit(8)
                      . addFacetField("category")
                      . addFacetField("inStock");
QueryResponse rsp = server.query(solrQuery);

七、高亮顯示

SolrQuery query = new SolrQuery(); query.setQuery("foo");
query.setHighlight(true).setHighlightSnippets(1); 
//set other params as needed 
query.setParam("hl.fl", "content");
QueryResponse queryResponse = getSolrServer().query(query);

處理高亮顯示結果:

Iterator<SolrDocument> iter = queryResponse.getResults().iterator(); 
while (iter.hasNext()) { 
    SolrDocument resultDoc = iter.next(); 
    String content = (String) resultDoc.getFieldValue("content"); 
    String id = (String) resultDoc.getFieldValue("id"); //id is the uniqueKey field
    if (queryResponse.getHighlighting().get(id) != null) {
        List<String> highlightSnippets = queryResponse.getHighlighting().get(id).get("content"); 
    } 
}

本文出自 luoshengsha.com,轉載時請註明出處及相應連接。

本文永久連接: http://www.luoshengsha.com/245.html

相關文章
相關標籤/搜索