-->
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.  [ 10 posts ] 
Author Message
 Post subject: Indexing Arrays
PostPosted: Tue Feb 15, 2011 12:12 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2011 6:49 am
Posts: 22
Hi,

can anyone guide me to indexing an array, of say String values?

Thanks


Top
 Profile  
 
 Post subject: Re: Indexing Arrays
PostPosted: Tue Feb 15, 2011 1:05 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you need to implement a FieldBridge: http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#example-custom-string-bridge

if you don't care about the order, just add each element to the same field multiple times.

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


Top
 Profile  
 
 Post subject: Re: Indexing Arrays
PostPosted: Tue Feb 15, 2011 5:46 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2011 6:49 am
Posts: 22
Hi Sanne,

I did write a bridge:
Code:
         public String objectToString(Object object) {

      String[] exclusionList = (String[]) object;
      StringBuffer buffer = new StringBuffer();
      for(int i=0; i< exclusionList.length; i++) {
         buffer.append(exclusionList[i]).append(" ");
            
           }
      return buffer.toString();
   }


so if then the result of buffer.toString() is something like: "england germany france greece" and i want to apply a filter such that any matches of say "england" should be excluded it would fail because the indexed string is "england germany france greece" and not as tokenized values, if that makes sense?


Top
 Profile  
 
 Post subject: Re: Indexing Arrays
PostPosted: Tue Feb 15, 2011 5:57 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
right, if you append the strings like that you would need an analyzer to tokenize them afterwards.
But you have already separate values, so it's actually much simpler:

Code:
   @Override
   public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
      String[] exclusionList = (String[]) value;
      for ( String string : exclusionList ) {
         luceneOptions.addFieldToDocument( name, string, document );
      }
   }


(I'm just implementing org.hibernate.search.bridge.FieldBridge)

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


Top
 Profile  
 
 Post subject: Re: Indexing Arrays
PostPosted: Tue Feb 15, 2011 6:18 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2011 6:49 am
Posts: 22
Hi Sanne,

Thankyou for your response, however where and how does this method get called?

my code:

Code:
import org.apache.lucene.document.Document;
import org.hibernate.search.bridge.StringBridge;

public class FilterArrayBridge implements StringBridge {

   public String objectToString(Object object) {

      String[] exclusionList = (String[]) object;
      StringBuffer buffer = new StringBuffer();
      if (exclusionList.length > 0) {
         for(int i=0; i< exclusionList.length; i++) {
            buffer.append(exclusionList[i]).append(" ");
            
         }
      }
      return buffer.toString();
   }

   public void set(String name, Object value, Document document,
         LuceneOptions luceneOptions) {
      String[] exclusionList = (String[]) value;
      for (String string : exclusionList) {
         luceneOptions.addFieldToDocument(name, string, document);
      }
   }
   

}


Top
 Profile  
 
 Post subject: Re: Indexing Arrays
PostPosted: Tue Feb 15, 2011 8:41 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you don't need to implement StringBridge, just FieldBridge which contains just one method.
After that, your read the manual about FieldBridge, and look for an example: you have to annotate your entity with the @FieldBridge annotation pointing to your custom implementation.

Copying from the reference documentation I pointed you in previous post:
Code:
@Field @FieldBridge(impl = PaddedIntegerBridge.class)
private Integer length;

would be in your case something like

Code:
@FieldBridge(impl = FilterArrayBridge.class)
private String[] yourfield;

or
Code:
@Field(bridge = @FieldBridge(impl = FilterArrayBridge.class))
private String[] yourfield;

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


Top
 Profile  
 
 Post subject: Re: Indexing Arrays
PostPosted: Wed Feb 16, 2011 5:17 am 
Beginner
Beginner

Joined: Wed Feb 09, 2011 6:49 am
Posts: 22
hi Sanne,

That's great, yes misread your second post. I'm using version 3.1 so my implementation is:

Code:
public void set(String string,
      Object value,
      Document document,
      Store store,
      Index index,
           Float boost) {
      String[] exclusionList = (String[]) value;
         for (String s : exclusionList) {
            Field field = new Field(string, s, store, index);
            
            document.add(field);
         }
      
   }


and it works perfect.

thankyou for your guidance :)


Top
 Profile  
 
 Post subject: Re: Indexing Arrays
PostPosted: Fri Sep 16, 2011 5:43 am 
Newbie

Joined: Fri Sep 16, 2011 5:36 am
Posts: 2
Hi!

I'm trying to implement what was discussed in this thread.

I have a FieldBridge that looks like this:

Code:
public class MyStringBridge implements FieldBridge {
  @Override
  public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
    if (value != null && value instanceof Iterable) {
      Iterable<String> names = (Iterable<String>) value;
      for (String val : names) {
        luceneOptions.addFieldToDocument(name, val, document);
      }
    }
  }
}


I defined a property in the bean:
Code:
  @Field
  @FieldBridge(impl = MyStringBridge.class)
  private Set<String> alternativeNames;


The problem is that everytime I try to search, I get following exception:

Code:
org.hibernate.search.SearchException: FieldBridge class football.util.search.MyStringBridge does not have a objectToString method: field alternativeNames in football.beans.federal.Country
   at org.hibernate.search.engine.DocumentBuilderIndexedEntity.objectToString(DocumentBuilderIndexedEntity.java:682) ~[hibernate-search-3.4.1.Final.jar:3.4.1.Final]
   at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.buildSearchTerm(ConnectedMultiFieldsTermQueryBuilder.java:138) ~[hibernate-search-3.4.1.Final.jar:3.4.1.Final]
   at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:92) ~[hibernate-search-3.4.1.Final.jar:3.4.1.Final]
   at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:78) ~[hibernate-search-3.4.1.Final.jar:3.4.1.Final]
   at football.service.SearchManager.findCountry(SearchManager.java:68) ~[classes/:na]


I really don't know what I did wrong here.

Thanks in advance for help!


Top
 Profile  
 
 Post subject: Re: Indexing Arrays
PostPosted: Fri Sep 16, 2011 6:35 am 
Beginner
Beginner

Joined: Wed Feb 09, 2011 6:49 am
Posts: 22
Hi,

I think it's because you have @Field also defined alongside your @FieldBridge.
Try and remove the @Field

Thanks


Top
 Profile  
 
 Post subject: Re: Indexing Arrays
PostPosted: Fri Sep 16, 2011 8:51 am 
Newbie

Joined: Fri Sep 16, 2011 5:36 am
Posts: 2
I tried that before, here's the exception I got:

Code:
org.hibernate.search.SearchException: Unable to find field alternativeNames in football.beans.federal.Country
   at org.hibernate.search.engine.DocumentBuilderIndexedEntity.objectToString(DocumentBuilderIndexedEntity.java:688) ~[hibernate-search-3.4.1.Final.jar:3.4.1.Final]
   at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.buildSearchTerm(ConnectedMultiFieldsTermQueryBuilder.java:138) ~[hibernate-search-3.4.1.Final.jar:3.4.1.Final]
   at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:92) ~[hibernate-search-3.4.1.Final.jar:3.4.1.Final]
   at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:78) ~[hibernate-search-3.4.1.Final.jar:3.4.1.Final]


I ended up implementing TwoWayFieldBridge with the following two stub-like methods:

Code:
public Object get(String name, Document document) {
    return name;
  }
  public String objectToString(Object object) {
    return object.toString();
  }


This way it at least starts but I'm not totally convinced that the result is correct.


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