Solr添加文檔到索引


solr添加文檔非常方便,不用像Lucene那樣一個一個添加Field,省去了很多的麻煩下面看操作

方法一:

 1     private static String URI = "http://localhost:8080/solr/";
 2 
 3     private CommonsHttpSolrServer httpSolrServer = null;
 4 
 5     @Before
 6     public void init() {
 7         try {
 8             httpSolrServer = new CommonsHttpSolrServer(URI);
 9         } catch (MalformedURLException e) {
10             e.printStackTrace();
11         }
12     }
13 
14     @Test
15     public void test1() {
16         try {
17             SolrInputDocument document = new SolrInputDocument();
18             document.addField("id", "1");
19             document.addField("news_title", "這是我的第一個solr程序");
20             document.addField("news_content", "希望能夠運行起來");
21             httpSolrServer.add(document);
22             httpSolrServer.commit();
23         } catch (SolrServerException e) {
24             e.printStackTrace();
25         } catch (IOException e) {
26             e.printStackTrace();
27         }
28     }

Note:id為唯一,不可重復,如果重復,solr會自動將索引中id相同的元素更新為現在的屬性
域的名稱可以在schema.xml的Field中設置,默認的有很多Field,我們也可以使用默認的

   <field name="news_title" type="textComplex" indexed="true" stored="true" />
   <field name="news_content" type="textComplex" indexed="true" sotred="true" />

 

方法2:直接添加對象

2.1 定義對象

 1 package com.solr.entity;
 2 
 3 import org.apache.solr.client.solrj.beans.Field;
 4 
 5 public class News {
 6     private String id;
 7     private String title;
 8     private String content;
 9 
10     public News(){}
11     
12     public News(String id, String title, String content) {
13         this.id = id;
14         this.title = title;
15         this.content = content;
16     }
17 
18     public String getId() {
19         return id;
20     }
21 
22     @Field
23     public void setId(String id) {
24         this.id = id;
25     }
26 
27     public String getTitle() {
28         return title;
29     }
30 
31     @Field("news_title")
32     public void setTitle(String title) {
33         this.title = title;
34     }
35 
36     public String getContent() {
37         return content;
38     }
39 
40     @Field("news_content")
41     public void setContent(String content) {
42         this.content = content;
43     }
44 
45 }

2.2 添加對象到索引

 1     @Test
 2     public void test2() {
 3         try {
 4             List<News> list = new ArrayList<News>();
 5             News news1 = new News("2", "title2", "content2");
 6             list.add(news1);
 7             News news2 = new News("3", "title3", "content3");
 8             list.add(news2);
 9 
10             httpSolrServer.addBeans(list);
11             httpSolrServer.commit();
12         } catch (SolrServerException e) {
13             e.printStackTrace();
14         } catch (IOException e) {
15             e.printStackTrace();
16         }
17     }

Note:如果對象的某個域里面的屬性為數組,我們需要在schema.xml的Field中設置 multiValued="true"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM