-->
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.  [ 5 posts ] 
Author Message
 Post subject: Indexing a collection using programmatic mapping
PostPosted: Wed Nov 10, 2010 7:08 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2008 7:05 pm
Posts: 27
As an example, I have a User that has a list of Items that each have a name. I need to return all the users that contain the Item with a given name. I've seen ways to do this with annotations, but no examples using programmatic mapping. Is this possible?

Thanks,

Jeff


Top
 Profile  
 
 Post subject: Re: Indexing a collection using programmatic mapping
PostPosted: Thu Nov 11, 2010 6:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Of course it is possible. The programmatic mapping is as expressive as annotations. Seems you have a simple indexed embedded case:
Code:
...
.entity(MyClass.class).indexed()
   .property("items", ElementType.FIELD).indexEmbedded()
...


Top
 Profile  
 
 Post subject: Re: Indexing a collection using programmatic mapping
PostPosted: Thu Nov 11, 2010 5:57 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2008 7:05 pm
Posts: 27
Thanks...I realized it was in the documentation after digging a little further...oops. Although I do have a question. Given the model of a User with Items that have a string field "name", I mapped it like this:

Code:
mapping.entity(User.class).indexed().indexName("user-index").property("items", ElementType.FIELD).indexEmbedded();

mapping.entity(Item.class).indexed().indexName("item-index")
.property("name", ElementType.FIELD).field().index(Index.TOKENIZED).store(Store.YES)
.property("user", ElementType.FIELD).containedIn();


Which works great as a query on User like this:

Code:
items.name:SomeName


gives me all Users that have an Item with the name "SomeName". However, when I inspect the index via Luke, there are redundant fields. Both the "name" field and "items.name" field appear in the index (with the same number of terms, of course). I don't like this, because eventually I'll be indexing the item description which has a lot more terms and I don't want to have two sets like that. I want to keep the index as lean as possible. So, am I being redundant in my mapping or is this just the way things work? I should also point out that "items.id" shows up as a field in the index and I'll never need to use that. Any advice would be appreciated.

Regards,

Jeff


Top
 Profile  
 
 Post subject: Re: Indexing a collection using programmatic mapping
PostPosted: Fri Nov 12, 2010 6:02 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

that's because you create a second (independent) index for Item - mapping.entity(Item.class).indexed().indexName("item-index").
Try removing the highlighted part and just configure the properties.

--Hardy


Top
 Profile  
 
 Post subject: Re: Indexing a collection using programmatic mapping
PostPosted: Mon Nov 15, 2010 11:58 am 
Beginner
Beginner

Joined: Tue Oct 07, 2008 7:05 pm
Posts: 27
Thanks Hardy, that worked like a charm.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.