一. 构建实体类Goods
package com.fan.pojo; import org.apache.solr.client.solrj.beans.Field; import org.springframework.data.annotation.Id; import java.io.Serializable; public class Goods implements Serializable { @Override public String toString() { return "Goods{" + "id='" + id + '\'' + ", goodsname='" + goodsname + '\'' + ", price=" + price + ", description='" + description + '\'' + '}'; } @Id private String id; @Field("goodsname") private String goodsname; @Field("price") private float price; @Field("description") private String description; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getGoodsname() { return goodsname; } public void setGoodsname(String goodsname) { this.goodsname = goodsname; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
二、 构建工具类solrUtis
package com.fan.utils; import com.fan.pojo.Goods; import org.apache.log4j.Logger; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.UpdateResponse; import org.apache.solr.common.SolrInputDocument; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * @ClassName SolrUtils * @Description TODO * @Author Fan * @Date 2019/11/25 14:05 * @Version 1.0 */ public class SolrUtils { static Logger logger = Logger.getLogger(SolrUtils.class); private static HttpSolrClient httpSolrClient = null; private SolrUtils(String solrUrl) { /** * 构建httpSolrClient请求对象,solrUrl参数必须到core路径 */ httpSolrClient = new HttpSolrClient.Builder(solrUrl).build(); } /** * @Description: 增加/修改 * @Param: [goods] * @return: void * @Author: Fan * @Date: 2019/11/25 14:24 */ public String addGoods(Goods goods) { UpdateResponse response = null; /** * 构建一个document */ SolrInputDocument solrInputDocument = new SolrInputDocument(); solrInputDocument.setField("id",goods.getId()); solrInputDocument.setField("goodsname",goods.getGoodsname()); solrInputDocument.setField("description", goods.getDescription()); try { response = httpSolrClient.add(solrInputDocument); logger.info("增加响应:" + response); httpSolrClient.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response.toString(); } /** * @Description: 查询列表 * @Param: [] * @return: java.util.List* @Author: Fan * @Date: 2019/11/25 15:51 */ public ListgetGoodsList() { List goodsList = new ArrayList<>(); SolrQuery queryParam = new SolrQuery(); //queryParam.set("q","*:*"); 全部查询 queryParam.set("q","goodsname:*"); //设置相关查询字段 queryParam.addFilterQuery("goodsname:手机"); //fq 设置过滤字段 queryParam.addFilterQuery("price:[1 TO 10000]"); //fq 设置价格过滤 queryParam.addSort("price", SolrQuery.ORDER.desc); //设置排序,根据price queryParam.setStart(0); //设置分页 queryParam.setRows(15); /* * 高亮设置 */ queryParam.setHighlight(true); //开启高亮 queryParam.addHighlightField("goodsname"); //设置高亮字段 queryParam.setHighlightSimplePost(""); //设置高亮样式 queryParam.setHighlightSimplePost(""); try { QueryResponse queryResponse = httpSolrClient.query(queryParam); logger.info("查询响应:" + queryResponse.toString()); queryResponse.getResults().stream().forEach(document -> { Goods goods = new Goods(); goods.setId(document.get("id").toString()); goods.setGoodsname(document.get("goodsname").toString()); goods.setPrice((Float) document.get("price")); goods.setDescription(document.get("description").toString()); goodsList.add(goods); }); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return goodsList; } /** * @Description: 删除根据id * @Param: [id] * @return: void * @Author: Fan * @Date: 2019/11/25 15:51 */ public String deleteGoodsById(String id) { UpdateResponse updateResponse = null; try { updateResponse = httpSolrClient.deleteById(id); logger.info("删除响应:" + updateResponse); httpSolrClient.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return updateResponse.toString(); } public static SolrUtils getInstance(String solrUrl) { return new SolrUtils(solrUrl); } public static void main(String[] args) { SolrUtils solrUtils = SolrUtils.getInstance("http://127.0.0.1:8983/solr/goods_core"); Goods goods = new Goods(); goods.setId("4"); goods.setGoodsname("努比亚手机2.0"); goods.setPrice(1200.0f); goods.setDescription("比红绿机强"); solrUtils.addGoods(goods); solrUtils.getGoodsList().parallelStream().forEach(item -> { System.out.println(item.toString()); }); solrUtils.deleteGoodsById("4"); } }
三、执行之后打印日志
16:18:56,906 INFO SolrUtils:55 - 增加响应:{responseHeader={status=0,QTime=3}}
16:18:57,226 INFO SolrUtils:90 - 查询响应:{responseHeader={status=0,QTime=5,params={q=goodsname:*,hl=true,hl.simple.post=,start=0,fq=[goodsname:手机, price:[1 TO 10000]],sort=price desc,hl.fl=goodsname,rows=15,wt=javabin,version=2}},response={numFound=3,start=0,docs=[SolrDocument{price=6999.0, description=装X神器, goodsname=苹果手机, id=2, _version_=1651141471547424768}, SolrDocument{price=3000.0, description=会爆炸吧, goodsname=三星手机, id=1, _version_=1651141471315689472}, SolrDocument{price=1400.0, description=国民手机, goodsname=小米手机, id=3, _version_=1651141471547424769}]},highlighting={2={goodsname=[苹果手机]},1={goodsname=[三星手机]},3={goodsname=[小米手机]}}} Goods{id='1', goodsname='三星手机', price=3000.0, description='会爆炸吧'} Goods{id='3', goodsname='小米手机', price=1400.0, description='国民手机'} Goods{id='2', goodsname='苹果手机', price=6999.0, description='装X神器'}
16:18:57,239 INFO SolrUtils:117 - 删除响应:{responseHeader={status=0,QTime=1}}
增删查的响应和后台管理页面的响应一样