-->
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.  [ 6 posts ] 
Author Message
 Post subject: sorting with collection
PostPosted: Thu Feb 17, 2011 4:26 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2008 7:05 pm
Posts: 27
This may be more of a lucene question but I was hoping some H-Search experts might be able to help out.

My problem is best explained with an example. Let's say I have a Movie entity with title and description that I'm doing a text search on. But this entity has a collection of name/value pair attributes like this:

genre: mystery
minutesLength: 120
yearMade: 2001

And the code is something like this:

Code:
public class Movie {
  String title;
  String description
  List<Attr> attrs
...
}

public class Attr {
  String name;
  String value;
...
}


So, let's say I run a search on movies with the word "war" in the title, but I want to sort by the length of the movie. The problem is, the length value is stored in a collection and in a field that also has other values both text and numeric. So, if I try to sort on the lucene field attrs.value I'm not sure what will happen since, for the given document, attrs.value would have values of "mystery", "120" and "2001".

Is it even possible to sort on a collection like this? Is there some kind of advanced indexing I can do to "manufacture" a sort field based on the structure above?

I appreciate any guidance.

-JF


Top
 Profile  
 
 Post subject: Re: sorting with collection
PostPosted: Thu Feb 17, 2011 6:28 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
to be able to sort on a Lucene field, the value stored in this field must not be multivalued, so it must be one single keyword, or a String which will not result in more than one token when tokenized.

You could apply a custom fieldbridge on the collection of attributes which writes each value to a different field. The FieldBridge implementation is free to create as many Fields as it wants, so they don't have to match across all Lucene Documents (it's "schemaless" in NoSQL terms).

You'll still need to ensure that the values you sort on do actually exist for each result and will have one and only one String (I assume the length attribute would be fine with that).

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


Top
 Profile  
 
 Post subject: Re: sorting with collection
PostPosted: Thu Feb 17, 2011 8:22 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2008 7:05 pm
Posts: 27
FieldBridge...that sounds exactly like what I need.

To be clear, I would use the field bridge at the "attrs" list and have an implementation (java class) for each attribute I wanted to map to a sortable field. These implementations would look for the attribute in question and return the value. Does that seem right?


Top
 Profile  
 
 Post subject: Re: sorting with collection
PostPosted: Fri Feb 18, 2011 4:42 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Code:
   @Override
   public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
      List<Attr> attrs = (List<Attr>) value;
      for ( Attr attribute : attrs ) {
         luceneOptions.addFieldToDocument( attribute.name, attribute.value, document );
      }
   }

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


Top
 Profile  
 
 Post subject: Re: sorting with collection
PostPosted: Tue Feb 22, 2011 4:15 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2008 7:05 pm
Posts: 27
Sanne,

Thanks, that works great.

One question though, I noticed there's the "addNumericFieldToDocument" method. I tried using that on some numeric fields and when inspecting the index, I noticed it creates 16 terms for each field with strange values (as viewed in Luke). I thought this method might be the thing to use for numeric fields with sorting but I'm guessing it's not? Do I have to add numeric fields normally and implement my own padding for those I wish to sort on?

-JF


Top
 Profile  
 
 Post subject: Re: sorting with collection
PostPosted: Tue Feb 22, 2011 4:29 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
no you should be able to sort directly on the NumericField - treat it as one filter when speficying your Sort.
See constants in org.apache.lucene.search.SortField, and read the javadocs of org.apache.lucene.document.NumericField

You can still use the padding-numbers strategy and treat them as fields, but NumericFields should perform better in most cases.

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