-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 
Author Message
 Post subject: search qeury for specifc fields on specific objects
PostPosted: Mon Jun 07, 2010 4:25 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
Hi!

I would like to add query for objects and fields per object.

How can I do it? I saw that projection is for all fields and FullTextQuery is for all objects.

How can it be mapped fields per object?


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Mon Jun 07, 2010 5:14 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Have you actually read the online manual before asking? I suggest you have a closer look at "Chapter 5 - Querying"

--Hardy


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 5:08 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
Yes.

Actually yes.

and saw this:

Code:
org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, Book.class );
query.setProjection( "id", "summary", "body", "mainAuthor.name" );
List results = query.list();


But I need something like this:

Code:
org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, Book.class , User.Class);
query.setProjection( "id", "summary", "body", "id");
List results = query.list();


Where the first id is from Book.class and the second id is from User.class.

How can I do it?


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 5:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Are your Book and User classes related - meaning are they in a @IndexedEmbedded relationship. In this case you might have something like:

Code:
query.setProjection( ProjectionConstants.ID, "summary", "body", "user.name" );


If the entities are in no relationship you can add another projection constant to determine the class type

Code:
query.setProjection( ProjectionConstants.ID, ProjectionConstants.OBJECT_CLASS, "summary", "body", "name" );


While iterating over the object array you can then the class type by looking at the second projection value.

--Hardy


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 5:27 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
Thanks

Your second example was exactly what i needed.


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 5:44 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
One more thing:

How will the projection know that the id is for user class and not for book class?

It can be any other field - i may need to search name field in user class and not in book class?

again, user and book have no relationship.


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 5:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
The projection does not have to know for which entity type it returns an id. In fact it does not even know that it is a Hibernate id. Projection basically return the values stored in the Lucene document. It's up to you to interpret the values.

--Hardy


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 7:20 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
So if it doesn't know, how would it search for name of type user and not of type book?


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 7:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
From a Lucene (search) point of view there are only Documents which are effectively key/value pairs. If the value of a field name belongs to a User or Book is from a Lucene point of view irrelevant. Hibernate Search always adds the fields ProjectionConstants.ID and ProjectionConstants.OBJECT_CLASS to each document. Using this additional information Hibernate (or in this case you) can always decide whether a User or Book was matched.

Have a look at a created index using the Lucene tool Luke. Looking at the actual indexed documents makes things often much clearer.

--Hardy


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 7:47 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
Thanks

I understood it.

My question is how in the same search I can define to HIBERNATE to use name for User and title for Book for my Lucene query?


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 8:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
It depends on your query. For example:
Code:
org.apache.lucene.queryParser.QueryParser parser = new QueryParser("title", new StandardAnalyzer() );

// we tell Lucene to search the 'name' field for 'foo' or the 'summary' field for 'bar'
org.apache.lucene.search.Query luceneQuery = parser.parse( "name:foo Or summary:bar" );

// by specifying Book.class and User.class as vararg arguments we tell Hibernate Search which indexes to search
// in this case the index for Book and the one for User
org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, Book.class , User.Class );

// instead of returning managed objects of type User or Book we want to use projection and return object arrays
query.setProjection( ProjectionConstants.ID, ProjectionConstants.OBJECT_CLASS, "summary", "body", "name" );
List results = query.list();


--Hardy


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 9:02 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
Thank you for helping.

I still don't understand how will it know that he has to check summery only field only in Book object and name field only in User object. (there could be summery in User object as well!)


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 9:44 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
It doesn't - if summary is also a field in the User object then it will search this field. This is the price you pay executing your search against two indexes (User + Book index). You also will have to be careful about the logical operators between terms.


--Hardy


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 10:01 am 
Beginner
Beginner

Joined: Sun Jan 24, 2010 3:04 am
Posts: 20
So it means that I will have to create 2 different calls.

How does it influence the performace?


Top
 Profile  
 
 Post subject: Re: search qeury for specifc fields on specific objects
PostPosted: Tue Jun 08, 2010 10:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
That's something you will have to measure. Projections are fast and making multiple queries should not be a problem. But it all depends on your requirements.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.