-->
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: Field comma separator
PostPosted: Mon Jan 10, 2011 11:43 am 
Newbie

Joined: Thu Dec 30, 2010 12:10 pm
Posts: 9
Location: France
Hello,

I have made my fieldBridge to store a Set<String> :

Code:
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.LuceneOptions;

public class SetStringFieldBridge implements FieldBridge {
   
   public static final char SEPARATOR = ',';
   
   @Override
   public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
      
      if ( value == null ) {
         return;
      }
      
      // we expect a Set<String> here. checking for Set for simplicity
      if ( ! (value instanceof Set )) {
         throw new IllegalArgumentException("support limited to Set<String>");
      }
      
      @SuppressWarnings("unchecked")
      Set<String> set = (Set<String>)value;
      String values = StringUtils.join(set, SEPARATOR);
      
      Field field = new Field(name, values, luceneOptions.getStore(), luceneOptions.getIndex(), luceneOptions.getTermVector());
      field.setBoost(luceneOptions.getBoost());
      document.add(field);
   }
   
   

}


Code:

@Field(index=Index.UN_TOKENIZED, store=Store.YES, analyzer=@Analyzer(impl=SimpleAnalyzer.class))
@FieldBridge(impl=SetStringFieldBridge.class)
@ElementCollection
@CollectionTable(name="v_logicalitem_downloadtype", joinColumns=@JoinColumn(name="logicalitem_id", insertable=false, updatable=false))
@Column(name="downloadtype")
private Set<String> downloadtypes;



Exemple of lucene-index :

In the index :
id | name | myset
1 | foo | value1,value2,value3
2 | bar | value1,value3
3 | bar | value3

Now I would like to use an analyser to make request into this field, because the SimpleAnalyzer is not build for this.
If my request is : 'myset:value3' only index with id=3 is return. And I would like also the id=2 and the id=1.


Top
 Profile  
 
 Post subject: Re: Field comma separator
PostPosted: Mon Jan 10, 2011 12:10 pm 
Newbie

Joined: Tue Jan 04, 2011 11:59 am
Posts: 8
Location: Italy
Why don't you split up the content to more fields?
I.e. calling document.add(field) for every element in the set.

Matthias


Top
 Profile  
 
 Post subject: Re: Field comma separator
PostPosted: Tue Jan 11, 2011 5:08 am 
Newbie

Joined: Thu Dec 30, 2010 12:10 pm
Posts: 9
Location: France
Yes, you've right, I didn't know I could do that.
This is what I need, thanks !


Top
 Profile  
 
 Post subject: Re: Field comma separator
PostPosted: Tue Jan 11, 2011 5:23 am 
Newbie

Joined: Thu Dec 30, 2010 12:10 pm
Posts: 9
Location: France
Here is my new SetStringFieldBridge :


Code:
import java.util.HashSet;
import java.util.Set;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.TwoWayFieldBridge;

public class SetStringFieldBridge implements TwoWayFieldBridge {
   
   @Override
   public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
      
      if ( value == null ) {
         return;
      }
      
      // we expect a Set<String> here. checking for Set for simplicity
      if ( ! (value instanceof Set )) {
         throw new IllegalArgumentException("support limited to Set<String>");
      }
      
      @SuppressWarnings("unchecked")
      Set<String> set = (Set<String>)value;
      
      for (String string : set) {
         Field field = new Field(name, string, luceneOptions.getStore(), luceneOptions.getIndex(), luceneOptions.getTermVector());
         field.setBoost(luceneOptions.getBoost());
         document.add(field);
      }
      
   }

   @Override
   public Object get(String name, Document document) {
      Field[] fields = document.getFields(name);
      Set<String> set = new HashSet<String>();
      for (Field field : fields) {
         set.add(field.stringValue());
      }
      return set;
   }

   @Override
   public String objectToString(Object value) {
      if ( value == null ) {
         return "";
      } else if ( value instanceof String ) {
         return (String) value;
      } else {
         return String.valueOf(value);
      }
   }
   
   

}


Top
 Profile  
 
 Post subject: Re: Field comma separator
PostPosted: Tue Jan 11, 2011 12:29 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
yes, Matthias is right the result is equivalent.
The only information you loose is relative positions of terms, but I guess you're not interested in that; you still have relative positions of the terms which make up each of your elements.
Relations from @IndexedEmbedded collections are encoded as your bridge is doing.

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