-->
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.  [ 3 posts ] 
Author Message
 Post subject: Querying relations in parent + child direction simultanious
PostPosted: Thu Jan 20, 2011 2:56 pm 
Newbie

Joined: Thu Jan 20, 2011 2:07 pm
Posts: 2
Hi there,

I'm a little bit lost in building the proper query/ resp. index for the following use case:

Code:
@Entity
@Indexed
public class A {
   @ManyToOne
   @JoinColumn(name = "bb", nullable = false)
   @IndexedEmbedded
   private B bb;

   @Field(index = Index.TOKENIZED)
   private String content = "";

   @OneToMany(cascade = CascadeType.ALL)
   @JoinColumn(name = "aa", nullable = false)
   @IndexColumn(name = "ccPosition", base = 0)
   @BatchSize(size = 100)
   @OrderBy
   @ContainedIn
   private List<C> ccs = new ArrayList<c>();

}

@Entity
@Indexed
public class B {
   @Field(index = Index.UN_TOKENIZED)
   private int id;
   
   @OneToMany(mappedBy = "bb", cascade = {CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH })
   @IndexColumn(name = "aasPosition", base = 0)
   @OrderBy
   @ContainedIn
   private List<A> aas = new ArrayList<A>();
}

@Entity
@Indexed
public class C {
   @Field(index = Index.UN_TOKENIZED)
   private int id;
   
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "aa", insertable = false, updatable = false, nullable = false)
   @IndexedEmbedded
   private A aa;
   
}


where I want to search within the field "content" of A and get all matching As but only get the subset of Docs that meet ID's of B and have a C with a given ID. The part where I don't get any further is limiting the search to retrieving only As who have a C with the given ID param2.
So basically something like:
Code:
MultiFieldQueryParser parser = createMultiFieldQueryParser(caseSensitive);
String query = "content:" + searchString + " bb.id:" + param1 + " ccs.id:" + param2;
Query ftq = parser.parse(query.toString());
List<A> aas = entityManager.createFullTextQuery(ftq, A.class).getResultList();


Param1 and param2 are coming from user input dynamically.

Maybe this isn't the proper approach and can be done using another concept like filters and someone can point me out how to search in both directions of relations simultaniously. In the exact use case the accosiations go much deeper and dont stop after ony ManyToOne and one OneToMany relation so I have to travers a lot more like "bb.dd.ee.ff.gg.id" and "ccs.hhs.id".

Thanks in advance.

Alexander


Top
 Profile  
 
 Post subject: Re: Querying relations in parent + child direction simultanious
PostPosted: Fri Jan 21, 2011 6:36 am 
Hibernate Team
Hibernate Team

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

Per default for the query parser is to use (implicitly) OR between the search terms. In your case you have to use an explicit AND. If you want to stick to using a query parser have a look at the Lucene query parser syntax -http://lucene.apache.org/java/3_0_2/queryparsersyntax.html

You can also combine the query parser query with a simple TermQuery for the ids using BooleanQuery. Best to browse the Lucene javadocs to see what types of queries exists.

--Hardy


Top
 Profile  
 
 Post subject: Re: Querying relations in parent + child direction simultanious
PostPosted: Fri Jan 21, 2011 7:36 am 
Newbie

Joined: Thu Jan 20, 2011 2:07 pm
Posts: 2
Hi Hardy,

thanks, I just figured it out by myself. I had to move one level down to C and start the query on this class.

Found the explanation in "Hibernate Search in Action" Page 106:
Quote:
You won’t be able to express queries involving a correlation between two properties of
a given entity in a collection.


In my use case I had to query a properties property of C. like "ccs.d.id:1"

Thanks.
Alexander


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