引言
我们如果在项目中使用solr肯定要用java语言来操作它,而这个操作的入口就是SolrJ,下面就介绍一下如何使用SolrJ来操作Solr
第一步:引入jar包
上面这些jar包来源与下面两个路径:
第二部分:
基本操作的代码
package com.itheima.solr; import java.util.List; import java.util.Map; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrQuery.ORDER; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.junit.Test; /** * @author zhenghao * @date 创建时间:2017年8月2日 上午11:31:09 * @version 1.0 * @parameter * @since * @return */ public class SolrManager { @Test public void testAdd() throws Exception { String baseURLString = "http://localhost:8080/solr"; // 单机版 SolrServer solrServer = new HttpSolrServer(baseURLString); SolrInputDocument doc = new SolrInputDocument(); doc.setField("id", "haha"); doc.setField("name", "范冰冰"); // 添加 solrServer.add(doc); solrServer.commit(); } // 删除 @Test public void testDelete() throws Exception { String baseURLString = "http://localhost:8080/solr/collection2"; // 单机版 SolrServer solrServer = new HttpSolrServer(baseURLString); // 删除 solrServer.deleteByQuery("*:*", 1000); } // update @Test public void testUpdate() throws Exception { String baseURLString = "http://localhost:8080/solr/collection2"; // 单机版 SolrServer solrServer = new HttpSolrServer(baseURLString); // 更新 // 与添加相同 只要id相同 则为更新 id不同则为添加 } // 查询 @Test public void testSearch() throws Exception { String baseURLString = "http://localhost:8080/solr"; // 单机版 SolrServer solrServer = new HttpSolrServer(baseURLString); // 查询关键词 SolrQuery solrQuery = new SolrQuery(); // 关键词 // solrQuery.set("q", "*:*"); solrQuery.setQuery("item_title:三星 Note II (N7100) 云石白 联通3G手机"); // solrQuery.set("fq", "product_catalog_name:幽默杂货"); // 价格在10元以下 solrQuery.set("fq", "item_price:[* TO 100000]"); // 价格排序 solrQuery.addSort("item_price", ORDER.desc); // 分页 solrQuery.setStart(0);// 从第几页开始 solrQuery.setRows(5);// 每页显示几条 // // 默认域 solrQuery.set("df", "item_title"); // 只查询指定域 solrQuery.set("fl", "id,item_title,item_sell_point,item_price,item_num,item_image"); // // 高亮 // // 打开高亮开关 solrQuery.setHighlight(true); // 指定高亮域 solrQuery.addHighlightField("item_title"); // 前缀 solrQuery.setHighlightSimplePre("<span style='color:red'>"); // 后缀 solrQuery.setHighlightSimplePost("</span>"); // 执行查询 QueryResponse response = solrServer.query(solrQuery); // 文档结果集 SolrDocumentList docs = response.getResults(); // 取出高亮的文档对象 Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); // 解释 高亮对象的文档结构 大Map 套小 map 小map 套 list // Map K id V Map // Map K 域名 V list // List list.get(0) // // 总条数 long numFound = docs.getNumFound(); System.out.println(numFound); for (SolrDocument doc : docs) { System.out.println(doc.get("item_price")); System.out.println(doc.get("item_num")); Map<String, List<String>> map = highlighting.get(doc.get("id")); // List<String> list = map.get("item_title"); // System.out.println(list.get(0)); } } }
如果我们和框架进行整合以后,solrServer的创建则交由spring容器来管理:
<!-- 配置SOlrJ --> <bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg value="http://localhost:8080/solr/collection1"/> </bean>
小结
以上就是solrJ的一些基本操作,这些就是我们在项目中常用的一些代码,尤其是查询的操作,在查询的时候比较不好写的就是查询条件的设置,其余的都比较容易!