为了账号安全,请及时绑定邮箱和手机立即绑定

Hibernate Serach 5.9全文检索快速入门

标签:
JavaScript

Hibernate Search是基于Lucene的全文检索框架,可以很好的整合Hibernate,实现快速检索实体类。我们今天主要来介绍Hibernate Serach的基础入门。

开发环境准备——使用Maven搭建开发环境

DEMO使用Spring Data JPA(1.10) + Hibernate Search(5.9)来实现。

以下为本次开发的pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.itheima</groupId>    <artifactId>hibernatesearch-demo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <properties>        <hibernate-version>5.2.12.Final</hibernate-version>        <hibernate-search-version>5.9.1.Final</hibernate-search-version>        <spring-version>4.2.4.RELEASE</spring-version>        <jdk-version>1.8</jdk-version>    </properties>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.0</version>                <configuration>                    <target>${jdk-version}</target>                    <source>${jdk-version}</source>                </configuration>            </plugin>        </plugins>    </build>    <dependencies>        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-core</artifactId>            <version>${hibernate-version}</version>        </dependency>        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-entitymanager</artifactId>            <version>${hibernate-version}</version>        </dependency>        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-search-orm</artifactId>            <version>${hibernate-search-version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring-version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring-version}</version>        </dependency>        <dependency>            <groupId>org.hibernate.javax.persistence</groupId>            <artifactId>hibernate-jpa-2.1-api</artifactId>            <version>1.0.0.Final</version>        </dependency>        <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-jpa</artifactId>            <version>1.10.4.RELEASE</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.6</version>        </dependency>        <!-- IK中文分词器 -->        <dependency>            <groupId>cn.bestwu</groupId>            <artifactId>ik-analyzers</artifactId>            <version>5.1.0</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>druid</artifactId>            <version>1.1.6</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>        </dependency>        <dependency>            <groupId>commons-lang</groupId>            <artifactId>commons-lang</artifactId>            <version>2.6</version>        </dependency>    </dependencies></project>

Spring配置文件

Spring整合了JPA以及Spring Data JPA的配置文件

<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa"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-4.2.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"><context:component-scan base-package="com.itheima"></context:component-scan><!--加载外部properties配置文件--><context:property-placeholder location="classpath:config.properties"/><!--配置Druid数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">    <property name="url" value="${jdbc.url}" />    <property name="username" value="${jdbc.username}" />    <property name="password" value="${jdbc.password}" />    <property name="driverClassName" value="${jdbc.driver}" /></bean><!-- Spring整合JPA --><bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">    <property name="dataSource" ref="dataSource"/>    <property name="packagesToScan" value="com.itheima.entity"/>    <property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"></property>    <property name="jpaProperties">        <props>            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>            <prop key="hibernate.show_sql">false</prop>            <prop key="hibernate.format_sql">false</prop>            <prop key="hibernate.hbm2ddl.auto">update</prop>            <!-- 将索引存放在文件系统 -->            <prop key="hibernate.search.default.directory_provider">filesystem</prop>            <!-- 指定用于保存索引的目录 -->            <prop key="hibernate.search.default.indexBase">G:/workspace/free_test/t26/index</prop>        </props>    </property></bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">    <property name="dataSource" ref="dataSource"/>    <property name="entityManagerFactory" ref="entityManagerFactory"/></bean><!-- 配置Spring Data JPA包扫描自动创建代理对象 --><jpa:repositories base-package="com.itheima.repository"     entity-manager-factory-ref="entityManagerFactory"    transaction-manager-ref="transactionManager"/><!-- 开启声明式事务 --><tx:annotation-driven transaction-manager="transactionManager"/></beans>

数据库配置文件

jdbc.url=jdbc:mysql:///demojdbc.driver=com.mysql.jdbc.Driverjdbc.username=rootjdbc.password=000000

在实体类中开启全文检索

编写实体类

