solr 學習之solrJ

solrJ是訪問Solr服務的JAVA客戶端,提供索引和搜索的請求方法,SolrJ一般嵌入在業務系統中,經過solrJ的API接口操做Solr服務。apache

 

 <!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
    <dependency>
        <groupId>org.apache.solr</groupId>
        <artifactId>solr-solrj</artifactId>
        <version>4.10.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>

1、添加數據

public static  void  addDocument() throws Exception{
        //建立Solr的客戶端連接對象
        HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
        for(int i=3;i<100;i++){
            //建立一個文檔對象
            SolrInputDocument sd=new SolrInputDocument();
            //添加域
            sd.addField("id", UUID.randomUUID());
            sd.addField("item_title", "商品"+i);
            sd.addField("item_sell_point", "好看"+i);
            sd.addField("item_price", 100L);
            sd.addField("item_desc", "商品"+i+"這個東西很不錯啊");
            sd.addField("item_image", "2"+i+".jpg");
            sd.addField("item_category_name", "分類"+i);
            solrServer.add(sd);
            solrServer.commit();
        }
    }

2、刪除

//根據document的Id直接刪除
    public static void deleteDocument() throws Exception{
        //建立Solr的客戶端連接對象
        HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
        solrServer.deleteById("8ceee0a5-52ee-43b6-88ba-249b02c8279c");
        solrServer.commit();
    }
    //根據條件查詢刪除
    public static void deleteQueryDocument() throws Exception{
        //建立Solr的客戶端連接對象
        HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
        //查詢刪除
        solrServer.deleteByQuery("item_title:商品1");
        solrServer.commit();
    }

3、查詢

public static void queryDocument() throws Exception{
        //建立Solr的客戶端連接對象
        HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
        //建立solr的查詢對象
        SolrQuery sq=new SolrQuery();
        //設置查詢條件
        sq.set("q","item_title:3" );
        //查詢
        QueryResponse qr=solrServer.query(sq);
        //獲取查詢結果
        SolrDocumentList sds=qr.getResults();
        //獲取查詢的記錄數
        long total=sds.getNumFound();
        System.out.println("數量:"+total);
        for(SolrDocument sd:sds){//默認取出10條記錄
            String id=(String) sd.getFieldValue("id");
            String item_title=(String) sd.getFieldValue("item_title");
            String item_sell_point=(String) sd.getFieldValue("item_sell_point");
            long item_price=(Long) sd.getFieldValue("item_price");
            String item_desc=(String) sd.getFieldValue("item_desc");
            String item_image=(String) sd.getFieldValue("item_image");
            String item_category_name=(String) sd.getFieldValue("item_category_name");
            System.out.println("========================================");
            System.out.println("id:"+id);
            System.out.println("item_title:"+item_title);
            System.out.println("item_sell_point:"+item_sell_point);
            System.out.println("item_price:"+item_price);
            System.out.println("item_desc:"+item_desc);
            System.out.println("item_image:"+item_image);
            System.out.println("item_category_name:"+item_category_name);
        }
    }

一、多條件查詢dom

//設置查詢條件
sq.set("q","item_title:3 AND item_desc:東西 OR item_sell_point:好看" );

二、設置過濾條件spa

//設置過濾條件
 sq.set("fq", "item_price:[1 TO 20]");

三、設置排序code

//設置排序
sq.addSort("item_title", ORDER.desc);

四、設置分頁對象

//設置分頁
sq.setStart(0);//開始位置 sq.setRows(3);//每頁3條

五、設置高亮blog

public static void queryDocument() throws Exception{
        //建立Solr的客戶端連接對象
        HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
        //建立solr的查詢對象
        SolrQuery sq=new SolrQuery();
        //設置查詢條件
        sq.set("q","item_title:商品" );
        //設置過濾條件
    //    sq.set("fq", "item_price:[1 TO 20]");
        //設置排序
        sq.addSort("item_title", ORDER.desc);
        //設置分頁
        sq.setStart(0);//開始位置
        sq.setRows(3);//每頁3條
        
        //開啓高亮
        sq.setHighlight(true);
        sq.addHighlightField("item_title");//設置高亮域
        sq.setHighlightSimplePre("<b>");//設置高亮樣式
        sq.setHighlightSimplePost("</b>");
        //查詢
        QueryResponse qr=solrServer.query(sq);
        //獲取查詢結果
        SolrDocumentList sds=qr.getResults();
        //獲取查詢的記錄數
        long total=sds.getNumFound();
        System.out.println("數量:"+total);
        for(SolrDocument sd:sds){//默認取出10條記錄
            String id=(String) sd.getFieldValue("id");
            String item_title=(String) sd.getFieldValue("item_title");
            String item_sell_point=(String) sd.getFieldValue("item_sell_point");
            long item_price=(Long) sd.getFieldValue("item_price");
            String item_desc=(String) sd.getFieldValue("item_desc");
            String item_image=(String) sd.getFieldValue("item_image");
            String item_category_name=(String) sd.getFieldValue("item_category_name");
            System.out.println("========================================");
            System.out.println("id:"+id);
            System.out.println("item_title:"+item_title);
            System.out.println("item_sell_point:"+item_sell_point);
            System.out.println("item_price:"+item_price);
            System.out.println("item_desc:"+item_desc);
            System.out.println("item_image:"+item_image);
            System.out.println("item_category_name:"+item_category_name);
            //獲取高亮顯示的結構
            Map<String, Map<String, List<String>>> highlighting=qr.getHighlighting();
            if(highlighting!=null){
                //根據Id得到每一個域的高亮內容
                Map<String, List<String>> map=highlighting.get(id);
                //根據具體的域獲取高亮內容
                List<String> list=map.get("item_title");
                if(list!=null && !list.isEmpty()){
                    for(String str:list){
                        System.out.println("str:"+str);
                    }
                }
            }
        }
    }
相關文章
相關標籤/搜索