首先,新年快乐!休眠用@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