Apache Solr 之 使用SolrJ操作索引庫

jopen 11年前發布 | 22K 次閱讀 Solr 搜索引擎 Apache Solr

       Solrj是Solr搜索服務器的一個比較基礎的客戶端工具,可以非常方便地與Solr搜索服務器進行交互。最基本的功能就是管理Solr索引,包括添加、更新、刪除和查詢等。對于一些比較基礎的應用,用Solj基本夠用,而且你可以非常容易地通過使用Solrj的API實現與Solr搜索服務器進行交互,實現對Solr的基本管理功能。如果你的應用比較復雜,可以擴展Solrj來滿足需要。

       使用 SolrJ操作索引庫:

package com.hcm.solr.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.junit.Before;
import org.junit.Test;

import com.hcm.solr.entity.Message;

public class SolrTest {

    private static Log logger = LogFactory.getLog(SolrTest.class);

    private static final String URL = "http://127.0.0.1:8086/solr";

    private HttpSolrServer server = null;

    @Before
    public void init() {
        // 創建 server
        server = new HttpSolrServer(URL);
    }

    /**
     * 添加文檔
     */
    @Test
    public void addDoc() {

        SolrInputDocument doc = new SolrInputDocument();

        doc.addField("id", "11");
        doc.addField("title", "this is my document !!");

        try {

            UpdateResponse response = server.add(doc);
            // 提交
            server.commit();

            logger.info("########## Query Time :" + response.getQTime());
            logger.info("########## Elapsed Time :" + response.getElapsedTime());
            logger.info("########## Status :" + response.getStatus());

        } catch (SolrServerException | IOException e) {
            logger.error("", e);
        }
    }

    /**
     * 添加多個文檔
     */
    @Test
    public void addDocs() {

        String[] titles = new String[] { "aaaaaaa", "bbbbbbb", "ccccccc", "dddddd", "eeeeee" };

        List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();

        int i = 0;
        for (String str : titles) {
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", i++);
            doc.addField("title", str);
            docs.add(doc);
        }

        try {

            UpdateResponse response = server.add(docs);
            server.commit();

            logger.info("########## Query Time :" + response.getQTime());
            logger.info("########## Elapsed Time :" + response.getElapsedTime());
            logger.info("########## Status :" + response.getStatus());

        } catch (SolrServerException | IOException e) {
            logger.error("", e);
        }
    }

    /**
     * 添加一個Entity到索引庫
     */
    @Test
    public void addBean() {

        Message msg = new Message("1001", "What is Fluentd?", new String[] { "Fluentd is an open source data collector for unified logging layer",
                "Fluentd allows you to unify data collection and consumption for a better use and understanding of data.",
                "Fluentd decouples data sources from backend systems by providing a unified logging layer in between.",
                "Fluentd proves you can achieve programmer happiness and performance at the same time. A great example of Ruby beyond the Web.",
                "Fluentd to differentiate their products with better use of data." });

        try {

            UpdateResponse response = server.addBean(msg);
            server.commit();

            logger.info("########## Query Time :" + response.getQTime());
            logger.info("########## Elapsed Time :" + response.getElapsedTime());
            logger.info("########## Status :" + response.getStatus());

        } catch (SolrServerException | IOException e) {
            logger.error("", e);
        }
    }

    /**
     * 添加多個Entity到索引庫
     */
    @Test
    public void addBeans() {

        List<Message> msgs = new ArrayList<Message>();

        Message msg = new Message("1001", "What is Fluentd?", new String[] { "Fluentd is an open source data collector for unified logging layer",
                "Fluentd allows you to unify data collection and consumption for a better use and understanding of data.",
                "Fluentd decouples data sources from backend systems by providing a unified logging layer in between.",
                "Fluentd proves you can achieve programmer happiness and performance at the same time. A great example of Ruby beyond the Web.",
                "Fluentd to differentiate their products with better use of data." });

        Message msg2 = new Message("1002", "What is Fluentd?", new String[] { "Fluentd is an open source data collector for unified logging layer",
                "Fluentd allows you to unify data collection and consumption for a better use and understanding of data.",
                "Fluentd decouples data sources from backend systems by providing a unified logging layer in between.",
                "Fluentd proves you can achieve programmer happiness and performance at the same time. A great example of Ruby beyond the Web.",
                "Fluentd to differentiate their products with better use of data." });

        msgs.add(msg);
        msgs.add(msg2);

        try {

            UpdateResponse response = server.addBeans(msgs);
            server.commit();

            logger.info("########## Query Time :" + response.getQTime());
            logger.info("########## Elapsed Time :" + response.getElapsedTime());
            logger.info("########## Status :" + response.getStatus());

        } catch (SolrServerException | IOException e) {
            logger.error("", e);
        }
    }

