981
技術社區[雲棲]
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