package com.itheima.entity;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;import org.hibernate.search.annotations.Analyze;import org.hibernate.search.annotations.Analyzer;import org.hibernate.search.annotations.Field;import org.hibernate.search.annotations.FieldBridge;import org.hibernate.search.annotations.Index;import org.hibernate.search.annotations.Indexed;import org.hibernate.search.annotations.NumericField;import org.hibernate.search.annotations.Store;import org.wltea.analyzer.lucene.IKAnalyzer;@Entity@Table(name = "t_product")// 使用@Indexed表示这个实体类需要创建索引@Indexed// 指定使用IK分词器@Analyzer(impl=IKAnalyzer.class)public class Product {    // 默认使用@Id注解修饰的字段作为索引的唯一标识符    @Id    @GenericGenerator(name = "sysuuid", strategy = "uuid")    @GeneratedValue(generator = "sysuuid")    private String id;    // 默认为字段建立索引、进行分词(分析)、不存储    @Field(store=Store.YES)    private String name; // 商品名称    @Field(store=Store.YES)    private String description; // 商品描述    @Field(store=Store.YES)    // 如果是数字字段,需要使用@NumericField进行修饰    @NumericField(forField="price")    private Double price; // 商品价格    @Field(store=Store.YES)    private String catelog; // 商品分类    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }    public Double getPrice() {        return price;    }    public void setPrice(Double price) {        this.price = price;    }    public String getCatelog() {        return catelog;    }    public void setCatelog(String catelog) {        this.catelog = catelog;    }    @Override    public String toString() {        return "Product [id=" + id + ", name=" + name + ", description=" + description + ", price=" + price                + ", catelog=" + catelog + "]";    }}

编写DAO接口

package com.itheima.repository;import org.springframework.data.jpa.repository.JpaRepository;import com.itheima.entity.Product;public interface ProductRepository extends JpaRepository<Product, String>{}

编写Service接口

package com.itheima.service;import java.util.List;import com.itheima.entity.Product;public interface ProductService {    void save(Product product);    List fullTextQuery(String[] fields, String keyword);    List rangeQuery(String fields, double from, double to);}

编写Service实现

package com.itheima.service;import java.util.List;import javax.persistence.EntityManager;import javax.transaction.Transactional;import org.hibernate.search.jpa.FullTextEntityManager;import org.hibernate.search.jpa.Search;import org.hibernate.search.query.dsl.QueryBuilder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.itheima.entity.Product;import com.itheima.repository.ProductRepository;@Service@Transactionalpublic class ProductServiceImpl implements ProductService {    @Autowired    private ProductRepository productRepository;    @Autowired    private EntityManager entityManager;    @Override    public void save(Product product) {        productRepository.save(product);    }        /**        * 根据关键字进行全文检索        */    @Override    public List fullTextQuery(String[] fields, String keyword) {        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);        QueryBuilder qb = fullTextEntityManager.getSearchFactory()                .buildQueryBuilder().forEntity(Product.class).get();        org.apache.lucene.search.Query luceneQuery = qb            .keyword()            .onFields(fields)            .matching(keyword)            .createQuery();        javax.persistence.Query jpaQuery             = fullTextEntityManager.createFullTextQuery(luceneQuery, Product.class);        return jpaQuery.getResultList();    }    /**    * 根据数值范围进行检索    */    @Override    public List rangeQuery(String field, double from, double to) {        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);        QueryBuilder qb = fullTextEntityManager.getSearchFactory()                .buildQueryBuilder().forEntity(Product.class).get();        org.apache.lucene.search.Query luceneQuery = qb            .range()            .onField(field)            .from(from)            .to(to)            .createQuery();        javax.persistence.Query jpaQuery             = fullTextEntityManager.createFullTextQuery(luceneQuery, Product.class);        return jpaQuery.getResultList();    }}

测试代码:
package com.itheima.service.test;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.commons.lang.math.RandomUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;
import com.itheima.entity.Product;
import com.itheima.service.ProductService;

// 整合Spring Junit@RunWith(SpringJUnit4Cla***unner.class)

@Autowiredprivate ProductService productService;/*** 创建测试数据,Hiberante Search会自动创建索引*/@Testpublic void saveTest01() {    for(int i = 0; i < 100; ++i) {        Product product = new Product();        product.setCatelog("手机");        product.setDescription("小米 红米5A 全网通版 3GB+32GB 香槟金 移动联通电信4G手机 双卡双待");        product.setName("红米5A" + i);        product.setPrice(RandomUtils.nextDouble() * 100 % 100);        productService.save(product);    }}@Testpublic void fullTextQueryTest01() {    List list = productService.fullTextQuery("catelog,description,name".split(","), "移动");    for (Object object : list) {        System.out.println(object);    }}@Testpublic void rangeQueryTest01() {    List list = productService.rangeQuery("price", 10.0D, 20D);    for (Object object : list) {        System.out.println(object);    }}

}

@RunWith(SpringJUnit4Cla***unner.class)<br/" rel="nofollow">

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消