Solr4.10使用教程(3):solr crud

Solr4.10使用教程(三):solr crud

solr的增删改查,啥都不说,直接代码

package com.johnny.lucene06.solr;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

public class SolrHelloWorldTest {
    //solr server URL指的时solr发布到web工程后的访问路径
    private final static String SolrURL = "http://localhost:8080/solr";
    
    private HttpSolrServer solrServer = null;
    /**如果要使用下面的test,需要确保solr的tomcat服务器以已经启动**/
    //使用Field添加doc
    @Test
    public void test01(){
        try {
            /**1、创建solrServer(HttpSolrServer和EmbeddedSolrServer)
             * 其中HttpSolrServer必须依赖tomcat等WEB容器,EmbeddedSolrServer则不需要,但是需要
             * 引入其它jar包,具体可以参照doc下得solr wiki.html
             * **/
            HttpSolrServer server = getSolrServer();
            server.deleteByQuery("*:*");//先删除默认数据,防止对理解产生混淆
            /**2、solr要求doc的Field中必须存在一个Field为id,并且值为java.lang.String类型
             * 并且要保证Id的唯一,否则最后添加的相同Id的域会覆盖前面的域,也就等于是更新
             * **/
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id",String.valueOf(1));
            /**3、对于需要使用doc添加的Field,必须先在schema.xml中进行配置,然后才可以使用,
             * 关于schema的配置说明,可以参照lucene-solr.txt中得说明
             * **/
            doc.addField("my_title","这是我的solr Hello world ");
            doc.addField("my_content","大家好,欢迎查看我的第一篇solr text");
            server.add(doc);
            SolrInputDocument doc2 = new SolrInputDocument();
            doc2.addField("id", String.valueOf(2));
            doc2.addField("my_title","我是中国人 ");
            doc2.addField("my_content","我爱你,伟大的祖国,我爱北京* hello");
            server.add(doc2);
            server.commit();//提交,将所有更细提交到索引中
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //solrServer是线程安全的,所以在使用时需要使用单例的模式,减少资源的消耗
    public HttpSolrServer getSolrServer(){
        if(solrServer==null){
            solrServer = new HttpSolrServer(SolrURL);
        }
        return solrServer;
    }
    
    /**
     * 使用POJO添加document
     */
    @Test
    public void testAddDocumentByObject(){
        try {
            HttpSolrServer server = getSolrServer();
            List<Message> msgs = new ArrayList<Message>();
            msgs.add(new Message("3","测试Hello POJO添加", "pojo应该可以添加成功"));
            msgs.add(new Message("4","Hello,我又添加了一条","现在Id已经是4了"));
            msgs.add(new Message("5", "我已经添加5条记录了", "hello ok,在添加我就吐了。。"));
            server.addBeans(msgs);
            server.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**修改记录,
     * 修改时是按照Id进行覆盖的
     * **/
    @Test
    public void testUpdate(){
        try {
            HttpSolrServer server = getSolrServer();
            server.addBean(new Message("5","id为5的数据被修改了", "hello,看来我真的被修改了"));
            server.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }
    /**
     * 查询,默认如果需要查看全部结果,需要使用*进行查询,不过可以在展示和提交的时候和空格进行转换
     */
    @Test
    public void testQuery(){
        try {
            HttpSolrServer server = getSolrServer();
            String q = "my_content:hello";//q表示查询的内容
            SolrQuery query = new SolrQuery(q);
            query.setStart(0)
                 .setRows(3);//进行分页查询
            query.setHighlight(true).setHighlightSimplePre("<span color='red'>")
                 .setHighlightSimplePost("</span>");//高亮
            
            query.setParam("hl.fl", "my_content");
            //只有将内容存储后才能进行展示,比如title_content查询结果就为空
            //query.setParam("hl.fl", "my_title,my_content");
            QueryResponse resp = server.query(query);
            SolrDocumentList sdList = resp.getResults();
            long totalResults = sdList.getNumFound();//命中的总记录数
            System.out.println("totalResults-->"+totalResults);
            for(SolrDocument sd:sdList){
                Collection<String> strs =  sd.getFieldNames();
                System.out.println(sd.getFieldValue("my_title"));
                System.out.println(sd.getFieldValue("my_content"));
                Object id = sd.getFieldValue("id");
                if(resp.getHighlighting().get(id)!=null){
                    System.out.println("高亮的结果-->"
                            +resp.getHighlighting().get(id).get("my_content"));
                }
                System.out.println("-------------------------------");
            
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }
}

配置说明:

关于schema.xml的配置说明
 (1)如果需要添加mmseg4j对solr的中文分词支持,添加:
     <!--配置mmsg4j-->
        <fieldtype name="textComplex" class="solr.TextField" positionIncrementGap="100">
            <analyzer>
                <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="dic"/>
            </analyzer>
        </fieldtype>
        <fieldtype name="textMaxWord" class="solr.TextField" positionIncrementGap="100">
            <analyzer>
                <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" />
            </analyzer>
        </fieldtype>
        <fieldtype name="textSimple" class="solr.TextField" positionIncrementGap="100">
            <analyzer>
                <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="n:/custom/path/to/my_dic" />
            </analyzer>
        </fieldtype>
 (2)添加自己的查询字段:
     <field name="my_title" type="string" indexed="true" stored="true"/>
     <field name="my_content" type="string" indexed="true" stored="true"/>
        其中,name:表示要添加的查询字段
             type:字段的类型,以及在schema.xml中有定义
             indexed:是否索引
             stored:是否存储
 (3)如果需要同时在title和content中进行查询,可以添加如下字段:
       <field name="title_content" type="textComplex" indexed="true" stored="false" multiValued="true"/>
      <copyField source="my_title" dest="title_content"/>
      <copyField source="my_content" dest="title_content"/>
      其中:title_content为新定义的查询字段,如果需要同时在title和content中进行查询,那么就使用这个查询字段
 (4)设置默认查询字段,这样的话就不需要在Query中进行查询时使用my_title:hello这样的格式
      将solrconfig.xml中得 <str name="df">text</str>
      修改为: <str name="df">title_content</str>
      这样,如果需要查询内容在标题字段或内容字段出现的结果的时候,只需要在查询条件中填写hello就可以了