一、导入 jar 包
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>4.10.3</version> </dependency>
或者
链接:https://pan.baidu.com/s/14VlYDwtPpT4aiRP-UBAekg 密码:hfo4
二、添加
@Test public void addDocuemn() throws Exception{ //创建一个 SolrServer 对象,参数为 solr 服务的 url SolrServer solrServer = new HttpSolrServer("http://192.168.25.129:8080/solr/collection1"); //创建一个文档对象 SolrInputDocuemnt SolrInputDocument document1 = new SolrInputDocument(); //向文档对象中添加域。文档中必须包含一个 id 域,所有域的名称必须在 schema.xml 中定义 document1.addField("id", "01"); document1.addField("name", "jack"); //创建一个文档对象 SolrInputDocuemnt SolrInputDocument document2 = new SolrInputDocument(); //向文档对象中添加域。文档中必须包含一个 id 域,所有域的名称必须在 schema.xml 中定义 document2.addField("id", "02"); document2.addField("name", "rose"); //把文档写入索引库 solrServer.add(document1); solrServer.add(document2); //提交 solrServer.commit(); }
三、结果
四、更新
更新文档就是覆盖原来文档就可以了
五、删除
@Test public void delDocument() throws Exception{ //创建一个 SolrServer 对象,参数为 solr 服务的 url SolrServer solrServer = new HttpSolrServer("http://192.168.25.129:8080/solr/collection1"); //solrServer.deleteById("01");//通过 id 删除 solrServer.deleteByQuery("id:01 id:02");//通过查询条件批量删除( 域:值 ) //提交 solrServer.commit(); }
六、查询
@Test public void queryDocument() throws Exception{ //创建一个 SolrServer 对象,参数为 solr 服务的 url SolrServer solrServer = new HttpSolrServer("http://192.168.25.129:8080/solr/collection1"); //查询类 SolrQuery solrQuery = new SolrQuery(); //查询关键词 solrQuery.set("q", "name:jack"); //查询数据 QueryResponse response = solrServer.query(solrQuery); //取数据 SolrDocumentList solrList = response.getResults(); //一共多少条数据 long num = solrList.getNumFound(); System.out.println("条数:"+num); for (SolrDocument solrDocument : solrList) { String id = (String) solrDocument.get("id"); String name = (String) solrDocument.get("name"); System.out.println("id:"+id); System.out.println("name"+name); } }
7、复杂查询
@Test public void queryDocument2() throws Exception{ //创建一个 SolrServer 对象,参数为 solr 服务的 url SolrServer solrServer = new HttpSolrServer("http://192.168.25.129:8080/solr/collection1"); //查询类 SolrQuery solrQuery = new SolrQuery(); //拼接查询条件 solrQuery.set("张三");//设置查询内容 solrQuery.setStart(0); solrQuery.setRows(20); solrQuery.set("df", "item_title");//设置默认搜索域 solrQuery.setHighlight(true);//开启高亮搜索 solrQuery.addHighlightField("name");//高亮显示字段 solrQuery.setHighlightSimplePre("<em style = "color:red">");//前缀 solrQuery.setHighlightSimplePost("</em>");//后缀 //查询数据 QueryResponse response = solrServer.query(solrQuery); //取数据 SolrDocumentList solrList = response.getResults(); //一共多少条数据 long num = solrList.getNumFound(); System.out.println("条数:"+num); for (SolrDocument solrDocument : solrList) { String id = (String) solrDocument.get("id"); Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); List<String> list = highlighting.get(solrDocument.get("id")).get("name"); String name = ""; if( list != null && list.size() > 0 ){ name = list.get(0); }else{ name = (String) solrDocument.get("name"); } System.out.println("id:"+id); System.out.println("name"+name); } }