Search capability is one of the primary demands of a application. Engines like Lucene provide search capabilities,
but with complex applications it can be tricky to implement. It’s tough to keep the index up to date, deal with the mismatch between the index structure and the domain model, handle querying conflicts, and so on.
Hibernate Search is an enterprise search tool based on Hibernate Core and Apache Lucene. It provides full text search capabilities for Hibernate-based applications without the infrastructural code required by other search engines. With this free, open-source technology, you can quickly add high-powered search features in an intelligent, maintainable way.
Hibernate Search 4.4.1.Final released which is compatibility with Hibernate ORM 4.2.8
Hibernate Search transparently indexes your objects and offers fast regular, full-text and geolocation search. Ease of use and easy clustering are core. Full-text search for entities Offers full-text search support for objects stored by Hibernate ORM, Infinispan and other sources. Think of it as Google search for your entities:
- search words with text
- order results by relevance
- find by approximation
Cluster friendly
Clustering indexes is not trivial. Hibernate Search offers several easy to setup clustering strategies:
master / slaves
JMS / JGroups replication
Infinispan distributed index
Faceting and geolocation
Geolocalized entities are as easy as @Spatial. Filter results around a certain location liek the user position. Hibernate Search offers to algorithms: a light one or a more scalable one.
Faceting categorizes results by properties like price range or brand.
Easy to use
Design to be easy to use from the ground up. Handles indexing, datastore synchronization, clustering and infrastructure transparently while you focus on
the business sense of your queries.
Prerequisite:
- Java Runtime
A JDK or JRE version 6 or greater.
- Hibernate Search
hibernate-search-4.4.1.Final.jar and all runtime dependencies (either from Sourceforge or the Maven central repository).
- Hibernate ORM
You will need hibernate-core-4.2.6.Final.jar and its dependencies (either from Sourceforge or the Maven central repository).
- JPA 2
Even though Hibernate Search can be used without needing JPA annotations the following instructions will use them for basic entity configuration
(@Entity, @Id, @OneToMany, @Indexed, @DocumentId, @Field,……). This part of the configuration could also be expressed in xml or code.
If you want to use MAVEN then this is all you need to add to your pom.xml to get started:
Maven artifact identifier for Hibernate Search
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>4.4.1.Final</version>
</dependency>
Optional Maven dependencies for Hibernate Search
<!– If using JPA (2), add: –>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.6.Final</version>
</dependency>
<!– Additional Analyzers: –>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-analyzers</artifactId>
<version>4.4.1.Final</version>
</dependency>
<!– Infinispan integration: –>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-infinispan</artifactId>
<version>4.4.1.Final</version>
</dependency>
Basic configuration options to be added to hibernate.properties, hibernate.cfg.xml or persistence.xml
…
<property name=”hibernate.search.default.directory_provider”
value=”filesystem”/>
<property name=”hibernate.search.default.indexBase”
value=”/var/lucene/indexes”/>
…
Example entities after adding Hibernate Search annotations:
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;
…
@Entity
public class Author {
@Id
@GeneratedValue
private Integer id;
@Field
private String name;
public Author() {
}
// standard getters/setters follow here
…
}
Using JPA to create and execute a search
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 = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Book.class).get();
org.apache.lucene.search.Query query = qb
.keyword()
.onFields(“title”, “subtitle”, “authors.name”)
.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();
- Salesforce Certified Platform Developer I – Winter ’18 Release Exam - November 23, 2017
- Salesforce Certified Platform App Builder – Winter ’18 Release Exam - November 22, 2017
- Salesforce Certified Administrator – Winter’ 18 Release Exam - November 21, 2017