標籤:http io ar os 使用 sp for java on
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;@Beforepublic void init() {// 建立 serverserver = new HttpSolrServer(URL);}/** * 添加文檔 */@Testpublic 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);}}/** * 添加多個文檔 */@Testpublic 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到索引庫 */@Testpublic 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到索引庫 */@Testpublic 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);}}/** * 刪除索引 */@Testpublic void deleteDoc() {try {server.deleteById("0");server.commit();} catch (SolrServerException | IOException e) {logger.error("", e);}}/** * 更新索引<br> * solr索引庫不同於資料庫,沒有更新的功能。如果想更新,先通過id刪除對應的文檔,再添加新的文檔。 */@Testpublic void updateDoc() {// ... ...}/** * 查詢 */@Testpublic 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);}}/** * 簡單查詢(分頁) */@Testpublic 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);}}/** * 查詢(分頁,高亮) */@Testpublic 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;}}
(完)
Apache Solr 之 使用SolrJ操作索引庫