2015-01-06 70 views
0

首先,新年快樂!休眠用@ClassBridge搜索多個字段

我想索引多種語言的實體標籤。

我有2個實體:

myEntity所

  • labelCode

翻譯

  • 代碼
  • 語言代碼
  • 標籤

MyEntity.labelCode必須與Translation.code匹配,然後每個MyEntity實例具有多種語言的多個標籤。

我寫上一個myEntity所ClassBridge添加多個字段來記錄:

class I18NTranslationClassBridge implements FieldBridge { 

Analyzer analyzer 

@Override 
void set(String name, Object value, Document document, LuceneOptions luceneOptions) { 
    if (value && value instanceof I18NDictionaryCategory) { 
     I18NDictionaryCategory entity = value as I18NDictionaryCategory 

     String labelCode = entity.getLabelCode() 
     def translations = TranslationData.findAllByCode(labelCode) 
     if (!analyzer) analyzer = Search.getFullTextSession(Holders.getApplicationContext().sessionFactory.currentSession).getSearchFactory().getAnalyzer('wildcardAnalyzer') 
     translations?.each { translation -> 
      document.add(getStringField("labelCode_${translation.languageCode}", translation.label, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO, 1f, analyzer)) 
      document.add(getStringField("labelCode__${translation.languageCode}_full", translation.label, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO, 1f, null)) 
     } 

    } 
} 

private static Field getStringField(String fieldName, String fieldValue, Field.Store store, Field.Index index, Field.TermVector termVector, float boost, Analyzer analyzer) { 
    Field field = new Field(fieldName, fieldValue, store, index, termVector); 
    field.setBoost(boost); 
    // manually apply token stream from analyzer, as hibernate search does not 
    // apply the specified analyzer properly 
    if (analyzer) { 
     try { 
      field.setTokenStream(analyzer.reusableTokenStream(fieldName, new StringReader(fieldValue))); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return field 
} 

}

我想索引每語言2個字段:1,沒有分析儀和無標記生成器(用於分選結果)和另一個帶標記器(用於全文搜索)。

我的問題是,沒有分析儀的所有字段都很好的索引,但有分析儀的字段沒有。只有一種語言被正確編入索引。

我嘗試用ClassBridge或FieldBridge做到這一點,但沒有成功。

有什麼建議嗎?

最好的問候,

萊奧

回答

1

你不應該使用類/場橋內的分析儀。分析儀在稍後階段應用。 Hibernate Search在所謂的ScopedAnalyzer中收集所有需要的分析器,當Lucene Document被添加到索引時,它將被使用。爲了支持您的使用案例,您可以使用動態分析器選擇功能。另見http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#d0e4119

基本的方法是通過@AnalyzerDiscriminator來定義語言特定的分析儀。這使得它們可以通過名稱全球可用。那麼你需要實施org.hibernate.search.analyzer.Decriminator。您基本上根據您的字段名稱返回正確的分析器名稱(假定字段名稱以某種形式包含語言代碼)。最後但並非最不重要的是,您需要使用@AnalyzerDiscriminator(impl = MyDiscriminator.class)註釋MyEntity

+0

謝謝!文檔現在已正確編入索引。 – persa