-->
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.  [ 4 posts ] 
Author Message
 Post subject: get Analyzer from ClassBridge
PostPosted: Thu Apr 02, 2009 8:24 am 
Newbie

Joined: Thu Apr 02, 2009 8:05 am
Posts: 4
Hi

I use Hibernate Search and have the following problem:

I use a ClassBridge so I can manipulate what gets indexed. In the ClassBridge class I create a new value but I'd like to use the defined Analyzers so that the new value is analyzed before it gets indexed.

As far as I can see the Analyzers can be looked up from SearchFactory or maybe from InitContext but none of them is available.

In DocumentBuilderIndexedEntity.java ~ line 400 where the classbridges are called, also nothing is passed Analyzers could be looked up from.

Is there any way to do so ?


another question:

a property is annotated with @FieldBridge and also with @Column
if the FieldBridge modifies the value of the property, will it be stored into the db ? what is the order of the two ?

Thanks

Gabor


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 12:03 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Regarding your first question, the @ClassBridge annotation also takes a analyzer parameter where you can define the analyzer to be used for the document created by the class bridge. In case your class bridge creates additional document fields you might want to use Lucene's PerFieldAnalyzerWrapper.

Regarding your second question, since the Lucene document is is build before committing to the db I would assume that the value of the property can be changed in the bridge. Provided the value is mutable. That said, just don't do it. Modifying the value in the field bridge seems just wrong.

--Hardy


Top
 Profile  
 
 Post subject: still don'tget it
PostPosted: Mon Apr 06, 2009 4:14 am 
Newbie

Joined: Thu Apr 02, 2009 8:05 am
Posts: 4
Hi Hardy and thank you for your answer!

Unfortunatelly I still don't get it :( so I write down my problem in details
I work on a multilingual website. Users can upload photos and I'd like to have a ~ language independent search. In order to have language independent search I use Google translate to translate everything into English.

@Indexed
@AnalyzerDef(name="analyzer",
tokenizer = @TokenizerDef(factory= StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = ISOLatin1AccentFilterFactory.class),
@TokenFilterDef(factory = StandardFilterFactory.class),
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
@Parameter(name="language", value="English")
})
}
)
@ClassBridge(impl = TranslatingBridge.class, store=Store.YES, analyzer=@Analyzer(definition="analyzer"))
@Entity
public class Photo {
...
@Column
Locale lang;
@Column
String title; // the title is entered by the user in some language
@Column
String titleEng; // the title translated into English
...
}


public class TranslatingBridge implements org.hibernate.search.bridge.FieldBridge {
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
Photo p = (Photo)value;
String titleEng = GTranslator.Translate2English((String)p.getTitle(), p.lang);
if (titleEng != null) {
Field field = new Field("titleEng", titleEng, luceneOptions.getStore(),
luceneOptions.getIndex(), luceneOptions.getTermVector() );
field.setBoost( luceneOptions.getBoost() );
document.add( field );
}
...
}

actually it works quite well, except 2 problems:
1. When I got back the english title from Google Translator I'd like to have it analyzed by the defined analyzer (named "analyzer").
From your answer I can not tell how to do that...
2. I'd like to store the English version into the db as well as in the Search index. So I could do the translation in a @PrePersist method (I use JPA with Spring) but I don't know if @PrePersist is called before indexing or after indexing....

Thanks for your help and have a nice day

Gabor


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 06, 2009 6:26 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

To use the mentioned PerFieldAnalyzerWrapper approach you would have to build your analyzers progammatically. That's maybe not so nice. Thinking about it there is a even better solution using @AnalyzerDiscriminator. Check the online documentation for this annotation. This annotation is only available since the lastest release of Search though.

Using this annotation you would your Photo entity would look like this:
Code:
@Indexed
@AnalyzerDef(name="analyzer",
tokenizer = @TokenizerDef(factory= StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = ISOLatin1AccentFilterFactory.class),
@TokenFilterDef(factory = StandardFilterFactory.class),
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
@Parameter(name="language", value="English")
})
}
)
@ClassBridge(impl = TranslatingBridge.class, store=Store.YES, analyzer=@Analyzer(definition="analyzer"))
@AnalyzerDiscriminator(impl = MyDiscriminator.class)
@Entity
public class Photo {
...
}


In the implementation of the descriminator could look like this:
Code:
public class LanguageDiscriminator implements Discriminator {

    public String getAnanyzerDefinitionName(Object value, Object entity, String field) {
        if ( "titleEng".equals(field)) {
            return "analyzer";
        } else {
            return null;
        }
    }
}


I think that would be the niceset solution. Give it a go and let us know if it works.

BTW, using a @PrePersist should work for the translation.

--Hardy


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