spring-data-solr搜索入门

编程入门 行业动态 更新时间:2024-10-22 14:35:00

spring-data-solr搜索<a href=https://www.elefans.com/category/jswz/34/1770026.html style=入门"/>

spring-data-solr搜索入门

1.Spring Data Solr简介

虽然支持任何编程语言的能力具有很大的市场价值,你可能感兴趣的问题是:我如何将Solr的应用集成到Spring中?可以,Spring Data Solr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ(官方API)的封装。

2 Spring Data Solr入门小Demo

2.1 搭建工程

(1)创建maven工程,pom.xml中引入依赖


<dependencies><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-solr</artifactId><version>1.5.5.RELEASE</version></dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version></dependency></dependencies>

(2)在src/main/resources下创建 applicationContext-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi="" xmlns:p=""xmlns:context=""xmlns:solr=""xsi:schemaLocation=" .0.xsd .xsd .xsd"><!-- solr服务器地址 --><solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" /><!-- solr模板,使用solr模板可对索引库进行CRUD的操作 --><bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"><constructor-arg ref="solrServer" /></bean>
</beans>

这里配制文件作用:连接上远程服务器(http://127.0.0.1是服务器地址,随实际情况决定)

2.2 @Field 注解

在编写注解之前,请注意solrhome的schema.xml 文件已经设置业务系统 Field
普通域

<field name="item_goodsid" type="long" indexed="true" stored="true"/><field name="item_title" type="text_ik" indexed="true" stored="true"/><field name="item_price" type="double" indexed="true" stored="true"/><field name="item_image" type="string" indexed="false" stored="true" /><field name="item_category" type="string" indexed="true" stored="true" /><field name="item_seller" type="text_ik" indexed="true" stored="true" /><field name="item_brand" type="string" indexed="true" stored="true" />

拷贝域

<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/><copyField source="item_title" dest="item_keywords"/><copyField source="item_category" dest="item_keywords"/><copyField source="item_seller" dest="item_keywords"/><copyField source="item_brand" dest="item_keywords"/>

动态域

<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />	

创建 cn.it.pojo 包,以TbItem实体类为例 ,属性使用@Field注解标识 。 如果属性与配置文件定义的域名称不一致,需要在注解中指定域名称。

public class TbItem implements Serializable{@Fieldprivate Long id;@Field("item_title")private String title;@Field("item_price")private BigDecimal price;@Field("item_image")private String image;@Field("item_goodsid")private Long goodsId;@Field("item_category")private String category;@Field("item_brand")private String brand;@Field("item_seller")private String seller;.......}

.2.3 增加(修改)

创建测试类TestTemplate.java

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:applicationContext-solr.xml")public class TestTemplate {@Autowiredprivate SolrTemplate solrTemplate;@Testpublic void testAdd(){TbItem item=new TbItem();item.setId(1L);item.setBrand("华为");item.setCategory("手机");item.setGoodsId(1L);item.setSeller("华为2号专卖店");item.setTitle("华为Mate9");item.setPrice(new BigDecimal(2000));		solrTemplate.saveBean(item);solrTemplate.commit();}}

2.4 按主键查询

@Testpublic void testFindOne(){TbItem item = solrTemplate.getById(1, TbItem.class);System.out.println(item.getTitle());}

2.5 按主键删除

@Testpublic void testDelete(){solrTemplate.deleteById("1");solrTemplate.commit();}

2.6 分页查询

	@Testpublic void testPageQuery(){Query query=new SimpleQuery("*:*");query.setOffset(20);//开始索引(默认0)query.setRows(20);//每页记录数(默认10)ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);System.out.println("总记录数:"+page.getTotalElements());List<TbItem> list = page.getContent();showList(list);}	//显示记录数据private void showList(List<TbItem> list){		for(TbItem item:list){System.out.println(item.getTitle() +item.getPrice());}		}

2.7 条件查询

@Testpublic void testPageQueryMutil(){	Query query=new SimpleQuery("*:*");Criteria criteria=new Criteria("item_title").contains("2");criteria=criteria.and("item_title").contains("5");		query.addCriteria(criteria);//query.setOffset(20);//开始索引(默认0)//query.setRows(20);//每页记录数(默认10)ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);System.out.println("总记录数:"+page.getTotalElements());List<TbItem> list = page.getContent();showList(list);}

2.8 删除全部数据

@Testpublic void testDeleteAll(){Query query=new SimpleQuery("*:*");solrTemplate.delete(query);solrTemplate.commit();}

更多推荐

spring-data-solr搜索入门

本文发布于:2023-07-28 17:39:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1265202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:入门   spring   data   solr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!