1. 程式人生 > >spring data solr的使用,spring整合solr

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();
    }
}