【solr】java整合solr5.0之solrj的使用


1、首先導入solrj需要的的架包

2、需要注意的是低版本是solr是使用SolrServer進行URL實例的,5.0之后已經使用SolrClient替代這個類了,在添加之后首先我們需要根據schema.xml配置一下我們的分詞器

這里的msg_all還需要在schema.xml中配置

它的主要作用是將msg_title,msg_content兩個域的值拷貝到msg_all域中,我們在搜索的時候可以只搜索這個msg_all域就可以了,

solr默認搜索需要帶上域,比如

 

solr更改默認搜索域的地方也在schema.xml,它默認是搜索text域的,但是5.0之后不在這里配置默認搜索域了,它的文檔也告訴我們,在solrconfig.xml中配置

 

在solrconfig.xml中配置默認搜素域,這樣我們就可以按照我們自己的域進行搜索了

 

 配置好以上,就可以使用代碼進行CURD

private final  static String URL="http://localhost:8080/solr/java";
    public SolrClient  server=null;
    
    @Before
    public void init() throws Exception{
        server=new HttpSolrClient(URL);
    }

 

刪除所有分詞

//刪除所有分詞
    @Test
    public void testDel() throws Exception{
        server.deleteByQuery("*:*");
        server.commit();//先刪除 基於query的刪除 會刪除所有建立的索引文件
    }

 

增加分詞

@Test
    public void testAdd() throws Exception{
        SolrInputDocument doc=new SolrInputDocument();
        doc.addField("id", "3");
        doc.addField("msg_title", "新浪微博");
        doc.addField("msg_content", "我有一個微博帳號名字叫做什么呢?");
        server.add(doc);
        server.commit();
    }

 

基於Bean增加分詞

@Test
    public void test03() throws Exception{
        List<Message> msgs=new ArrayList<Message>();
        msgs.add(new Message("4", "第四個測試solr測試文件", new String[]{"中華人民共和國萬歲","中華上下五千年那年"}));
        msgs.add(new Message("5", "第5個好朋友是什么意思呢?", new String[]{"上海是個好地方","歌唱我們親愛的祖國曾經走過千山萬水"}));
        server.addBeans(msgs);
        server.commit();
    }

 

查詢結果

@Test
    public void test04() throws Exception{
        //定義查詢內容 * 代表查詢所有    這個是基於結果集
         SolrQuery query = new SolrQuery("solr");
         query.setStart(0);//起始頁
         query.setRows(3);//每頁顯示數量
         QueryResponse rsp = server.query( query );
         SolrDocumentList results = rsp.getResults();
         System.out.println(results.getNumFound());//查詢總條數
         for(SolrDocument doc:results){
             System.out.println(doc);
         }
    }

 

將查詢結果集封裝為對象Bean

@Test
    public void test05() throws Exception{
         SolrQuery query = new SolrQuery("中華");// * 號 是查詢 所有的數據
         QueryResponse rsp = server.query( query );
         List<Message> beans = rsp.getBeans(Message.class);//這個不能獲取查詢的總數了 也不能高亮
         for(Message message:beans){
             System.out.println(message.toString());
         }
    }

 

將結果集高亮顯示

@Test
    public void test06() throws Exception{
        //定義查詢內容 * 代表查詢所有    這個是基於結果集
         SolrQuery query = new SolrQuery("solr");
         query.setStart(0);//起始頁
         query.setRows(5);//每頁顯示數量
         query.setParam("hl.fl", "msg_title,msg_content");//設置哪些字段域會高亮顯示
         query.setHighlight(true).setHighlightSimplePre("<span class='hight'>")
         .setHighlightSimplePost("</span>");
         
         QueryResponse rsp = server.query( query );
         SolrDocumentList results = rsp.getResults();
         System.out.println(results.getNumFound());//查詢總條數
         for(SolrDocument doc:results){
             String id = (String) doc.getFieldValue("id"); //id is the uniqueKey field
             if(rsp.getHighlighting().get(id)!=null){
                 //高亮必須要求存儲 不存儲的話 沒法添加高亮
                 System.out.println(rsp.getHighlighting().get(id).get("msg_title"));
             }
         }
    }

 

ok,solr的基本使用就完成了

 


免責聲明!

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



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