Spring Boot整合Solr

安装Solr服务

1.下载地址:https://www.apache.org/dyn/closer.lua/lucene/solr/8.1.1/solr-8.1.1.zip
2.solr8.0的简单搭建(一):https://www.cnblogs.com/ITDreamer/p/10661873.html
3.solr8.0 ik中文分词器的简单配置(二):https://www.cnblogs.com/ITDreamer/p/10661949.html
4.solr8.0 从数据库导入数据(三):https://www.cnblogs.com/ITDreamer/p/10662016.html
5.managed-schema文件配置:(放最后)

	<fieldType name="text_ik" class="solr.TextField">
	  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
	</fieldType>
	
	<field name="item_title" type="text_ik" indexed="true" stored="true"/>
	<field name="item_sell_point" type="text_ik" indexed="true" stored="false"/>
	<field name="item_price"  type="pdouble" indexed="true" stored="true"/>
	<field name="item_show_image" type="string" indexed="false" stored="true" />
	<field name="item_category_name" type="string" indexed="true" stored="false" />
	<field name="item_desc" type="text_ik" indexed="true" stored="false" />
	<field name="item_spsc" type="text_ik" indexed="true" stored="false" />

	<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
	<copyField source="item_title" dest="item_keywords"/>
	<copyField source="item_sell_point" dest="item_keywords"/>
	<copyField source="item_category_name" dest="item_keywords"/>
	<copyField source="item_desc" dest="item_keywords"/>
	<copyField source="item_spsc" dest="item_keywords"/>

Spring Boot整合Solr

1.搭建Spring Boot工程,导入依赖

pom.xml:
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.5.RELEASE</version>
	</parent>
	<!-- solr -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-solr</artifactId>
	</dependency>

2.创建Solr返回对象映射类

package com.wode369.pojo.custom;

import java.io.Serializable;

import org.apache.solr.client.solrj.beans.Field;

public class SolrItemCustom implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2247152138438556188L;

	@Field
	private String id;
	
	@Field("item_title")
	private String title;

	@Field("item_price")
	private Double price;

	@Field("item_show_image")
	private String showImage;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public String getShowImage() {
		return showImage;
	}

	public void setShowImage(String showImage) {
		this.showImage = showImage;
	}

	@Override
	public String toString() {
		return "IndexItemCustom [id=" + id + ", title=" + title + ", price=" + price + ", showImage=" + showImage + "]";
	}

}

3.创建测试方法

package com.wode369.test;

import java.util.List;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Crotch;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.test.context.junit4.SpringRunner;

import com.wode369.pojo.custom.SolrItemCustom;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SolrTest {

	@Autowired
	SolrTemplate solrTemplate;
	@Autowired
	SolrClient solrClient;

	@Test
	public void solrTest() throws Exception {

		/第一种查询方法/
		// // 创建查询对象
		// SolrQuery query = new SolrQuery();
		// // 设置查询条件
		// query.setQuery("120");
		// // 设置分页
		// query.setStart(1);
		// query.setRows(10);
		// // 设置默认搜素域
		// query.set("df", "item_keywords");
		// QueryResponse queryResponse = solrClient.query("collection1", query);
		// // 取查询结果
		// SolrDocumentList solrDocumentList = queryResponse.getResults();
		// for (SolrDocument solrDocument : solrDocumentList) {
		// System.out.println(solrDocument);
		// }
		// Optional<SolrItemCustom> optional = solrTemplate.query(collection, query,
		// clazz)getById("collection1", "6e0205963f72444aa34175015e55a7eb",
		// SolrItemCustom.class);
		// System.out.println(optional.get());

		/第二种查询方法/
		Query query = new SimpleQuery();
		Criteria criteria = new Criteria("item_keywords");
		query.addCriteria(criteria);
		Page<SolrItemCustom> page = solrTemplate.queryForPage("collection1", query, SolrItemCustom.class);
		List<SolrItemCustom> list = page.getContent();
		for (SolrItemCustom solrItemCustom : list) {
			System.out.println(solrItemCustom);
		}
	}
}

4.solrTemplate使用:https://blog.csdn.net/ab102a/article/details/94397550

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值