Solr:SolrJ(增刪改查)
阿新 • • 發佈:2020-12-24
1、SolrJ
solrj是訪問Solr服務的java客戶端,提供索引和搜尋的請求方法,java程式通過SolrJ請求Solr服務,最終Solr完在索引和搜尋
2、增加文件
@Test public void addDocument() throws Exception { SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr"); SolrInputDocument document = new SolrInputDocument(); document.addField("id", "p1"); document.addField("title_ik", "z"); document.addField("content_ik", "酸酸甜甜"); document.addField("product_name", "橘子"); solrServer.add(document); solrServer.commit(); }
先獲取Solr的連線,然後向文件中新增域,最後將域提交
"response": { "numFound": 1,"start": 0, "docs": [ { "id": "p1", "title_ik": "z", "product_name": "橘子", "_version_": 1686879363571646500 } ]
3、更新
與新增一樣,ID相同時增加,ID不同是更新
4、刪除
(1)根據ID
@Test public void deleteDocumentByid() throws Exception { SolrServer solrServer = newHttpSolrServer("http://localhost:8080/solr"); solrServer.deleteById("c0001"); solrServer.commit(); }
(2)根據條件
solrServer.deleteByQuery("*:*");
5、查詢
(1)簡單查詢
public void queryIndex() throws Exception { SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr"); SolrQuery query = new SolrQuery(); query.setQuery("*:*"); QueryResponse queryResponse = solrServer.query(query); SolrDocumentList solrDocumentList = queryResponse.getResults(); System.out.println("總量:" + solrDocumentList.getNumFound()); for (SolrDocument solrDocument : solrDocumentList) { System.out.println(solrDocument.get("id")); System.out.println(solrDocument.get("product_name")); System.out.println(solrDocument.get("product_price")); System.out.println(solrDocument.get("product_catalog_name")); System.out.println(solrDocument.get("product_picture")); } }
預設只查詢前十條資料
(2)複雜查詢
@Test public void queryIndex2() throws Exception { //建立連線 SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr"); //建立一個query物件 SolrQuery query = new SolrQuery(); //設定查詢條件 query.setQuery("鑽石"); //過濾條件 query.setFilterQueries("product_catalog_name:幽默雜貨"); //排序條件 query.setSort("product_price", SolrQuery.ORDER.asc); //分頁處理 query.setStart(0); query.setRows(10); //結果中域的列表 query.setFields("id","product_name","product_price","product_catalog_name","product_picture"); //設定預設搜尋域 query.set("df", "product_keywords"); //高亮顯示 query.setHighlight(true); //高亮顯示的域 query.addHighlightField("product_name"); //高亮顯示的字首 query.setHighlightSimplePre("<em>"); //高亮顯示的字尾 query.setHighlightSimplePost("</em>"); //執行查詢 QueryResponse queryResponse = solrServer.query(query); //取查詢結果 SolrDocumentList solrDocumentList = queryResponse.getResults(); //共查詢到商品數量 System.out.println("共查詢到商品數量:" + solrDocumentList.getNumFound()); //遍歷查詢的結果 for (SolrDocument solrDocument : solrDocumentList) { System.out.println(solrDocument.get("id")); //取高亮顯示 String productName = ""; Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); List<String> list = highlighting.get(solrDocument.get("id")).get("product_name"); //判斷是否有高亮內容 if (null != list) { productName = list.get(0); } else { productName = (String) solrDocument.get("product_name"); } System.out.println(productName); System.out.println(solrDocument.get("product_price")); System.out.println(solrDocument.get("product_catalog_name")); System.out.println(solrDocument.get("product_picture")); } }
高亮顯示的資料與普通的資料是分開取出的