添加SolrJ的jar包
solrj是訪問Solr服務的java客戶端,提供索引和搜索的請求方法,SolrJ通常在嵌入在業務系統中,通過SolrJ的API接口操作Solr服務,
1 <dependency>
2 <groupId>org.apache.solr</groupId>
3 <artifactId>solr-solrj</artifactId>
4 <version>4.10.4</version>
5 </dependency>
6 <dependency>
7 <groupId>commons-logging</groupId>
8 <artifactId>commons-logging</artifactId>
9 <version>1.2</version>
10 </dependency>
創建索引
使用SolrJ創建索引,通過調用SolrJ提供的API請求Solr服務,Document通過SolrInputDocument進行構建。
1 // 創建索引
2 public static void createIndex() throws Exception { 3 SolrServer solrServer = new HttpSolrServer("http://192.168.50.50:8080/solr/collection1"); 4 SolrInputDocument document = new SolrInputDocument(); 5 document.addField("id", "00001"); 6 document.addField("name", "solr全文檢索"); 7 document.addField("price", 86.5f); 8 document.addField("description", "這是一本關於solr的書籍!"); 9 UpdateResponse response = solrServer.add(document); 10 solrServer.commit(); 11 }
刪除索引
1 //刪除索引
2 public void deleteIndex() throws Exception { 3 SolrServer solrServer = new HttpSolrServer(solrUrl); 4 //根據id刪除
5 UpdateResponse response = solrServer.deleteById("c0001"); 6 //根據多個id刪除
7 solrServer.deleteById("0001,0002"); 8 //自動查詢條件刪除
9 solrServer.deleteByQuery("name:教程"); 10 solrServer.commit(); 11 }
搜索索引
1 //查詢索引
2 public static void selectIndex() throws Exception { 3 SolrServer solr = new HttpSolrServer(solrUrl); 4 // 查詢對象
5 SolrQuery query = new SolrQuery(); 6 //設置查詢條件,名稱“q”是固定的且必須的 7 //搜索keywords域,keywords是復制域包括name和description
8 query.set("q", "keywords:java教程"); 9 // 設置商品分類、關鍵字查詢
10 query.setQuery("name:數據 AND price:11.1"); 11 // 設置價格范圍
12 query.set("fq", "price:[1 TO 20]"); 13 // 查詢結果按照價格降序排序 14 //query.set("sort", "price desc");
15 query.addSort("price", ORDER.desc); 16 // 請求查詢
17 QueryResponse response = solr.query(query); 18 // 查詢結果
19 SolrDocumentList docs = response.getResults(); 20 // 查詢文檔總數
21 System.out.println("查詢文檔總數" + docs.getNumFound()); 22 for (SolrDocument doc : docs) { 23 String id = (String) doc.getFieldValue("id"); 24 String name = (String)doc.getFieldValue("name"); 25 Float price = (Float)doc.getFieldValue("price"); 26 String description = (String)doc.getFieldValue("description"); 27 System.out.println(id); 28 } 29 }
高亮搜索索引
1 // 分頁和高亮
2 public static void selectHeightLight() throws Exception { 3 SolrServer solr = new HttpSolrServer(solrUrl); 4 // 查詢對象
5 SolrQuery query = new SolrQuery(); 6 // text是name、title等眾多字段的復制域
7 query.setQuery("text:算"); 8 // 每頁顯示記錄數
9 int pageSize = 2; 10 // 當前頁碼
11 int curPage = 1; 12 // 開始記錄下標
13 int begin = pageSize * (curPage - 1); 14 // 起始下標
15 query.setStart(begin); 16 // 結束下標
17 query.setRows(pageSize); 18 // 設置高亮參數
19 query.setHighlight(true); // 開啟高亮組件
20 query.addHighlightField("name");// 高亮字段
21 query.setHighlightSimplePre("<span color='red'>");//前綴標記
22 query.setHighlightSimplePost("</span>");// 后綴標記 23 // 請求查詢
24 QueryResponse response = solr.query(query); 25 // 查詢結果
26 SolrDocumentList docs = response.getResults(); 27 // 查詢文檔總數
28 System.out.println("查詢文檔總數" + docs.getNumFound()); 29 for (SolrDocument doc : docs) { 30 // 商品主鍵
31 String id = (String) doc.getFieldValue("id"); 32 // 商品名稱
33 String name = (String)doc.getFieldValue("name"); 34 // 高亮信息
35 if(response.getHighlighting() != null) { 36 if(response.getHighlighting().get(id) != null) { 37 // 取出高亮片段
38 Map<String, List<String>> map = response.getHighlighting().get(id); 39 if(map.get("name") != null) { 40 for(String s : map.get("name")) { 41 System.out.println(s); 42 } 43 } 44 } 45 } 46 } 47 }