1. 程式人生 > >【Apache Solr系列之三】Solr客戶端SolrJ API使用文件-增刪改

【Apache Solr系列之三】Solr客戶端SolrJ API使用文件-增刪改

通過之前兩篇文章的學習之後,使用solr對mysql進行資料匯入以及增量索引應該都會了!

接下來我們學習下如果從Solr中讀取我們想要的資料。同時你也可以結合Solr的web介面進行驗證,看看你的查詢結果是否正確。

環境準備:

從之前下載的solr安裝包中解壓獲取以下jar包

/dist

apache-solr-solrj-*.jar

/dist/solrj-lib:

commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-io-1.4.jar
jcl-over-slf4j-1.5.5.jar
slf4j-api-1.5.5.jar

/lib:

slf4j-jdk14-1.5.5.jar

或者如果你通過maven進行jar包管理的。可以使用以下maven庫新增所需要的jar包

<dependency>
               <artifactId>solr-solrj</artifactId>
               <groupId>org.apache.solr</groupId>
               <version>1.4.0</version>
               <type>jar</type>
               <scope>compile</scope>
        </dependency>
如果需要使用到EmbeddedSolrServer,那麼需要匯入core包。
<dependency>
               <artifactId>solr-core</artifactId>
               <groupId>org.apache.solr</groupId>
               <version>1.4.0</version>
               <type>jar</type>
               <scope>compile</scope>
        </dependency>
還有兩個依賴包
<dependency>
               <groupId>javax.servlet</groupId>
               <artifactId>servlet-api</artifactId>
               <version>2.5</version>
        </dependency>

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.5.6</version>
        </dependency>
環境準備好之後,我們先來看下使用HttpSolrServer建立連線
String url = "http://${ip}:${port}";
  /*
    HttpSolrServer is thread-safe and if you are using the following constructor,
    you *MUST* re-use the same instance for all requests.  If instances are created on
    the fly, it can cause a connection leak. The recommended practice is to keep a
    static instance of HttpSolrServer per solr server url and share it for all requests.
    See https://issues.apache.org/jira/browse/SOLR-861 for more details
  */
SolrServer server = new HttpSolrServer( url );
你還可以在建立連線的時候設定相應的一些連線屬性
String url = "http://<span style="font-family: Arial, Helvetica, sans-serif;">${ip}:${port}</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span>
  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);
我想大夥很多都是使用實體來接收返回的資料,這樣的話方便管理,那麼看下SolrJ裡面是如何定義實體的。

其實SolrJ中定義實體和平時沒有太大區別。就是多了一個Annotation註解,用來標誌與solr entry屬性對應。

import org.apache.solr.client.solrj.beans.Field;

 public class Item {
    @Field
    String id;

    @Field("cat")
    String[] categories;

    @Field
    List<String> features;

  }
除了設定在欄位上,我們還可以設定在set方法上。
@Field("cat")
   public void setCategory(String[] c){
       this.categories = c;
   }
新增資料:

首先獲取SolrServer

SolrServer server = new HttpSolrServer("http://${ip}:${port}");
如果要刪除所有的索引的話
server.deleteByQuery( "*:*" );// CAUTION: deletes everything!
使用我們定義的Bean來往solr插入資料
Item item = new Item();
    item.id = "one";
    item.categories =  new String[] { "aaa", "bbb", "ccc" };
server.addBean(item);
如果需要一次插入多個的話。插入一個List<Bean>即可
List<Item> beans ;
  //add Item objects to the list
  server.addBeans(beans);

你可以通過以下形式在一個HTTP請求中更改你所有的索引。這個是最優化的方式

HttpSolrServer server = new HttpSolrServer();
Iterator<SolrInputDocument> iter = new Iterator<SolrInputDocument>(){
     public boolean hasNext() {
        boolean result ;
        // set the result to true false to say if you have more documensts
        return result;
      }

      public SolrInputDocument next() {
        SolrInputDocument result = null;
        // construct a new document here and set it to result
        return result;
      }
};
server.add(iter);

solrj查詢方面會有單獨的博文講解。