日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SSM+solr 通过商品搜索学习solr的简单使用

發布時間:2023/12/1 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SSM+solr 通过商品搜索学习solr的简单使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

學習了一下https://github.com/TyCoding/ssm-redis-solr這個github上的solr搜索功能,現在來記錄一下。

我的理解就是solr有點類似于數據庫,但它是有索引的數據庫,按很多字段建立索引,可能是b+樹或者散列索引,然后就能夠實現海量數據的查找。solr通過導入jar包就可以對這個庫就行增刪改查了,后端逃不掉的增刪改查。。。

?1.配置tomcat

具體我就不說了,因為我是直接用了github上配置好的,畢竟站在巨人的肩膀上學習嘛

地址:https://github.com/TyCoding/solr-tomcat

2.訪問solr并使用

訪問端口:localhost:8080/solr/index.html

這里的new_core就是項目中配置的路徑,就將商品的索引放在這里。

然后用Test測試它的使用,測試的時候要引入配置文件,不然會導致空指針錯誤,我居然現在才知道。怪不得以前只要用Autowired的時候就會空指針錯誤。。,而且還要@Runwith注解,引入包import org.springframework.test.context.junit4.*;eclipse點擊不會有import提示,需要自己加上去。

?

?這里新建了一個實體對象,然后把這個實體對象加入到索引庫里,在solr索引庫里面就可以找到這個字段

在new_core的schema里面就以Id建好了索引

以及很多的信息

@Testpublic void testFindById() {Goods goods = solrTemplate.getById(1, Goods.class);System.out.println("--------" + goods.getTitle());}

通過id查找,控制臺會輸出你剛剛插入的數據,也就是通過solrTemplate找到了你的數據。

@Testpublic void testAddList() {List<Goods> list = new ArrayList<Goods>();//循環插入100條數據for (int i = 0; i < 100; i++) {BigDecimal price=new BigDecimal (2.3);Goods goods = new Goods(i + 1L, "華為Mate" + i,price, "手機", "手機", "華為專賣店");list.add(goods);}solrTemplate.saveBeans(list); //添加集合對象,調用saveBeans();添加普通對象類型數據,使用saveBean();solrTemplate.commit(); //提交}

還可以批量插入數據,或者分頁查詢

@Testpublic void testPageQuery() {Query query = new SimpleQuery("*:*");query.setOffset(20); //開始索引(默認0)query.setRows(20); //每頁記錄數(默認10)ScoredPage<Goods> page = solrTemplate.queryForPage(query, Goods.class);System.out.println("總記錄數:" + page.getTotalElements());List<Goods> list = page.getContent();}

3.學習一下項目中怎么配置

注意要在web.xml加一個過濾,不然注入不了solrTemplate這個bean

?

spring-solr.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:solr="http://www.springframework.org/schema/data/solr"xsi:schemaLocation="http://www.springframework.org/schema/data/solrhttp://www.springframework.org/schema/data/solr/spring-solr-1.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- solr服務器地址 --><solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr/new_core"/><!-- solr模板,使用solr模板可對索引庫進行CRUD的操作 --><bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"><constructor-arg ref="solrServer"/></bean></beans>

就是加載一個solr的模板

?

SolrUtil.java

把數據庫的數據庫批量加入

@Component public class SolrUtil {@Autowiredprivate GoodsMapper goodsMapper;@Autowiredprivate SolrTemplate solrTemplate;/*** 實現將數據庫中的數據批量導入到Solr索引庫中*/public void importGoodsData() {List<Goods> list = goodsMapper.findAll();System.out.println("====商品列表====");for (Goods goods : list) {System.out.println(goods.getTitle());}solrTemplate.saveBeans(list);solrTemplate.commit(); //提交System.out.println("====結束====");}public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring*.xml");SolrUtil solrUtil = (SolrUtil) context.getBean("solrUtil");solrUtil.importGoodsData();} }

?

?這樣就把數據加入索引庫中。

實體類有一個Field標識這個實體字段在索引庫里的名稱

@Fieldprivate Long id; //商品ID@Field("item_title")private String title; //商品標題@Field("item_price")private BigDecimal price; //商品價格@Field("item_image")private String image; //商品圖片@Field("item_category")private String category; //商品類別@Field("item_brand")private String brand; //商品品牌@Field("item_seller")private String seller; //商品賣家

最后,搜索功能的實現

按價格查找

//按價格區間查詢if (searchMap.get("price") != null) {if (!searchMap.get("price").equals("")) {String[] price = ((String) searchMap.get("price")).split("-");if (!price[0].equals("0")) {//如果起點區間不等于0Criteria filterCriteria = new Criteria("item_price").greaterThanEqual(price[0]);FilterQuery filterQuery = new SimpleFilterQuery(filterCriteria);query.addFilterQuery(filterQuery);}if (!price[1].equals("*")) {//如果區間重點不等于*Criteria filterCriteria = new Criteria("item_price").lessThanEqual(price[1]);FilterQuery filterQuery = new SimpleFilterQuery(filterCriteria);query.addFilterQuery(filterQuery);}}}

?4.實現效果

?

轉載于:https://www.cnblogs.com/HannahLihui/p/10104416.html

總結

以上是生活随笔為你收集整理的SSM+solr 通过商品搜索学习solr的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。