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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

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

?

學(xué)習(xí)了一下https://github.com/TyCoding/ssm-redis-solr這個(gè)github上的solr搜索功能,現(xiàn)在來(lái)記錄一下。

我的理解就是solr有點(diǎn)類(lèi)似于數(shù)據(jù)庫(kù),但它是有索引的數(shù)據(jù)庫(kù),按很多字段建立索引,可能是b+樹(shù)或者散列索引,然后就能夠?qū)崿F(xiàn)海量數(shù)據(jù)的查找。solr通過(guò)導(dǎo)入jar包就可以對(duì)這個(gè)庫(kù)就行增刪改查了,后端逃不掉的增刪改查。。。

?1.配置tomcat

具體我就不說(shuō)了,因?yàn)槲沂侵苯佑昧薵ithub上配置好的,畢竟站在巨人的肩膀上學(xué)習(xí)嘛

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

2.訪問(wèn)solr并使用

訪問(wèn)端口:localhost:8080/solr/index.html

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

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

?

?這里新建了一個(gè)實(shí)體對(duì)象,然后把這個(gè)實(shí)體對(duì)象加入到索引庫(kù)里,在solr索引庫(kù)里面就可以找到這個(gè)字段

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

以及很多的信息

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

通過(guò)id查找,控制臺(tái)會(huì)輸出你剛剛插入的數(shù)據(jù),也就是通過(guò)solrTemplate找到了你的數(shù)據(jù)。

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

還可以批量插入數(shù)據(jù),或者分頁(yè)查詢(xún)

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

3.學(xué)習(xí)一下項(xiàng)目中怎么配置

注意要在web.xml加一個(gè)過(guò)濾,不然注入不了solrTemplate這個(gè)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服務(wù)器地址 --><solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr/new_core"/><!-- solr模板,使用solr模板可對(duì)索引庫(kù)進(jìn)行CRUD的操作 --><bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"><constructor-arg ref="solrServer"/></bean></beans>

就是加載一個(gè)solr的模板

?

SolrUtil.java

把數(shù)據(jù)庫(kù)的數(shù)據(jù)庫(kù)批量加入

@Component public class SolrUtil {@Autowiredprivate GoodsMapper goodsMapper;@Autowiredprivate SolrTemplate solrTemplate;/*** 實(shí)現(xiàn)將數(shù)據(jù)庫(kù)中的數(shù)據(jù)批量導(dǎo)入到Solr索引庫(kù)中*/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("====結(jié)束====");}public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring*.xml");SolrUtil solrUtil = (SolrUtil) context.getBean("solrUtil");solrUtil.importGoodsData();} }

?

?這樣就把數(shù)據(jù)加入索引庫(kù)中。

實(shí)體類(lèi)有一個(gè)Field標(biāo)識(shí)這個(gè)實(shí)體字段在索引庫(kù)里的名稱(chēng)

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

最后,搜索功能的實(shí)現(xiàn)

按價(jià)格查找

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

?4.實(shí)現(xiàn)效果

?

轉(zhuǎn)載于:https://www.cnblogs.com/HannahLihui/p/10104416.html

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。