-->
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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: fultext search and associated objects
PostPosted: Thu Apr 12, 2007 4:01 am 
Newbie

Joined: Thu Apr 12, 2007 2:34 am
Posts: 5
I have two classes:
Code:
@Indexed
public class Book implements Serializable
{
   @DocumentId
   private Integer id;

   @ContainedIn //is this the correct annotaion??
   private Set <Keyword> keywords = new HashSet();

//whole bag of other fields
............

}

@Indexed
public class Keyword implements Serializable
{
   @DocumentId
   private Integer id;

   @ IndexedEmbedded //is this the correct annotaion??
   @Field(name="keyword", index=Index.TOKENIZED, store=Store.YES)
   private String name;

   //do I need to index this????
   private Set <Book>books = new HashSet();

..........
}


And my query code looks like this:

Code:
FullTextSession fullTextSession = Search.createFullTextSession(session);
//why "id" here???
org.apache.lucene.queryParser.QueryParser parser = new QueryParser("id", new StopAnalyzer() );   
String queryString = "keyword:france"; //for example
org.apache.lucene.search.Query luceneQuery = parser.parse(queryString );
//do I need to add Keyword.class here????
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery( luceneQuery, Book.class/*, Keyword.class*/ );
result = fullTextQuery.list();


And I want to do a fulltext search returning only Book objects. This is working nicely if I just search the Book fields - but I also want it to match Keyword names.

Firstly I'm unclear about the roles of @ContainedIn and @IndexedEmbedded. Have I got it right?

At present, it builds two indexes - one for Books and one for Keywords. But I only want to get Book objects back. Is this correct? Or do I need to "embed" keyword index inside the book index?

Also, I wonder if I perhaps need to index the
private Set <Book> books = new HashSet();
in my Keywords class?

And what arguments should I pass to the method
fullTextSession.createFullTextQuery()??
I've noticed that if I pass the Keyword.class, I can get my Keyword objects back - but then I have to go through a load of laborious code to get back the corresponding Book object.

Does anyone know a good online tutorial that explains all this? I've read the docs and googled around but not found anything beyond the Hibernate Reference Guide and have spent a couple of days on this without getting much further - so any help anyone can give me would be much appreciated. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 12, 2007 5:48 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Currently Hibernate Search does not support IndexedEmbedded collections (not sure how it should look like).

IndexedEmbedded is only useful on association attributes (ie one annotated with @*ToOne) or embedded attribute (one annotated with @Embedded), so @indexedEmbedded does not make sense

What you want is all the books having a given keyword, this is not possible today but the query should look like

"keywords.name:france" filtered by Book.class only.

In the mean time you'll need to retrieve the appropriate keyword and then do a second HQL query looking for books by keyword id.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 1:37 am 
Newbie

Joined: Thu Apr 12, 2007 2:34 am
Posts: 5
Thanks Emmanuel - that clears my doubts and points me in the right direction.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 2:41 pm 
Newbie

Joined: Wed Mar 28, 2007 2:51 pm
Posts: 16
Hi guys, I was just trying to do this as well and it doesn't seem to work for me. I was following the example here: http://www.hibernate.org/hib_docs/searc ... pping.html

I have a class with a collection of other classes (onetomany).

The example is hard to understand because Address embeds a collection of Places which embeds Address.

So I don't understand this. Can I not index associated collections of embedded classes or not? And is there a better example of this?

For example, how would it be annotated for this.

Code:
class Customer {

   private String name;
   private Collection accounts;

   public Collection getAccounts() {
       return accounts;
   }

   public String getName() { return name; }

}

class Account {
    private String name;

    public String getName() { return name; }

}


What i want is to do "name:aname" and have it search Customer and all its accounts "name" field. Or I will settle for something like "name:aname AND account.name:aname".

Thanks for any help! This is an awesome feature if it works.

Darren


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 3:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you want to retrieve customers with a particular name having accounts with a particular name, is that correct (correlated query)?

as I said earlier, it is not yet possible to index correlated collections

But you can do it the other way around

Look for Account.class
name:aname AND customer.name:othername

You will retrieve accounts, then you can access each customer.

I need to think about adding the colection indexing feature. It's a bit more complex than it sounds

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 3:56 pm 
Newbie

Joined: Thu Apr 12, 2007 2:34 am
Posts: 5
dgovoni wrote:
The example is hard to understand because Address embeds a collection of Places which embeds Address.

So I don't understand this. Can I not index associated collections of embedded classes or not? And is there a better example of this?


Yes, I agree with Darren entirely - I don't completely understand the example either and spent a long time puzzling over it. A clearer example might perhaps spare a lot of the confusion.

But with Emmanuel's pointer, I got my search queries working nicely today.

Thanks again guys,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 5:06 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Heard
http://opensource.atlassian.com/projects/hibernate/browse/HSEARCH-38

If you have any idea on how to make it simpler, add them to the case

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 8:46 am 
Newbie

Joined: Wed Mar 28, 2007 2:51 pm
Posts: 16
You are awesome. Thanks for opening the ticket.

I understood about how to query using the subfields, but based on the example I could not figure out how to "annotate" it properly because my attempts failed to return results using the proper query fields.

Thanks!!

PS. I agree that correlated collection searching is a tricky problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 3:08 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I have added support for @indexedEmbedded on collections
http://opensource.atlassian.com/projects/hibernate/browse/HSEARCH-40
It was easier (and more useful) that I thought

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 11:11 am 
Regular
Regular

Joined: Wed Apr 25, 2007 10:29 am
Posts: 110
Location: France
Where could we find the Beta 2 to have this feature ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 4:03 am 
Newbie

Joined: Fri Apr 20, 2007 10:31 am
Posts: 10
You can access it via SVN.

_________________
Meine Webseite (German):

Germanische Mythologie


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 4:29 am 
Regular
Regular

Joined: Wed Apr 25, 2007 10:29 am
Posts: 110
Location: France
Ok, thank you ;).

And there is the support of @indexedEmbedded ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 9:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yesw SVN (trunk) has support for @IndexedEmbedded in collections

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 08, 2007 10:33 am 
Regular
Regular

Joined: Wed Apr 25, 2007 10:29 am
Posts: 110
Location: France
Thank you, i will try ;)


Top
 Profile  
 
 Post subject: Workaround
PostPosted: Tue May 08, 2007 5:29 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I think I found a workaround for the collection indexing problem. I for example have a class which contains a collection of Location classes. For indexing purposes I am creating a transient dummy getter which I use for indexing and searching, eg:

Code:
    @Transient
    @Field(name = "location", index = Index.TOKENIZED)
    public String getLocationIndexString() {
        String locations = "";
        if (getLocations() != null) {
            for (Location loc : getLocations()) {
                locations += loc.getName() + " ";
            }
        }
        return locations;
    }


Making the field transient will stop Hibernate to persist a locationIndexString property, but the @Field annotation will still make sure that the locations get indexed. I just concatenate all locations for indexing. Specifying Index.TOKENIZED will then split it up into seperate tokens again.

I am not sure if this is such a good idea, but it seems to work.

Cheers,
Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.