-->
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.  [ 12 posts ] 
Author Message
 Post subject: Projection and FieldBridge
PostPosted: Fri Aug 22, 2008 10:46 am 
Newbie

Joined: Tue Nov 14, 2006 3:20 am
Posts: 3
Hi,

i'm using Spring, Hibernate and Hibernate Search (3.0.1). I mapped an object to be indexed as explained in the doc. My object (Node) has a key/value map attribute where the value is another object (NodeProperty). It's a kind of exensibility. Here the definition:
Code:
@Field(index = Index.TOKENIZED, store = Store.YES)
@FieldBridge(impl = PropertyBridge.class)
private Map<String, NodeProperty> properties = new TreeMap<String, NodeProperty>();

I used a FiledBridge to index the properties contained in the map. Here the code of the PropertyBridge
Code:
public class PropertyBridge implements FieldBridge {

   public void set(String name, Object value, Document document, Store store, Index index, Float boost) {
      if (value != null) {
         Map<String, NodeProperty> properties = (Map<String, NodeProperty>) value;
         for (Iterator iter = properties.keySet().iterator(); iter.hasNext();) {
            String key = (String) iter.next();
            Field field = new Field(key, properties.get(key).getValue().toString(), store, index);
            if (boost != null)
               field.setBoost(boost);
            document.add(field);
         }
      }
   }
}

Running the indexing everything works fine. I can query my node using Lucene.
My problems comes when I tried to use the query setting projection. If i use a field mapped on a Node's attribute works fine. Using a field mapped on a key of the map (NodeProperty) return always null. I read that i need to implements a TwoWayFieldBridge so I changed the interface and added the two methods set() and objectToString() but during the search these methods are never called.
Code:
public class PropertyBridge implements TwoWayFieldBridge {

   @SuppressWarnings("unchecked")
   public void set(String name, Object value, Document document, Store store, Index index, Float boost) {
      if (value != null) {
         Map<String, NodeProperty> properties = (Map<String, NodeProperty>) value;
         for (Iterator iter = properties.keySet().iterator(); iter.hasNext();) {
            String key = (String) iter.next();
            Field field = new Field(key, properties.get(key).getValue().toString(), store, index);
            if (boost != null)
               field.setBoost(boost);
            document.add(field);
         }
      }
   }

   public Object get(String name, Document document) {
      
      System.out.println(name + " " + document);
      
      return name;
   }

   public String objectToString(Object object) {
      System.out.println(object.toString());
      return object.toString();
   }

   
}

Looking the code of the bridge the properties are stored in the index.

I miss something?
Thanks
Andrew


Top
 Profile  
 
 Post subject: Projection and (TwoWay)FieldBridge
PostPosted: Thu Aug 28, 2008 12:20 pm 
Newbie

Joined: Thu Aug 28, 2008 12:13 pm
Posts: 9
Location: Heilbronn, Germany
Hi @all,

I am experiencing exactly the same problem. As soon as i try to get a Field (annotated with a custom TwoWayFieldBridge) via a Projection, those values are null.

Could someone of the experts plz help along with this problem. I don't have to post any code, as it looks very similar to the one already posted here. I just want to do exactly the same: TwoWayFieldBridge -> Projection on that Field.

Official Tutorial is not very precise about such things... Couldn't find anything explaining this in detail. Maybe it is really a bug in Hibernate-Search (using 3.1-beta release already).

Regards and thanks in advance,
Martin


Last edited by mwiesner on Thu Aug 28, 2008 1:36 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Projection and (TwoWay)FieldBridge
PostPosted: Thu Aug 28, 2008 12:20 pm 
Newbie

Joined: Thu Aug 28, 2008 12:13 pm
Posts: 9
Location: Heilbronn, Germany
Hi @all,

I am experiencing exactly the same problem. As soon as i try to get a Field (annotated with a custom TwoWayFieldBridge) via a Projection, those values are null.

Could someone of the experts plz help along with this problem. I don't have to post any code, as it looks very similar to the one already posted here. I just want to do exactly the same: TwoWayFieldBridge -> Projection on that Field.

