Chapter 1. Getting started
主要记录一下和Hibernate Search 相关的配置,Spring 和 JPA的配置请查看相关文章。
Hibernate Search 的配置相当简单,主要涉及三个方面:(本文是基于jpa的)
1、配置Hibernate Search,修改JPA配置文件:persistence.xml
<?xml version="1.0" encoding="utf-8"?> <persistence ...> <persistence-unit ...> <properties> ... <!-- hibernate search --> <property name="hibernate.search.default.directory_provider" value="filesystem"/> <property name="hibernate.search.default.indexBase" value="索引文件存储位置"/> </properties> </persistence-unit> </persistence>
属性hibernate.search.default.directory_provider告诉hibernate使用哪个DirectoryProvider实现。在Apache Lucene中有一个概念Directory来保存Index Files。Hibernate通过DirectoryProvider来初始化和配置一个Lucene Directory实例。在本例中,我们使用一个能把Index Files保存在file system中的DirectoryProvider。当Index Files保存在file system中时,我们可以通过Luke工具实时查看Lucene索引文件。除了DirectoryProvider外,还需要告诉hibernate索引文件保存在哪个具体的目录中,这通过hibernate.search.default.indexBase属性来配置。
2、添加相应的jar依赖
hibernate-search-4.1.1.Final\dist:
hibernate-search-engine-4.1.1.Final.jar
hibernate-search-orm-4.1.1.Final.jar
hibernate-search-4.1.1.Final\dist\lib\required:
antlr-2.7.7.jar
avro-1.5.1.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.3.Final.jar
jackson-core-asl-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
lucene-core-3.5.0.jar
paranamer-2.3.jar
slf4j-api-1.6.1.jar
snappy-java-1.0.1-rc3.jar
3、为实体添加hibernate search 相关annotation
package example;...@Entity @Indexed public class Book { @Id @GeneratedValue private Integer id; @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO) private String title; @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO) private String subtitle; @Field(index = Index.YES, analyze=Analyze.NO, store = Store.YES) @DateBridge(resolution = Resolution.DAY) private Date publicationDate; @IndexedEmbedded @ManyToMany private Set<Author> authors = new HashSet<Author>(); public Book() { } // standard getters/setters follow here ... }
package example;...@Entitypublic class Author { @Id @GeneratedValue private Integer id; @Field private String name; public Author() { } // standard getters/setters follow here ...}
- @Indexed标注需要索引的实体
- 如果有@Id标志,则不需要显示声明@DocumentId,因为lucene就不需要生成唯一标志来区分索引。
- @Fileld标明为需要搜索的属性。默认@Filed的参数是
index=Index.YES
,analyze=Analyze.YES
andstore=Store.NO,即进行索引,分词,不保存具体内容到索引。
Note :无论store=Store.NO还是store=Store.YES都不会影响最终的搜索能力。store.YES的作用是可以在搜索后可以直接从索引中获取域的完整值。在hibernate中,如果store=Store.NO,搜索结果中,域的值是通过数据库中获取,如果store=Store.YES,域的值是直接从索引文档中获取。 - 由于lucene都是基于String进行索引 (Lucene2.9后支持数值索引),所以hibernate search使用@Bridge标签来转换数据类型,比如@DateBridge,时间数据转换
-
由于lucene索引是平整的数据结构(flat data structure),无法识别对象关联关系
@ManyToMany
,@*ToOne
,@Embedded
and@ElementCollection,hibernate search,为了让上面的书的作者能被识别,使用@IndexedEmbedded标签
1.4. Indexing
- //使用Hibernate Session初始化索引
- FullTextSession fullTextSession = Search.getFullTextSession(session);
- fullTextSession.createIndexer().startAndWait();
Hibernate Session创建和运行搜索
- FullTextSession fullTextSession = Search.getFullTextSession(session);
- Transaction tx = fullTextSession.beginTransaction();
- // create native Lucene query unsing the query DSL
- // alternatively you can write the Lucene query using the Lucene query parser
- // or the Lucene programmatic API. The Hibernate Search DSL is recommended though
- QueryBuilder qb = fullTextSession.getSearchFactory()
- .buildQueryBuilder().forEntity( Book.class ).get();
- org.apache.lucene.search.Query query = qb
- .keyword()
- .onFields("title", "subtitle", "authors.name", "publicationDate")
- .matching("Java rocks!")
- .createQuery();
- // wrap Lucene query in a org.hibernate.Query
- org.hibernate.Query hibQuery =
- fullTextSession.createFullTextQuery(query, Book.class);
- // execute search
- List result = hibQuery.list();
- tx.commit();
- session.close();
通过JPA创建和运行搜索
- EntityManager em = entityManagerFactory.createEntityManager();
- FullTextEntityManager fullTextEntityManager =
- org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
- em.getTransaction().begin();
- // create native Lucene query unsing the query DSL
- // alternatively you can write the Lucene query using the Lucene query parser
- // or the Lucene programmatic API. The Hibernate Search DSL is recommended though
- QueryBuilder qb = fullTextSession.getSearchFactory()
- .buildQueryBuilder().forEntity( Book.class ).get();
- org.apache.lucene.search.Query query = qb
- .keyword()
- .onFields("title", "subtitle", "authors.name", "publicationDate")
- .matching("Java rocks!");
- .createQuery();
- // wrap Lucene query in a javax.persistence.Query
- javax.persistence.Query persistenceQuery =
- fullTextEntityManager.createFullTextQuery(query, Book.class);
- // execute search
- List result = persistenceQuery.getResultList();
- em.getTransaction().commit();
- em.close();
1.6. Analyzer
-
在配置文件中设置 默认分词器
hibernate.search.analyzer
. -
用标注的方式在实体上设置分词器
.@Analyzer
-
用标注的方式在field域上设置分词器
@
.Analyzer
@AnalyzerDef
and the Solr framework to define and use an analyzer@Entity@Indexed @AnalyzerDef(name = "customanalyzer", tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class), filters = { @TokenFilterDef(factory = LowerCaseFilterFactory.class), @TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = { @Parameter(name = "language", value = "English") }) }) public class Book { @Id @GeneratedValue @DocumentId private Integer id; @Field @Analyzer(definition = "customanalyzer") private String title; @Field @Analyzer(definition = "customanalyzer") private String subtitle; @IndexedEmbedded @ManyToMany private Set<Author> authors = new HashSet<Author>(); @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES) @DateBridge(resolution = Resolution.DAY) private Date publicationDate; public Book() { } // standard getters/setters follow here ... }
最后更新:2017-04-03 18:52:02