-->
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: hibernate search on one to many relations
PostPosted: Fri Aug 21, 2015 9:35 am 
Newbie

Joined: Fri Aug 21, 2015 9:25 am
Posts: 2
I'm having a problem with one to many relations in hibernate search. Because of the way the fields are stored in lucene (all the same fieldname) it seems impossible to search for a certain entity having a childentity with two properties.

For example lets take an indexed class A with field FieldA and class B with fields FieldB1 and FieldB2. Every class A could have one or more class Bs related to it, annotated with @IndexedEmbedded. Assume following data:

a.FieldA = "test"
a.b1.FieldB1 = "testb1"
a.b1.FieldB2 = true
a.b2.FieldB1 = "testb2"
a.b2.FieldB2 = false

which will be indexed in lucene with the fields: FieldA (test), B.FieldB1 (testb1), B.FieldB2 (true), B.FieldB1 (testb2), B.FieldB2 (false)

Now I want to look for all A's that have a B where FieldB1 is "testb2" and FieldB2 is true. This should give me no results. However hibernate search will always give me results because the query

(+FieldB1:testb2 +FieldB2:true) will correspond to the document. However that is not the result I want. Can anybody give me a clue how to solve it, or should I index in a completely different manner.


Top
 Profile  
 
 Post subject: Re: hibernate search on one to many relations
PostPosted: Fri Aug 21, 2015 9:56 am 
Newbie

Joined: Fri Aug 21, 2015 9:25 am
Posts: 2
I guess this is the same question as

https://forum.hibernate.org/viewtopic.php?f=9&t=1032384
https://forum.hibernate.org/viewtopic.php?f=9&t=1030375

But they never got an answer, so ... anyone? :p


Top
 Profile  
 
 Post subject: Re: hibernate search on one to many relations
PostPosted: Wed Aug 26, 2015 8:19 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I'm sorry I missed those other questions.

You're right, this is how it works and the use case you describe requires some care. There are many different ways to do it, but mainly they can be classified in the following general approaches:

# Encode them together
With a custom FieldBridge or ClassBridge you make sure you encode the two properties which need not be separated as a single keyword. For example you could store it index it as "testb1|true". That's not convenient at all for free-form full text but it works well for flags, codes and other metadata which is not related with full-text.

# Reverse the relationship
Don't target entity A, but search for all types B. Of course this only works in simple cases, were you don't have multiple _toMany relations. The case of your example it would work fine, but it's not a silver bullet.

# Combine the full-text query with a Criteria
In this section of the documentation we mention that you can customize the Criteria used to load results:
- http://docs.jboss.org/hibernate/search/5.5/reference/en-US/html_single/#_building_a_hibernate_search_query
We also explain that you really shouldn't use that to apply further restrictions via SQL, as this hasn't been tested much and it will throw-off some statistics like the expected amount of results. But as a matter of fact, it's working for some people.. Just make sure you understand you're cheating a bit and test it carefully.

# Post-processing
If you're not loading many thousands of objects this way, you might want to simply filter out some results in your own code.

# Don't ;-)
When it comes down to complex relational queries, it's probably better to use the relational database for this.

I should add that we're exploring possible improvements on the area, but it will always be a complex matter. For example we might be able to do a single join, but not more than one.

_________________
Sanne
http://in.relation.to/


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.