【发布时间】:2015-01-06 09:49:28
【问题描述】:
首先,新年快乐!
我想索引多种语言的实体标签。
我有 2 个实体:
我的实体
- 标签代码
翻译
- 代码
- 语言代码
- 标签
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 个没有分析器和分词器(用于排序结果),另一个有分词器(用于全文搜索)。
我的问题是所有没有分析器的字段都被很好地索引,但没有分析器的字段。只有 1 种语言被正确编入索引。
我尝试用 ClassBridge 或 FieldBridge 来做,但没有成功。
有什么建议吗?
最好的问候,
狮子
【问题讨论】:
标签: hibernate solr lucene hibernate-search