[SpringBoot系列]--Spring Hibernate search 注解实现(未测试)

1.maven项目pom.xml加入依赖

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-search-orm</artifactId>

<version>5.5.0.Final</version>

</dependency>

<dependency>

<groupId>org.apache.lucene</groupId>

<artifactId>lucene-queryparser</artifactId>

<version>5.3.0</version>

</dependency>

<!--中文分词器>

<dependency>

<groupId>org.apache.lucene</groupId>

<artifactId>lucene-analyzers-smartcn</artifactId>

<version>5.3.0</version>

</dependency>

 

2.在application.xml中添加配置

<property name="hibernateProperties">
<props>
//文件系统
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
//索引存储目录
<prop key="hibernate.search.default.indexBase">e:/index</prop>
</props>
</property>

 

3.domain(bean)中实体类配置

 

类上注解

 注释@Indexed将实体类标记为需要通过Hibernate Search进行索引的实体。

 注释@Analyzer(impl=SmartChineseAnalyzer.class),我配置了中文分词器,默认是StandardAnalyzer.calss

类中属性注解

  Hibernate Search需要将实体标识符存储在每个实体的索引中。默认情况下,它将使用标记为@Id的字段,但您可以使用@DocumentId(仅限高级用户)覆盖此字段。

  注释@Field用于实体类属性上,其中参数的默认值为index = Index.YES,analyze = Analyze.YES和store = Store.NO。

  Lucene索引主要是基于字符串的,还有一些额外的数字类型支持。有关更多详情,请参阅Bridges。

关联实体的索引

  @IndexedEmbedded注释用于索引关联实体,如通常通过@ManyToMany,@OneToOne,@ManyToOne,@Embedded和@ElementCollection定义的实体,有关详细信息,请参阅嵌入式和关联对象。

4.注解实体类例子

@Entity
@Table(name = "t_task")
@Indexed
@Analyzer(impl=SmartChineseAnalyzer.class)
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@DocumentId
private Long id;

@Column(name="c_taskname")
@Field
private String taskName;//任务名称
@Column(name="c_address")
private String address;//工作地点
@Column(name = "c_content")
@Field
private String content;// 任务内容

 

5.全文检索索引初始化

  /*全文搜索索引初始化*/
public void initFullTextIndex() throws Exception{
FullTextSession fullTextSession = Search.getFullTextSession(currentSession());
fullTextSession.createIndexer().startAndWait();
}

 

6.全文检索查询

/*全文搜索*/
public List fullTextSearch(){
FullTextSession fullTextSession = Search.getFullTextSession(currentSession());
Transaction tx = fullTextSession.beginTransaction();
QueryBuilder qb = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity(entityType).get();
org.apache.lucene.search.Query query = qb .keyword() .onFields("taskName", "content") .matching("扫黄") .createQuery();
org.hibernate.Query hibQuery =  fullTextSession.createFullTextQuery(query, entityType);
List result = hibQuery.list();
return result;
}

 

 

转载于:https://www.cnblogs.com/tanwei81/p/6816532.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建独立的、生产级的Spring应用程序的框架。在Spring Boot中,使用@SpringBootApplication注解来标注应用程序的入口类,该注解包含了许多其他注解和配置项,以便简化Spring应用程序的开发和部署过程。在入口类中,可以使用ApplicationPidFileWriter来写入一个PID文件,以便在需要时可以从外部关闭应用程序。 对于与数据库的持久化层,可以使用Spring Boot提供的spring-boot-starter-data-jpa依赖。通过引入这个依赖,可以轻松地使用JPA(Java Persistence API)来访问数据库。 下面是一个示例入口文件App.java的代码,展示了如何使用Spring Boot和JPA来创建一个应用程序: ```java package com.pingcap; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.ApplicationPidFileWriter; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(App.class); springApplication.addListeners(new ApplicationPidFileWriter("spring-jpa-hibernate.pid")); springApplication.run(args); } } ``` 这个示例中,通过@SpringBootApplication将App类标记为Spring Boot应用程序的入口类,然后使用ApplicationPidFileWriter将应用程序的PID写入名为"spring-jpa-hibernate.pid"的文件中。最后,通过调用SpringApplication的run方法来启动应用程序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [使用 Spring Boot 构建 TiDB 应用程序](https://blog.csdn.net/TiDBer/article/details/126522878)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值