Official Tutorial is not very precise about such things... Couldn't find anything explaining this in detail. Maybe it is really a bug in Hibernate-Search (using 3.1-beta release already).

Regards and thanks in advance,
Martin


Last edited by mwiesner on Thu Aug 28, 2008 1:37 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Projection and (TwoWay)FieldBridge
PostPosted: Thu Aug 28, 2008 12:22 pm 
Newbie

Joined: Thu Aug 28, 2008 12:13 pm
Posts: 9
Location: Heilbronn, Germany
Sry for double posts... Forum lagged, i just clicked again ... -> sry :-)

One thing to add:
I did experience the same behaviour that get/objectToString methods are never called. IS THAT AS ITS MEANT TO BE? I used a Debugger not just some sysouts...


Top
 Profile  
 
 Post subject: Projection and (TwoWay)FieldBridge
PostPosted: Tue Sep 02, 2008 12:33 pm 
Newbie

Joined: Thu Aug 28, 2008 12:13 pm
Posts: 9
Location: Heilbronn, Germany
Nobody having a clue about that? Not even someone who has a working example, piece of helping information??

Regards,
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 3:49 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hello,

I'm not an expert about this topic, but I think the problem is that you are defining your own name for the Field (the key of the property), so your values get stored in a different field than the one Search is going to load for your projection.

Using a bridge you are free to manipulate the document, but it is of course "advanced topic" so if you want to use Search to retrieve them you should use it's naming scheme;
I would suggest to use @IndexedEmbedded unless you are needing to search on a specific key of your properties. In this case you should use your own IndexReader if you really want only the String back; returning the entity could be more appropriate.

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


Top
 Profile  
 
 Post subject: IndexEmbedded vs. Projection Stuff
PostPosted: Wed Sep 03, 2008 5:52 am 
Newbie

Joined: Thu Aug 28, 2008 12:13 pm
Posts: 9
Location: Heilbronn, Germany
Thanks for some care about this :-)

The thing is, my code is even using IndexEmbedded for that field. Is having both annotations at the same time not as it is meant to be? My problem is, if i only use IndexEmbedded i get NULL values when projectinng like this for example:

Index/Search-Query is for Description objects (via projection) having a reference to an Article. An Article itself has a reference to an Address.
Description.class --> belongs to one Article --> has one -> Address
Description.Article (is @IndexEmbedded) -> Article.Address (is @IndexEmbedded)

If I project for the following fields it works:
"Article.number", "Article.XYZ"
But if i do:
"Address.street", ...
Only null values... If I use "Article.Adress.street" the same behaviour.

So my question is:
How to annotate correctly to obtain the values when projecting a Description.class query to fields that are 2 or more references "away"?? One thing to mention: Things are written into the index files on disk, I checked on that with a simple editor...

Any suggestions would be great, as such a FieldBridge workaround seams not to help.

Regards,
Martin


Top
 Profile  
 
 Post subject: Re: Projection and FieldBridge
PostPosted: Tue Feb 02, 2010 8:46 am 
Newbie

Joined: Tue Feb 02, 2010 8:36 am
Posts: 2
I have a similar question.
If we don't set projections directly to fullTextQuery, and instead create a criteria and set projections to that(since criteria supports projections to Associations along with aliasing), and finally set that criteria to the fullTextQuery, does it not work?

I tried it, but no success.

_________________
Shareef


Top
 Profile  
 
 Post subject: Re: Projection and FieldBridge
PostPosted: Thu Feb 25, 2010 9:11 pm 
Newbie

Joined: Wed Jul 20, 2005 12:57 am
Posts: 15
Location: Sydney, Australia
shareef wrote:
I have a similar question.
If we don't set projections directly to fullTextQuery, and instead create a criteria and set projections to that(since criteria supports projections to Associations along with aliasing), and finally set that criteria to the fullTextQuery, does it not work?

I tried it, but no success.


According to the Documentation of FullTextQuery.setCriteriaQuery this is not supported, but you can hack the projection as follows:
Code:

