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 = new HttpSolrServer("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")); } }
高亮显示的数据与普通的数据是分开取出的