顯示solr服務及系統運行信息java
solr運行的日誌信息web
在Solr中,每個Core,表明一個索引庫,裏面包含索引數據及其配置信息。
Solr中能夠擁有多個Core,也就同時管理多個索引庫!就像在MySQL中能夠有多個database同樣!瀏覽器
Java運行環境信息tomcat
solr運行線程信息服務器
注意:在本文件中,有兩個字段是Solr自帶的字段,絕對不要刪除:_version_節點和_root_節點app
屬性及含義:
name:字段名稱,最好如下劃線或者字母開頭
type:字段類型,指向的是本文件中的
indexed:是否建立索引
stored:是否被存儲
multiValued:是否能夠有多個值,若是字段能夠有多個值,設置爲truewebapp
屬性及含義:
name:字段類型的名稱,能夠自定義,
class:字段類型在Solr中的類。StrField可索引不可分詞。TextField字段可索引,能夠分詞,因此須要指定分詞器
Lucene中原本是沒有主鍵的。刪除和修改都須要根據詞條進行匹配。而Solr卻能夠設置一個字段爲惟一主鍵,這樣刪改操做均可以根據主鍵來進行!命令行
<fieldType name="text_ik" class="solr.TextField"> <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType>
private static String baseURL = "http://localhost:8080/solr/core1"; @Test public void createTest() throws Exception { //鏈接solr服務器 HttpSolrServer solrServer = new HttpSolrServer(baseURL); //建立文檔對象 SolrInputDocument document = new SolrInputDocument(); document.addField("id", "5"); document.addField("title", "8848手機,鈦合金外殼,註定不平凡"); document.addField("content", "8848發發發"); //向solr服務器寫入文檔 solrServer.add(document); solrServer.commit(); }
@Test public void create2Test() throws Exception { //鏈接solr服務器 HttpSolrServer solrServer = new HttpSolrServer(baseURL); //建立文檔對象 Item item = new Item(); item.setId("6"); item.setTitle("金立M2017成功人士的標配"); item.setContent("金立你值得擁有"); //向solr服務器寫入文檔 solrServer.addBean(item); solrServer.commit(); }
//添加@Field註解 public class Item{ @Field private String id; @Field private String title; @Field private String content; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
@Test public void deleteTest() throws SolrServerException, IOException { // 鏈接solr服務器 HttpSolrServer solrServer = new HttpSolrServer(baseURL); // 刪除索引 //solrServer.deleteById("6"); solrServer.deleteByQuery("title:金立"); // 提交 solrServer.commit(); }
在建立SolrQuery時,咱們填寫的Query語句,能夠有如下高級寫法:線程
@Test public void queryTest() throws SolrServerException{ // 鏈接solr服務器 HttpSolrServer solrServer = new HttpSolrServer(baseURL); //建立查詢條件對象 SolrQuery params = new SolrQuery("*:*"); //執行查詢,獲取響應數據 QueryResponse response = solrServer.query(params); //獲取數據結果集 SolrDocumentList list = response.getResults(); System.out.println("一共獲取了" + list.size()+"條結果:"); for (SolrDocument solrDocument : list) { System.out.println("id: " + solrDocument.getFieldValue("id")); System.out.println("title:" + solrDocument.getFieldValue("title")); } }
@Test public void queryBeanTest() throws SolrServerException{ // 鏈接solr服務器 HttpSolrServer solrServer = new HttpSolrServer(baseURL); //建立查詢條件對象 SolrQuery params = new SolrQuery("*:*"); //執行查詢,獲取響應 QueryResponse response = solrServer.query(params); List<Item> beans = response.getBeans(Item.class); System.out.println("一共獲取了" + beans.size()+"條結果:"); for (Item item : beans) { System.out.println("id: " + item.getId()); System.out.println("title:" + item.getTitle()); } }
@Test public void querySortTest() throws SolrServerException, IOException { // 鏈接solr服務器 HttpSolrServer solrServer = new HttpSolrServer(baseURL); // 建立查詢條件對象,範圍查詢,包含兩端 SolrQuery query = new SolrQuery("*:*"); // 設置查詢的排序參數,1-排序的字段名,2-排序方式(ORDER:asc desc) query.setSort("id", ORDER.asc); // 執行查詢,獲取響應數據 QueryResponse response = solrServer.query(query); // 獲取結果集數據 List<Item> list = response.getBeans(Item.class); System.out.println("總記錄數 numFound:"+response.getResults().getNumFound()); for (Item item : list) { System.out.println("id: " + item.getId()); System.out.println("title:" + item.getTitle()); } }
@Test public void queryPageTest() throws SolrServerException, IOException { // 準備分頁參數 int pageNum = 1; //頁碼 int pageSize = 2; //每頁條數 // 鏈接solr服務器 HttpSolrServer solrServer = new HttpSolrServer(baseURL); // 建立查詢條件對象 SolrQuery params = new SolrQuery("*:*"); // 設置查詢的排序參數,1-排序的字段名,2-排序方式(ORDER:asc desc) params.setStart((pageNum-1)*pageSize);//設置起始條數 params.setRows(pageSize);//設置每頁條數 // 執行查詢,獲取響應數據 QueryResponse response = solrServer.query(params); // 獲取結果集數據 SolrDocumentList list = response.getResults(); System.out.println("一共獲取了" + list.size() + "條結果:"); for (SolrDocument solrDocument : list) { System.out.println("id: " + solrDocument.getFieldValue("id")); System.out.println("title:" + solrDocument.getFieldValue("title")); } }
@Test public void highLightingTest() throws SolrServerException, IOException{ // 初始化solrj服務 HttpSolrServer server = new HttpSolrServer(baseURL); // 設置查詢條件 SolrQuery params = new SolrQuery("title:手機"); // 設置前置標籤 params.setHighlightSimplePre("<em >"); // 設置後置標籤 params.setHighlightSimplePost("</em>"); // 添加高亮字段 params.addHighlightField("title"); // 執行查詢 QueryResponse queryResponse = server.query(params); // 外層的Map,key:id,value:id之外的其餘高亮字段,可能有多個,也是一個Map // 內層的Map,key:高亮字段的名稱,value:字段的內容,集合 Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); // 遍歷map,獲取結果 Set<String> ids = highlighting.keySet(); for (String id : ids) { System.out.println("id: " + id);; // 獲取高亮字段的集合 Map<String, List<String>> map = highlighting.get(id); // 獲取高亮字段 System.out.println(map.get("title").get(0)); // 由於content不是高亮字段,因此打印出的內容爲null System.out.println(map.get("content")); } }