org.hibernate.Query query = fullTextSession.createFullTextQuery( fullTextQuery )
    .setCriteriaQuery( hibernateCriteriaWithYourProjection );

// Executing the ft query will set the where clause of the hibernate criteria above
List ftResultList = query.list();

// So now you can re-execute the query from the hibernate criteria with the projection applied to it.
// Unfortunately the underlying query is executed twice.
@SuppressWarnings( "unchecked" )
final List< YourProjectionType > projectionList = hibernateCriteriaWithYourProjection.list();



I'd be keen to know of a more direct way of doing this so I don't end up with running the SQL twice. Or if it really isn't supported to add the feature in a future release. Another way of approaching it is to have access to the list of document Ids that map to your key list before any SQL is executed and to build the disjunction list manually (I'm still new to Hibernate Search so this might be doable).

_________________
Christian Maslen


Top
 Profile  
 
 Post subject: Re: Projection and FieldBridge
PostPosted: Fri Feb 26, 2010 2:02 am 
Newbie

Joined: Tue Feb 02, 2010 8:36 am
Posts: 2
Thank you cmaslen.

Currently, while indexing I ensured field names contains only alphabet and "_" character so that a valid setter-getter can be provided while projecting it and transforming it, and if I don't have any associations in my index, I am pulling out the required fields from the projections, and if I have associations, especially if the associations contain composite keys, I am projecting out the ids from the index and running criteria with those ids and any other restrictions.

One more problem I faced with indexed entities which have composite keys(FieldBridges) is the sql generated after searching the indexes is improper. Check the sql for search resulting in more than 2 records for the compositeKeyId.

_________________
Shareef


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Aug 24, 2011 4:01 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
s.grinovero wrote:
Hello,

I'm not an expert about this topic, but I think the problem is that you are defining your own name for the Field (the key of the property), so your values get stored in a different field than the one Search is going to load for your projection.

Using a bridge you are free to manipulate the document, but it is of course "advanced topic" so if you want to use Search to retrieve them you should use it's naming scheme;


I don't agree with the notion that you should be forced to HSearch's naming scheme. I'm also running into this limitation and AFAIC, when I do a projection, I'm already walking away from the DataStore-Hibernate-Entity hydration path. In the same way that a regular Hibernate Core projection will return you some database fields a Hibernate Search projection should behave in a similar way and just hand you the fields: if you use projection, all bets are off: here are the values. What's worse for me, is that I don't see an obvious way around this. Is there? (apart from the obvious hard cording getters (which is not cool if there are variable amounts of fields involved))?


Top
 Profile  
 
 Post subject: Re: Projection and FieldBridge
PostPosted: Thu Aug 25, 2011 6:14 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi Marc,
Quote:
I don't agree with the notion that you should be forced to HSearch's naming scheme. I'm also running into this limitation and AFAIC, when I do a projection, I'm already walking away from the DataStore-Hibernate-Entity hydration path. In the same way that a regular Hibernate Core projection will return you some database fields a Hibernate Search projection should behave in a similar way and just hand you the fields: if you use projection, all bets are off: here are the values. What's worse for me, is that I don't see an obvious way around this. Is there? (apart from the obvious hard cording getters (which is not cool if there are variable amounts of fields involved))?


Fair enough, I agree with the general concept. pardon my comments from so long ago, I was then new to this project (and not part of the Hibernate team as my updated logo now suggests) and have certainly misjudged the importance of being able to do this.
Nowadays my updated idea is that if I have a TwoWayFieldBridge applied on the field, I'd like the transformed value returned if possible, as that would likely be some primitive or similarly encoded value so that would be useful for my code (and as you say consistent with Hibernate Core which would return me a java.util.Date, not a String, when loading a Date type).
But if there is no fieldbridge, especially if the field is unknown, the raw String should be returned so that I could apply whatever I want to it. Null is certainly not a useful value. In fact I didn't expect Search to return a null, if that's the case please provide a test and I'll fix that - assuming the rest of the team will consider this a reasonable request too. If you want to try fixing it yourself, that would be great too ;)

_________________
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.  [ 12 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.