spring data solr的使用,spring集成solr

1.介绍

Spring Data Solr就是为了方便Solr的开发所研制的一个框架,其底层是对SolrJ(官方API)的封装。

2.使用 solrTemplate

创建maven工程 springdatasolr-demo

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

<dependencies>
 <!--Spring-Data-Solr-->
 <dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-solr</artifactId>
   <version>1.5.5.RELEASE</version>
 </dependency>

 <!--spring测试依赖-->
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>4.1.9.RELEASE</version>
   <scope>test</scope>
 </dependency>

 <!-- Test dependencies -->
 <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
 </dependency>
</dependencies>

(2)在src/main/resources/spring下创建 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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:solr="http://www.springframework.org/schema/data/solr"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/solr
        http://www.springframework.org/schema/data/solr/spring-solr.xsd">

    <!--指定solr地址-->
    <solr:solr-server id="solrServer" url="http://192.168.211.128:28081/solr/collection1" />

    <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg ref="solrServer" />
    </bean>
</beans>

配置文件中Field域

要使用solr存储和搜索数据,就像数据库中表的column列一样,必须配置域来映射实体类中的字段

修改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中的数据复制到另一个域中

<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"/>

动态域

添加多个类似的字段, 后期扩展字段, 域也要对应繁琐的添加, 为了避免重复操作, 使用动态域

1

后期即使需要添加字段, 只要字段名满足item_spec_*这种格式,就可以自动生成该域一一对应

动态域需要使用 @Dynamic 注解

@Dynamic
@Field("item_spec_*")
private Map<String,String> specMap;
public Map<String, String> getSpecMap() {
    return specMap;
}

@Filed注解

在实体pojo类中, 想要将属性名和配置文件中定义的域名要对应发生映射关系,就需要用到@Field注解

public class Item{
    @Field("item_title")
    private String title;

    @Field("item_price")
    private BigDecimal price;

    ...
}

代码 增删改查

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-solr.xml")
public class SolrTest {

    @Autowired
    private SolrTemplate solrTemplate;

    /**
     * 添加数据的方法
     */
    @Test
    public void add(){
        Item item = new Item();
        item.setId(1L);
        item.setBrand("华为");
        item.setCategory("手机");
        item.setGoodsId(1L);
        item.setSeller("华为深圳专卖店");
        item.setTitle("华为p20");
        item.setPrice(new BigDecimal(2000));
        item.setImage("假装有图片地址");
        solrTemplate.saveBean(item);
    }

    /**
     * 根据id获取
     */
    @Test
    public void selectById(){
        Item item = solrTemplate.getById(1, Item.class);
        System.out.println("item = " + item);
    }

    /**hua
     * 根据条件查询,分页查询
     */
    @Test
    public void select(){
        // 查询所有
        Query query = new SimpleQuery("*:*");

        // 设置条件
        Criteria criteria = new Criteria("item_title").contains("华为");
        query.addCriteria(criteria);

        //设置分页
        query.setOffset(0); //开始索引(默认0)
        query.setRows(15);   //每页记录数(默认10)

        ScoredPage<Item> pages = solrTemplate.queryForPage(query, Item.class);
        System.out.println("pages.getTotalElements() = " + pages.getTotalElements());
        List<Item> content = pages.getContent();
        for (Item item : content) {
            System.out.println("item = " + item);
        }
    }

    /***
     * 全部删除
     */
    @Test
    public void delete(){
        Query query=new SimpleQuery("*:*");
        solrTemplate.delete(query);
        solrTemplate.commit();
    }
}

本文来自 zzzgd_666 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/zzzgd_666/article/details/80718220?utm_source=copy

猜你喜欢

转载自blog.csdn.net/weixin_43242688/article/details/82953800