首先,新年快樂!休眠用@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做到這一點,但沒有成功。
有什麼建議嗎?
最好的問候,
萊奧
謝謝!文檔現在已正確編入索引。 – persa