Solr例子一

来源:互联网 发布:软件著作权 专利权 编辑:程序博客网 时间:2024/05/16 04:15

1、实体

public class VideoSolr {
    
    private Integer id;
    private String name;
    private String author;
    private String description;
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

}


2、测试类

import java.io.IOException;

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

import javax.swing.SortOrder;

import org.apache.solr.client.solrj.SolrQuery;
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;

/*
 * Copyright (C) 2015 ShenZhen tianlang Co.,Ltd All Rights Reserved.
 * 未经本公司正式书面同意,其他任何个人、团体不得使用、复制、修改或发布本软件.
 * 版权所有深圳天狼服务有限公司 www.tianlang.com.
 */

/**
 * 测试
 * @version 2016年11月22日上午9:54:29
 * @author wuliu
 */
public class SolrTest {
    
public static final String SOLR_URL = "http://admin:123456@localhost:8080/solr/collection1/";
    
    private final HttpSolrServer server;
    
    public SolrTest(){
        server = new HttpSolrServer(SOLR_URL);
        server.setAllowCompression(true);
    }
    
    /**
     * 添加单个信息
     * @throws IOException
     * @throws SolrServerException
     */
    public void addVideoSolr(VideoSolr videoSolr) throws SolrServerException, IOException{
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", videoSolr.getId());
        document.addField("name", videoSolr.getName());
        document.addField("author", videoSolr.getAuthor());
        document.addField("description", videoSolr.getDescription());
        server.add(document);
        server.commit();
    }
    
    /**
     * 删除所有索引
     *
     * @version 2016年11月22日上午9:51:33
     * @author wuliu
     * @throws SolrServerException
     * @throws IOException
     */
    public void deleteAllIndex() throws SolrServerException, IOException{
        server.deleteByQuery("*:*");
        server.commit();
    }
    
    /**
     * 批量添加
     *
     * @version 2016年11月22日上午10:06:32
     * @author wuliu
     * @param videoSolrs
     * @throws IOException
     * @throws SolrServerException
     */
    public void addVideoSolrList(List<VideoSolr> videoSolrs) throws SolrServerException, IOException{
        Collection<SolrInputDocument> documents = new ArrayList<SolrInputDocument>();
        for(VideoSolr videoSolr : videoSolrs){
            SolrInputDocument document = parese(videoSolr);
            documents.add(document);
        }
        server.add(documents);
        server.commit();
    }
    
    private SolrInputDocument parese(VideoSolr videoSolr) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", videoSolr.getId());
        doc.addField("name", videoSolr.getName());
        doc.addField("author", videoSolr.getName());
        doc.addField("description", videoSolr.getDescription());
        return doc;
    }
    
    /**
     * 根据Id删除
     *
     * @version 2016年11月22日上午10:16:39
     * @author wuliu
     * @param id
     * @throws IOException
     * @throws SolrServerException
     */
    public void deleteById(Integer id) throws SolrServerException, IOException{
        server.deleteById(id + "");
        server.commit();
    }
    
    /**
     * 根据语句删除
     *
     * @version 2016年11月22日上午10:18:52
     * @author wuliu
     * @throws SolrServerException
     * @throws IOException
     */
    public void deleteByQuery() throws SolrServerException, IOException{
        server.deleteByQuery("name:张三101");
        server.commit();
    }
    
    /**
     * 更新索引
     *
     * @version 2016年11月22日上午10:24:26
     * @author wuliu
     * @param videoSolr
     * @throws SolrServerException
     * @throws IOException
     */
    public void updateIndex(VideoSolr videoSolr) throws SolrServerException, IOException{
        addVideoSolr(videoSolr);
    }

    /**
     * 查询
     *
     * @version 2016年11月22日上午11:34:06
     * @author wuliu
     * @param keyword
     * @param page
     * @param size
     * @throws IOException
     * @throws SolrServerException
     */
    public  List<VideoSolr> queryVideoInfos(String keyword,int page,int size) throws SolrServerException, IOException{
        SolrQuery solrQuery = new SolrQuery();
//      String queryString = String.format("name:*%s* description:*%s*", key,key);
        String queryString = String.format("name:%s",keyword);
//      String queryString = String.format("name:solr*");
        solrQuery.setQuery(queryString);
        solrQuery.setSort("id", SolrQuery.ORDER.desc);
        solrQuery.setStart((page-1) * size);
        solrQuery.setRows(size);
        solrQuery.setHighlight(true); // 开启高亮组件
        solrQuery.addHighlightField("name");// 高亮字段
//      solrQuery.setHighlightSimplePre("<font color=\"red\">");// 标记
//      solrQuery.setHighlightSimplePost("</font>");
//      solrQuery.setHighlightSnippets(1);// 结果分片数,默认为1
//      solrQuery.setHighlightFragsize(1000);// 每个分片的最大长度
        QueryResponse res = server.query(solrQuery);
        List<VideoSolr> videoSolrs = new ArrayList<VideoSolr>();
        SolrDocumentList resList = res.getResults();
        System.out.println("查询长度:" + resList.size());
        for(SolrDocument document : resList){
            String id = (String) document.getFieldValue("id");
            System.out.println(document.getFieldValue("name"));
            VideoSolr videoSolr = new VideoSolr();
            videoSolr.setId(Integer.parseInt(id));
            videoSolrs.add(videoSolr);
        }
        
        return videoSolrs;
    }
    public static void main(String[] args) throws Throwable {
        SolrTest solrTest = new SolrTest();
//        //遍历一个一个的添加
//        for(int i = 1; i <= 10; i++){
//            VideoSolr videoSolr = new VideoSolr();
//            videoSolr.setId(i);
//            if(i <= 5){
//                videoSolr.setName("admin" + i);
//            }else{
//                videoSolr.setName("manager" + i);
//            }
//            videoSolr.setAuthor("author" + i);
//            videoSolr.setDescription("desc" + i);
//            solrTest.addVideoSolr(videoSolr);
//        }
        
        //删除所有索引
//        solrTest.deleteAllIndex();
        //批量添加
//        List<VideoSolr> list = new ArrayList<VideoSolr>();
//        VideoSolr videoSolr = new VideoSolr();
//        videoSolr.setId(1000);
//        videoSolr.setName("李四1000");
//        videoSolr.setAuthor("作者1000");
//        videoSolr.setDescription("描述信息1000");
//        
//        VideoSolr videoSolr2 = new VideoSolr();
//        videoSolr2.setId(1001);
//        videoSolr2.setName("李四1001");
//        videoSolr2.setAuthor("作者1001");
//        videoSolr2.setDescription("描述信息1001");
//        list.add(videoSolr);
//        list.add(videoSolr2);
//        solrTest.addVideoSolrList(list);
        //根据ID删除单个信息
//        solrTest.deleteById(1001);
//        solrTest.deleteByQuery();
        //更新索引
//        VideoSolr videoSolr = new VideoSolr();
//        videoSolr.setId(1000);
//        videoSolr.setName("王五1000");
//        videoSolr.setAuthor("作者王五1000");
//        videoSolr.setDescription("描述1000");
//        solrTest.updateIndex(videoSolr);
        //查询
//        solrTest.queryVideoInfos("李四", 1, 3);
    }
}

0 0