solr的客户端操作:使用solrj进行curd操作
导入相关的jar包
<dependency><groupId>org.apache.solr</groupId><artifactId>solr-solrj</artifactId><version>4.10.2</version> </dependency> <!--日志的包, solrj执行需要一个日志包--> <dependency><groupId>commons-logging</groupId><artifactId>commons-logging-api</artifactId><version>1.1</version> </dependency>
增删改的操作:
package com.zy.solr;import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.common.SolrInputDocument; import org.junit.Test;import java.io.IOException; import java.util.ArrayList; import java.util.List;public class BaseSolr {//solrj写入一个索引 @Testpublic void createIndexToSolr() throws IOException, SolrServerException {//solrj的服务对象SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");//添加document文档SolrInputDocument document = new SolrInputDocument();document.addField("id", 1);document.addField("title", "solr是一个企业级的搜索应用服务器");document.addField("content", "solr是一个独立的企业级搜索应用服务器, 用户可以通过http请求访问这个服务器, 获取或者写入对应的内容, 其底层是Lucene");solrServer.add(document);//提交 solrServer.commit();}//solrj写入多个索引 @Testpublic void createMultiIndexToSolr() throws IOException, SolrServerException {//solrj的服务对象SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");//添加document文档List<SolrInputDocument> list = new ArrayList<SolrInputDocument>();for (int i = 1; i < 6; i++) {SolrInputDocument document = new SolrInputDocument();document.addField("id", i);document.addField("title", "solr是一个企业级的搜索应用服务器 " + i);document.addField("content", "solr是一个独立的企业级搜索应用服务器, 用户可以通过http请求访问这个服务器, 获取或者写入对应的内容, 其底层是Lucene " + i);list.add(document);}solrServer.add(list);//提交 solrServer.commit();}//solrj写入一个索引(使用JavaBean方式) @Testpublic void createIndexJavaBeanToSolr() throws IOException, SolrServerException {//solrj的服务对象SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");//添加JavaBean信息TestBean bean = new TestBean();bean.setId("6");bean.setName("solr");bean.setTitle("solr title");bean.setContent("solr content");solrServer.addBean(bean);//提交 solrServer.commit();}//solrj写入多个索引(使用JavaBean方式) @Testpublic void createMultiIndexJavaBeanToSolr() throws IOException, SolrServerException {//solrj的服务对象SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");//添加JavaBean信息List<TestBean> list = new ArrayList<TestBean>();for (int i = 7; i < 10; i++) {TestBean bean = new TestBean();bean.setId(i + "");bean.setName("solr " + i);bean.setTitle("solr title " + i);bean.setContent("solr content " + i);list.add(bean);}solrServer.addBeans(list);//提交 solrServer.commit();}//solrj进行删除操作 @Testpublic void deleteIndexToSolr() throws IOException, SolrServerException {//solrj的服务对象SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");//执行删除操作//根据id删除solrServer.deleteById("9");//根据条件删除 条件基本格式: 字段的名称:字段的值solrServer.deleteByQuery("content:solr content 8");//提交 solrServer.commit();} }
查询操作:
//基础查询 返回原生document @Testpublic void baseQuery() throws SolrServerException {//创建solr的服务对象SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");//执行查询操作SolrQuery query = new SolrQuery("*:*");QueryResponse response = solrServer.query(query);SolrDocumentList documents = response.getResults();for (SolrDocument document : documents) {Object id = document.get("id");Object name = document.get("name");Object title = document.get("title");Object content = document.get("content");System.out.println(id + " " + name + " " + title + " " + content);}}//基础查询 返回JavaBean @Testpublic void javaBeanQuery() throws SolrServerException {SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");SolrQuery query = new SolrQuery("*:*");QueryResponse response = solrServer.query(query);List<TestBean> list = response.getBeans(TestBean.class);for (TestBean bean : list) {System.out.println(bean);}}//复杂查询*********************************************************//公共方法public void baseToSolr(SolrQuery solrQuery) throws SolrServerException {//创建solr的服务对象SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");//执行查询操作QueryResponse response = solrServer.query(solrQuery);//返回JavaBean listList<TestBean> list = response.getBeans(TestBean.class);for (TestBean bean : list) {System.out.println(bean);}}// 在solr中, 如果要使用模糊查询 需要使用一个符号 ~//~ 默认最大的编辑2次//注意: 如果搜索条件中包含了特殊符号, 需要在条件的两边添加双引号或者单引号 @Testpublic void fuzzQueryToSolr() throws SolrServerException {SolrQuery solrQuery = new SolrQuery("content:'sol~'~");baseToSolr(solrQuery);}//通配符查询//?代表一个字符 *代表多个字符 @Testpublic void wildCardQueryToSolr() throws SolrServerException {SolrQuery solrQuery = new SolrQuery("content:so*");baseToSolr(solrQuery);}//solr范围查询: 数值范围 日期范围 文本范围(采用字典顺序)//使用格式: [ start TO end ] 包含边界值//建议关键字都为大写(跟查询条件的内容进行区分)//1 2 3 4 5 6 7 8 10 11 22 33 @Testpublic void rangeQueryToSolr() throws SolrServerException {SolrQuery solrQuery = new SolrQuery("id:[1 TO 3]");baseToSolr(solrQuery);}//布尔查询: lucene中: MUST NOT SHOULD// solr: AND NOT OR @Testpublic void booleanQueryToSolr() throws SolrServerException {SolrQuery solrQuery = new SolrQuery("content:企业级 NOT name:solr");baseToSolr(solrQuery);}// 子表达式查询// 格式: (条件1 [OR AND NOT] 条件2) [OR AND NOT] (条件1 [OR AND NOT] 条件2) @Testpublic void biaoDaShiQueryToSolr() throws SolrServerException {SolrQuery solrQuery = new SolrQuery("(content:lucene OR name:solr) AND id:[1 TO 2]");baseToSolr(solrQuery);}
其他高级操作:(排序,分页,高亮)
//solr排序 @Testpublic void sortQueryToSolr() throws SolrServerException {SolrQuery solrQuery = new SolrQuery("*:*");solrQuery.setSort(new SolrQuery.SortClause("id", "desc"));// 参数1 默认排序的字段 参数2 排序的方式solrQuery.setSort("id", SolrQuery.ORDER.desc);baseToSolr(solrQuery);}//solr分页 @Testpublic void limitQueryToSolr() throws SolrServerException {int page = 3; //当前页int pageSize = 3; //每页显示的条数SolrQuery solrQuery = new SolrQuery("*:*");//设置从第几条开始 每页几条solrQuery.setStart((page - 1) * pageSize);solrQuery.setRows(pageSize);solrQuery.setSort(new SolrQuery.SortClause("id", "desc"));// 参数1 默认排序的字段 参数2 排序的方式solrQuery.setSort("id", SolrQuery.ORDER.desc);baseToSolr(solrQuery);}//solr高亮 @Testpublic void highlighterQueryToSolr() throws SolrServerException {//1. 创建solrj的服务对象SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");//2. 执行查询SolrQuery solrQuery = new SolrQuery("content:solr");//高亮参数的设置solrQuery.setHighlight(true);//开启高亮solrQuery.addHighlightField("content");//设置高亮的字段,支持添加多个solrQuery.addHighlightField("title");solrQuery.setHighlightSimplePre("<font color='red'>"); //设置高亮标签的前缀solrQuery.setHighlightSimplePost("</font>"); //设置高亮的后缀//solrQuery.setHighlightSnippets(1); //进行分片高亮 默认为1, 主要针对多值的情况 QueryResponse response = solrServer.query(solrQuery);//获取高亮的内容:Map<String, Map<String, List<String>>> map = response.getHighlighting();//获取高亮的结果集/*** 最外层的map:* key: 文档的id值** 内层的map* key: 高亮的字段* 内层中list:* 存放的就是高亮的内容,默认集合中只有一个**/for (String docId : map.keySet()) {//System.out.println(docId);Map<String, List<String>> listMap = map.get(docId);for (String highlighterField : listMap.keySet()) {//System.out.println(highlighterField);List<String> list = listMap.get(highlighterField);//获取到高亮的内容System.out.println("集合的长度:" + list.size());System.out.println(highlighterField + " " + list.get(0));}}}
posted @ 2017-11-13 18:57 青衫仗剑 阅读( ...) 评论( ...) 编辑 收藏