    /**
     * 刪除索引
     */
    @Test
    public void deleteDoc() {
        try {
            server.deleteById("0");
            server.commit();
        } catch (SolrServerException | IOException e) {
            logger.error("", e);
        }
    }

    /**
     * 更新索引<br>
     * solr索引庫不同于數據庫,沒有更新的功能。如果想更新,先通過id刪除對應的文檔,再添加新的文檔。
     */
    @Test
    public void updateDoc() {
        // ... ...
    }

    /**
     * 查詢
     */
    @Test
    public void testQuery() {
        String queryStr = "*:*";
        SolrQuery params = new SolrQuery(queryStr);
        params.set("rows", 10);
        try {
            QueryResponse response = server.query(params);
            SolrDocumentList list = response.getResults();
            logger.info("########### 總共 : " + list.getNumFound() + "條記錄");
            for (SolrDocument doc : list) {
                logger.info("######### id : " + doc.get("id") + "  title : " + doc.get("title"));
            }
        } catch (SolrServerException e) {
            logger.error("", e);
        }
    }

    /**
     * 簡單查詢(分頁)
     */
    @Test
    public void querySimple() {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("q", "this my");
        params.set("q.op", "and");
        params.set("start", 0);
        params.set("rows", 5);
        params.set("fl", "*,score");
        try {
            QueryResponse response = server.query(params);
            SolrDocumentList list = response.getResults();
            logger.info("########### 總共 : " + list.getNumFound() + "條記錄");
            for (SolrDocument doc : list) {
                logger.info("######### id : " + doc.get("id") + "  title : " + doc.get("title"));
            }
        } catch (SolrServerException e) {
            logger.error("", e);
        }
    }

    /**
     * 查詢(分頁,高亮)
     */
    @Test
    public void queryCase() {
        String queryStr = "title:this";
        SolrQuery params = new SolrQuery(queryStr);
        params.set("start", 0);
        params.set("rows", 5);

        // 啟用高亮組件, 設置高亮
        params.setHighlight(true)                           
            .addHighlightField("title")                     
            .setHighlightSimplePre("<span class=\"red\">")
            .setHighlightSimplePost("</span>")
            .setHighlightSnippets(2)
            .setHighlightFragsize(1000)
            .setStart(0)
            .setRows(10)
            .set("hl.useFastVectorHighlighter", "true")
            .set("hl.fragsize", "200");

        try {
            QueryResponse response = server.query(params);
            SolrDocumentList list = response.getResults();
            logger.info("########### 總共 : " + list.getNumFound() + "條記錄");
            for (SolrDocument doc : list) {
                logger.info("######### id : " + doc.get("id") + "  title : " + doc.get("title"));
            }

            Map<String, Map<String, List<String>>> map = response.getHighlighting();
            Iterator<String> iterator = map.keySet().iterator();

            while (iterator.hasNext()) {
                String key = iterator.next();
                Map<String, List<String>> values = map.get(key);
                logger.info("############################################################");
                logger.info("############ id : " + key);

                for (Map.Entry<String, List<String>> entry : values.entrySet()) {
                    String subKey = entry.getKey();
                    List<String> subValues = entry.getValue();

                    logger.info("############ subKey : " + subKey);
                    for (String str : subValues) {
                        logger.info("############ subValues : " + str);
                    }
                }

            }

        } catch (SolrServerException e) {
            logger.error("", e);
        }
    }

}

POJO:

package com.hcm.solr.entity;

import java.io.Serializable;

import org.apache.solr.client.solrj.beans.Field;

public class Message implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String id;
    private String title;
    private String[] content;

    public Message() {
        super();
    }

    public Message(String id, String title, String[] content) {
        super();
        this.id = id;
        this.title = title;
        this.content = content;
    }

    public String getId() {
        return id;
    }

    @Field("id")
    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    @Field("title")
    public void setTitle(String title) {
        this.title = title;
    }

    public String[] getContent() {
        return content;
    }

    @Field("content")
    public void setContent(String[] content) {
        this.content = content;
    }

}


(完)

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!