一澳骤、項(xiàng)目結(jié)構(gòu)
二粹舵、pom.xml
在pom.xml文件中添加springboot、solr、freemarker相關(guān)的依賴包
? <dependencies>
? ? ? <dependency>
? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? <artifactId>spring-boot-starter-freemarker</artifactId>
? ? ? </dependency>
? ? ? <dependency>
? ? ? ? ? <groupId>cn.hutool</groupId>
? ? ? ? ? <artifactId>hutool-all</artifactId>
? ? ? ? ? <version>5.2.0</version>
? ? ? </dependency>
? ? ? <!--spring boot項(xiàng)目必須引入的依賴web-->
? ? ? <dependency>
? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId>
? ? ? </dependency>
? ? ? <!--spring boot項(xiàng)目引入的solr的依賴-->
? ? ? <!-- 引入solr庫連接jar-->
? ? ? <dependency>
? ? ? ? ? <groupId>org.apache.solr</groupId>
? ? ? ? ? <artifactId>solr-solrj</artifactId>
? ? ? </dependency>
? ? ? <dependency>
? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? <artifactId>spring-boot-starter-data-solr</artifactId>
? ? ? </dependency>
? ? ? <dependency>
? ? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? ? <version>1.16.14</version>
? ? ? ? ? <scope>provided</scope>
? ? ? </dependency>
? </dependencies>
三、制作.ftl模板
1、創(chuàng)建excel模板宵荒,并填充一些假數(shù)據(jù),方便后續(xù)定位
2净嘀、另存為xml格式
3报咳、修改后綴為ftl
4、處理ftl文件
記得將ss:ExpandedRowCount="5"刪掉挖藏,因?yàn)楹竺嫜h(huán)遍歷的時(shí)候肯定會超過行數(shù)為5的限制暑刃,刪除就表示不限制表格的行數(shù)
5、將stat.ftl放入resources/templates
四膜眠、在springboot上使用solr進(jìn)行多字段分組統(tǒng)計(jì)數(shù)據(jù)
這里需要注意因?yàn)椴榭碒ttpSolrClient源碼發(fā)現(xiàn)DEFAULT_PATH默認(rèn)是"/select"岩臣,并且沒找到可以修改path的地方,所以如果solr服務(wù)有自定義/select的情況下宵膨,就可以拷貝源碼架谎,然后在本地新建一個(gè)同名的HttpSolrClient,可以參考我的項(xiàng)目結(jié)構(gòu)
拓展知識:solr中q與fq參數(shù)的區(qū)別?
如果用q=content:"中國",就會查找所有content中包含"中國"的辟躏,然后order by score,這時(shí)就會按content的相似度來排序谷扣。
而如果用fq,q設(shè)置為*:*。那么solr會先order by score,而因?yàn)闂l件是*:*,所以所有文檔的相關(guān)性都是一樣的捎琐,所以即使用fq過濾了会涎,返回排在最前面的卻不是相關(guān)性最高的。
綜上所述瑞凑,以相關(guān)性(score)為優(yōu)先排序條件的在塔,那么條件一定放在q參數(shù)中,而fq僅僅只是條件的過濾而沒有權(quán)重相關(guān)性為優(yōu)先排序拨黔。?
五、封裝完數(shù)據(jù)并渲染到ftl模板绰沥,導(dǎo)出EXCEL
大家可能會遇到模板導(dǎo)出的時(shí)候報(bào)錯(cuò)篱蝇,提示template not found(模板未找到),這時(shí)候需要到application.yml配置下freemarker模板的加載路徑徽曲,其實(shí)還有挺多默認(rèn)的配置零截,但是只要這兩行相關(guān)的配置就能滿足普遍需要,如有特別的配置需求可到官網(wǎng)查看
源碼地址:https://github.com/AsLightJava/openJava/tree/main/solr
如有不清楚或者遇到問題的秃臣,可下面留言聯(lián)系我涧衙。