zoukankan      html  css  js  c++  java
  • 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"

  • 相关阅读:
    关于深拷贝和浅拷贝的理解
    Oracle数据库入门
    单例模式入门
    oracle存储过程 (还没动手实践、剩余内容找时间在处理、游标还没接触)
    Oracle用户名及默认密码
    oracle数据库怎么创建数据库实例
    Java 强、弱、软、虚,你属于哪一种?
    内存溢出和内存泄漏的区别
    aop中execution 表达式
    JPA 中@Enumerated
  • 原文地址:https://www.cnblogs.com/Laupaul/p/2467169.html
Copyright © 2011-2022 走看看