SpringBoot集成Solr

tech2023-12-18  73

SpringBoot集成Solr

solr搜索引擎搭建详细过程多字段匹配配置到springboot项目中

solr小白,所以查了很多相关的博客,最终在项目中实现了全文搜索,思路大概为:下载solr,然后配置到Tomcat,在solr中先实现分词和全局查询,再配置到项目中,代码控制即可。

solr搜索引擎搭建详细过程

参考:solr搜索引擎搭建详细过程

多字段匹配

schema.xml文件中做类似的配置

<!-- tbaclarea --> <field name="pid" type="text_ik" indexed="true" stored="true"/> <field name="shortname" type="text_ik" indexed="true" stored="true"/> <field name="longitude" type="text_ik" indexed="true" stored="true"/> <field name="latitude" type="text_ik" indexed="true" stored="true"/> <field name="level" type="text_ik" indexed="true" stored="true"/> <field name="sort" type="text_ik" indexed="true" stored="true"/> <field name="status" type="text_ik" indexed="true" stored="true"/> <!-- 综合查询--> <field name="fault_all" type="text_ik" indexed="true" stored="true" multiValued="true"/> <!-- 将fault_name的分词复制到fault_reason、fault_solve中 按照fault_name检索时会检索这两个字段 --> <defaultSearchField>fault_all</defaultSearchField> <solrQueryParser defaultOperator="AND"/> <copyField source="pid" dest="fault_all"/> <copyField source="name" dest="fault_all"/> <copyField source="shortname" dest="fault_all"/> <copyField source="longitude" dest="fault_all"/> <copyField source="latitude" dest="fault_all"/> <copyField source="level" dest="fault_all"/> <copyField source="sort" dest="fault_all"/> <copyField source="status" dest="fault_all"/>

配置到springboot项目中

pom.xml中添加如下依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>

application.yml中做如下配置

spring: application: name: solr data: solr: host: http://127.0.0.1:8080/solr

实体类文件

package com.fc.test.model.auto; import org.apache.solr.client.solrj.beans.Field; import org.springframework.data.annotation.Id; import org.springframework.data.solr.core.mapping.Indexed; import org.springframework.data.solr.core.mapping.SolrDocument; import java.io.Serializable; import java.util.Date; @SolrDocument(collection="collection1") public class SolrTbAclUser implements Serializable { /** 人员uuid **/ //@ApiModelProperty(value = "人员uuid") @Field("id") @Id private String useruuid; /** 用户名 **/ //@ApiModelProperty(value = "用户名") @Indexed private String username; /** 登录名 **/ //@ApiModelProperty(value = "登录名") @Indexed private String userlogin; ...

实现类

package com.fc.test.service; import com.fc.test.model.auto.SolrTbAclUser; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Sort; import org.springframework.data.solr.core.SolrTemplate; import org.springframework.data.solr.core.query.SimpleQuery; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @Service public class SolrTbAclUserService{ @Autowired private SolrClient solrClient; public void solrsearchtbacluser() throws IOException, SolrServerException { // 设置查询 // SolrTbAclUser solrTbAclUser = new SolrTbAclUser(); // solrTbAclUser.setUseruuid("admin74578578578578"); // solrTbAclUser.setCreateTime(new Date()); // solrTbAclUser.setUserlevel("11"); // solrClient.addBean(solrTbAclUser); // solrClient.commit(); // String id ="admin"; // SolrDocument solrDocument = solrClient.getById(id); SolrQuery query = new SolrQuery(); query.set("q","user_all:物流*"); //设置每页显示多少条 query.setRows(10); //发起搜索请求 QueryResponse response = solrClient.query(query); SolrDocumentList docs = response.getResults(); // 查询结果总数 long cnt = docs.getNumFound(); System.out.println("总条数为"+cnt+"条"); for (SolrDocument doc : docs) { System.out.println("object:"+ doc); System.out.println("fieldValueMap======"+doc.getFieldValueMap()); } System.out.println("byobject=================="+docs); solrClient.close(); //获取filedName // Collection<String> fieldNames = solrDocument.getFieldNames(); //获取file名和内容 // Map<String, Object> fieldValueMap = solrDocument.getFieldValueMap(); // int childDocumentCount = solrDocument.getChildDocumentCount(); // List<SolrDocument> childDocuments = solrDocument.getChildDocuments(); // System.out.println("byId=================="+solrDocument); // System.out.println("fieldNames=================="+fieldNames); // System.out.println("fieldValueMap=================="+fieldValueMap); // System.out.println("childDocumentCount=================="+childDocumentCount); // System.out.println("childDocuments=================="+childDocuments); } }
最新回复(0)