pojo configuration:
=================
1. The first thing to do is to place an @Indexed annotation on the entity that will be
searchable through Hibernate Search
2.The second (and last) mandatory thing to do is to add a @DocumentId on the
entity’s identity property. Hibernate Search uses this property to make the link
between a database entry and an index entry
package com.manning.hsia.dvdstore.model;
@Indexed
public class Item {
@DocumentId
private Integer id;
@Field
private String title;
@Field
private String description;
@Field(index=Index.UN_TOKENIZED, store=Store.YES)
private String ean;
private String imageURL;
//public getters and setters
}
=================================================================
While building a query seems like a lot of steps, it’s a very easy process. In summary:
1 Build the Lucene query (using one of the query parsers or programmatically).
String searchQuery = "title:Batman OR description:Batman";
QueryParser parser = new QueryParser("title",new StandardAnalyzer());
org.apache.lucene.search.Query luceneQuery;
try {
luceneQuery = parser.parse(searchQuery);
}
catch (ParseException e) {
throw new RuntimeException("Unable to parse query: " + searchQuery, e);
}
An analyzer is responsible for breaking sentences into individual words.
2 Wrap the Lucene query inside a Hibernate Search query.
FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);
org.hibernate.Query query = ftSession.createFullTextQuery(luceneQuery,Item.class);
3 Optionally set some query properties (such as pagination).
query.setFirstResult(20)
.setMaxResults(20);
4 Execute the query
List results = query.list();
for (Item item : (List
display( "title: " + item.getTitle() + "\nDescription: " +item.getDescription() );
}
No comments:
Post a Comment