Professional Documents
Culture Documents
4130-Rc032-010d-Hibernate Search 0 1
4130-Rc032-010d-Hibernate Search 0 1
4130-Rc032-010d-Hibernate Search 0 1
www.dzone.com
#32
CONTENTS INCLUDE:
n
Mapping Entities
Bridges
Querying Indexes
Hibernate Search
By John Griffin
URL
Lucene
http://lucene.apache.org/java/docs/
Hibernate Search
http://www.hibernate.org/410.html
Mailing lists
http://www.hibernate.org/20.html
JIRA
http://opensource.atlassian.com/projects/hibernate/secure/
Dashboard.jspa
Hot
Tip
Version
Hibernate Core
DZone, Inc.
Annotations
Entity
Manager
Search
3.2.6 GA
3.2.x, 3.3.x
3.2.x, 3.3.x
3.0.x
3.3.0 SP1
3.4.x
3.4.x
3.1.x
3.3.x
3.0.x
3.4.x
3.1.x
3.3.1 GA
3.2.x
3.4.0 GA
3.3.x
Hibernate
EntityManager
3.3.2 GA
3.2.x
3.3.x
3.0.x
3.4.0 GA
3.3.x
3.4.x
3.1.x
3.0.0 GA
3.2.x
3.3.x
3.3.x
3.0.x
3.1.0 GA
3.3.x
3.4.x
3.4.x
3.1.x
3.0.1 GA
>= 3.2.2
(better if
>= 3.2.6)
3.3.x (better
if >= 3.3.1 )
3.3.x
3.1.0
Beta1
3.3
3.4
3.4
Hibernate
Shards
3.0.0
Beta2
3.2.x
3.3.x
Not
compatible
3.0.x
Hibernate
Tools
3.2.2
3.2.x
3.2.x and
3.3.x
3.2.x and
3.3.x
(3.2.0)
Hibernate
Validator
Core
Hibernate
Annotations
GETTING STARTED
Hibernate
Search
>\kjlggfik]fi?`Y\ieXk\
JBoss Enterprise Application Platform
includes Hibernate
JBoss Enterprise Application Platform pre-integrates
JBoss Application Server, Seam, and Hibernate
Includes caching, clustering, messaging, transactions, and
integrated web services stack
Support for industry-leading Java and technologies like
JAX-WS, EJB 3.0, JPA 1.0, JSF 1.2, and JTA 1.1
Use JBoss Operations Network to monitor and tune
Hibernate queries
www.dzone.com
Hot
Tip
Table 3, continued
hibernate.search.filter.cache_
strategy
hibernate.search.filter.cache_bit_
results.size
Configuration Parameters
MAPPING ENTITIES
BRIDGES
Parameter
Description
hibernate.search.autoregister_
listeners
hibernate.search.indexing_strategy
hibernate.search.analyzer
hibernate.search.similarity
hibernate.search.worker.batch_size
hibernate.search.worker.backend
Build-in Bridge
Description
String
StringBridge
no-op
short / Short
ShortBridge
int / Integer
IntegerBridge
hibernate.search.worker.thread_
pool.size
long / Long
LongBridge
float / Float
FloatBridge
hibernate.search.worker.buffer_
queue.max
double /
Double
DoubleBridge
BigDecimal
BigDecimalBridge
BigInteger
BigIntegerBridge
boolean /
Boolean
BooleanBridge
Class
ClassBridge
Enum
EnumBridge
Use enum.name()
URL
UrlBridge
URI
UriBridge
hibernate.search.worker.execution
hibernate.search.worker.jndi.*
hibernate.search.worker.jms.
connection_factory
hibernate.search.worker.jms.queue
hibernate.search.reader.strategy
www.dzone.com
Bridges, continued
Date
DateBridge
Associations, continued
Listing 4 shows that @ContainedIn is paired to an @IndexedEmbedded
annotation on the other side of the bi-directional relationship.
@Entity @Indexed
public class Item {
@ManyToMany
@IndexedEmbedded
private Set<Actor> actors; // embed actors when indexing
@ManyToOne
@IndexedEmbedded
private Director director; // embed director when indexing
...
}
@Entity @Indexed
public class Actor {
@Field private String name;
@ManyToMany(mappedBy=actors)
@ContainedIn actor is contained in item index (1)
private Set<Item> items;
...
}
@Entity @Indexed
public class Director {
@Id @GeneratedValue @DocumentId private Integer id;
@Field private String name;
@OneToMany(mappedBy=director)
@ContainedIn director is contained in item index
private Set<Item> items;
...
}
Embeddable Objects
Embedded objects in Java Persistence (they are called components in Hibernate) are objects whose life cycle entirely depends
on the owning entity. When the owning entity is deleted, the
embedded object is deleted as well.
Hot
Tip
ANALYZERS
Analyzers are responsible for taking text as input, chunking it
into individual words (tokens) and optionally applying some
operations (filters) on the tokens. A filter can alter the stream
of tokens as it pleases. It can remove, change, and add words.
@Entity
@Indexed
public class Item {
// mark the association for indexing
@IndexedEmbedded private Rating rating;
...
}
Associations
Associations between objects are similar to embeddable objects
except that an associated objects life time is not dependent on
the owning entity. Below is an example association mapping.
@Entity @Indexed
@AnalyzerDef(
name=applicationanalyzer, // analyzer definition name
tokenizer =
// tokenizer factory
@TokenizerDef(factory = StandardTokenizerFactory.
class ),
filters = {
// list of filters to apply
@TokenFilterDef(factory=LowerCaseFilterFactory.
class),
@TokenFilterDef(factory = StopFilterFactory.
class,
// parameters passed to the filter factory
params = {
@Parameter(name=words,
value=com/manning/hsia/dvdstore/
stopwords.txt),
@Parameter(name=ignoreCase, value=true)
} )
} )
// Use the pre defined analyzer
@Analyzer(definition=applicationanalyzer)
public class Item {
...
}
DZone, Inc.
www.dzone.com
Manually
First you need an instance of either a FullTextEntityManager
or a FullTextSession depending on whether or not you are
using an EntityManager.
Listing 6: Manually indexing data.
SessionFactory factory =
new AnnotationConfiguration().buildSessionFactory();
Session session = factory.openSession();
FullTextSession fts =
org.hibernate.search.Search.getFullTextSession(session);
fts.getTransaction.begin()
for (Item item : items) {
fts.index(item); //manually index an item instance
}
fts.getTransaction().commit(); //index is written at commit time
session.close();
or
EntityManagerFactory factory =
Persistence.createEntityManagerFactory();
EntityManager em = factory.createEntityManager();
FullTextEntityManager ftem =
org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
ftem.getTransaction().begin();
for (Item item : items) {
ftem.index(item); //manually index an item instance
}
//index is written at commit time
ftem.getTransaction().commit();
Hot
Tip
<hibernate-configuration>
<session-factory>
...
<event type=post-update>
<listener class=org.hibernate.search.event
.FullTextIndexEventListener/>
</event>
<event type=post-insert>
<listener class=org.hibernate.search.event
.FullTextIndexEventListener/>
</event>
<event type=post-delete>
<listener class=org.hibernate.search.event
.FullTextIndexEventListener/>
</event>
<!-- collection listener is different -->
<event type=post-collection-recreate>
<listener class=org.hibernate.search.event
.FullTextIndexCollectionEventListener/>
</event>
<event type=post-collection-remove>
<listener class= org.hibernate.search.event
.FullTextIndexCollectionEventListener/>
</event>
<event type=post-collection-update>
<listener class=org.hibernate.search.event
.FullTextIndexCollectionEventListener/>
</event>
</session-factory>
</hibernate-configuration>
getFullTextSession
and getFullTextEntityManager
were named createFullTextSession and
createFullTextEntityManager in
QUERYING INDEXES
Table 5 shows the three ways to obtain results.
Updates, additions and deletions to indexes are handled automatically by Hibernate Search via entity listeners. If you are using
Hibernate Annotations these listeners are automatically wired for
you. If you are not using the annotations then you have to wire
the listeners manually as shown in Listing 8.
DZone, Inc.
Method Call
Description
query.list()
www.dzone.com
@AnalyzerDefs
@Boost
@ClassBridge
@ClassBridges
@ContainedIn
Marks the owning entity as being part of the associated entitys index (to be more accurate, being part
of the indexed object graph). This is only necessary
when an entity is used as a @IndexedEmbedded target
class. @ContainedIn must mark the property pointing
back to the @IndexedEmbedded owning Entity. Not
necessary if the class is an embeddable class.
SessionFactory factory =
new AnnotationConfiguration().buildSessionFactory();
Session session = factory.openSession();
@DateBridge
@DocumentId
FullTextSession fts =
org.hibernate.search.Search.getFullTextSession(session);
@Factory
@Field
@FieldBridge
@Fields
@FullTextFilterDef
query.
iterate()
query.
scroll()
fts.getTransaction.begin()
// create a Term for the description field
Term term = new Term(description, salesman);
TermQuery query = new TermQuery(term);
// generate a FullTextQuery and obtain a result list
org.hibernate.search.FullTextQuery hibQuery =
s.createFullTextQuery(query, Dvd.class);
List<Dvd> results = hibQuery.list();
@FullTextFilterDefs
Query
Description
@Indexed
TermQuery
@IndexedEmbedded
WildcardQuery
PrefixQuery
@Key
PhraseQuery
FuzzyQuery
RangeQuery
Allows you to search for results between two values. Values can
be inclusive or exclusive but not mixed.
BooleanQuery
MatchAllDocsQuery
@ProvidedId
@Similarity
Ex. @Entity
Annotation
Description
@Analyzer
@AnalyzerDef
@Indexed
@Similarity(impl =
BookSpecificSimilarity.public class Book {
...
}
@TokenFilterDef
@TokenizerDef
DZone, Inc.
www.dzone.com
LUKE
The most indispensable utility you can have in your arsenal
of index troubleshooting tools (in fact it may be the only one
you really need) is Luke, shown in Figure 3. With Luke you can
examine any facet of an index you can imagine. Some of its
capabilities are:
n view individual documents
n execute a search, and browse the results
n selectively delete documents from the index
n examine term frequency, and many more...
The Luke author, Andrzej Bialecki, actively maintains Luke to keep
up with the latest Lucene version. Luke is available for download,
in several different formats, at http://www.getopt.org/luke/. The
most current version of the Java WebStart JNLP direct download
is the easiest to retrieve.
Figure 3: The search window of the Luke utility for Lucene indexes.
RECOMMENDED BOOK
John Griffin
John Griffin has been in the software and computer industry in one form
or another since 1969. He remembers writing his first FORTRAN IV program
on his way back from Woodstock. Currently, he is the software engineer/
architect for SOS Staffing Services, Inc. He was formerly the lead e-commerce
architect for Iomega Corporation and an independent consultant for the
Dept. of the Interior, among many other callings. John is a member of the
ACM. Currently, he resides in Layton, Utah with wife Judy and Australian Shepards Clancy
and Molly.
Publications
Blog
as Search clustering.
http://thediningphilosopher.blogspot.com
BUY NOW
books.dzone.com/books/hibernate-search
Available:
Core Mule 2
Core Seam
Essential Ruby
Struts2
SOA Patterns
Spring Annotations
Core .NET
Core Java
C#
PHP
Groovy
JavaServer Faces
DZone, Inc.
1251 NW Maynard
Cary, NC 27513
ISBN-13: 978-1-934238-34-9
ISBN-10: 1-934238-34-1
50795
888.678.0399
919.678.0300
Refcardz Feedback Welcome
refcardz@dzone.com
Sponsorship Opportunities
sales@dzone.com
$7.95
FREE
9 781934 238349
Copyright 2008 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical,
photocopying, or otherwise, without prior written permission of the publisher. Reference: Hibernate Search in Action, Emmanuel Bernard and John Griffin, Manning Publications, December 2008.
Version 1.0