Quantcast
Channel: Technical Blogs » lucene query
Viewing all articles
Browse latest Browse all 2

Lucene and Hibernate – Text searching within ORM wrapper

$
0
0


So we have one kind of searching application with lucene query in Lucene search – a workable example.

Also let us assume, we have hibernate related knowledge in our previous works. If we do not have that much, we can google with Hibernate tutorial. Because again this post is not the place to elaborate hibernate ORM ideas.

Now as we have fantastic object relational mapping tool hibernate and search library Lucene, let us dig in about the topic that how hibernate and lucene can be used together…

No… we do not have to invent any wheel, just look for Hibernate Lucene Search in hibernate site. The hard part i.e. the integration of these two libraries is already done. We will use it to get advantage of ORM over text searching.

So what are we going to do now??…

1> We will make an Eclipse based Project.

2> We add Hibernate search 3.4.1.jar and the reference jars in the Project Library.

3> Make Simple POJO class in hibernate.

4> Make Annotations for the hbernate to table mappings. Like (@Entity, @Table) and others as required.

5> Put hibernate lucene specific annotations in the pojo file. This is a highly important step. I am trying to put some imortant annotaions here.
A> @Indexed – by which name lucene will create the directory for indexing in lucene.
B> @AnalyzerDefs and @AnalyzerDef, these are required to tell lucene by which analyser library lucene will analyze the conts for every object.

For example, if we use phonetic analyzer for our application search, the example code snippet is below -

@AnalyzerDef(name="phonetic",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = { @TokenFilterDef(factory = PhoneticFilterFactory.class,
params = {
@Parameter(name = "encoder", value = "Metaphone"),
@Parameter(name = "inject", value = "false")
})
})

Here library specific to phonetic analyzer will be provided in hibernate seacrh jars. So we do not have to care about this.

Also we have to define the analyzer definition which is below -

@Analyzer(definition="doublephonetic")

Now we have to put an unique id for the document for lucene, which we will do by giving @EmbeddedId @DocumentId annotations.

Sample of this is -

@EmbeddedId @DocumentId

For the fields to be marked as lucene serach fields and the indexing we will use @Field annotation.

Sample of this is -

@Field(index=Index.TOKENIZED , store=Store.YES)
private String symptomdescription;

Parameter “index” will tell lucene whether this field is to be tokenised by analyzer or not.
Parameter “store” will tell lucene whether this field will be stored in the lucene directory or not.

6>Next step is to use lucene indexing in the cotainer of the Hibernate.

As we have put different information already in pojo file, now we have to put only threee key lines to make the index work.

Session session = SessionFactoryUtil.getFactory().getCurrentSession();

Opening a session throgh hibernate.

FullTextSession fullTextSession = Search.getFullTextSession(session);

open a session for lucene text search.

fullTextSession.createIndexer().startAndWait();

indexing the documents and prepared for any further indexing.

7> Now we look at the searching part through hibernate lucene search -

After opening the Session with full textsession we have to start transaction -

fSession.beginTransaction();

assumption – fSession is fulltextsession variable.

Prepare the searchfield string array -

String[] searchFields = {“Application Search fields”}; // targeted fields

Create a parser to parse a full text query

SearchFactory sf = fSession.getSearchFactory();

Creating the QueryParser Object -

QueryParser parser = null;
parser = new MultiFieldQueryParser(Version.LUCENE_31, profileFields, sf.getAnalyzer("phonetic"),boostPerField);

Create the Lucene Query Objects -

Query lucenceQuery = null;
lucenceQuery = parser.parse(searchQuery);

And get the actual query object lists -

org.hibernate.search.FullTextQuery hibQuery =
fSession.createFullTextQuery(lucenceQuery, <>.class);
List results = hibQuery.list();

Lastly close the tarnsaction session -

fSession.getTransaction().commit();

This is it.
As we are getting the object list here, we can type convert it to our required pojo object list and work on those as per application requirement.

I have deployed and tested the application in Tomcat 7 and JBoss 7.1.0Beta1. Both are working fine.

Screeshots to show the sample application -

Actual Search Result

Search Screen

Here “Get Actual Result” button returns the search result.
And “Get Score Result” button returns the score.
Also I have provided Phonetic and Double Phonetic Analyze option in UI.
Below screens are showing the results.

Actual Search Result –

Search Result

Search Result

Above is the search result screen with the highlight term option. I have shown the all the fields where I have populated data.  I have not shown the highlighting code here, because my aim was to work with the hibernate search with basic code. I will explain the highlighting in later posts that I have planned.

Scoring Result Screen : -

Lucene Scoring

Lucene Scoring

This is the score result screen. It confirms that results are in higher to lower ranking order.

So last but not the least –
My suggestion is –
Gain as much as knowledge by using Hibernate Search as much possible in various complex business cases. And check for updates in my next posts in searching techniques with lucene.

Mail me for source code reference in piyas.de@gmail.com – I will be happy to share those and discuss complex search areas of yours.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images