我的一些索引字段使用希腊语分析器,我想在其他字段中使用英语分析器。我的问题是:当搜索结果时(目前使用MultiFieldQueryParser),我如何在每个字段中使用不同的分析器,以便希腊语分析器用于希腊语索引字段,英语分析器用于英语索引字段?< / p>
答案 0 :(得分:1)
这是我找到的解决方案。请评论。
transaction.begin();
PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new StandardAnalyzer(Version.LUCENE_30));
wrapper.addAnalyzer("greekTitle", new GreekAnalyzer(Version.LUCENE_30));
wrapper.addAnalyzer("greekDescription", new GreekAnalyzer(Version.LUCENE_30));
String[] fields = {"greekTitle", "greekDescription", "englishTitle", "englishDescription"};
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, fields, wrapper);
queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
org.apache.lucene.search.Query query = queryParser.parse(QueryParser.escape(queryString));
javax.persistence.Query persistenceQuery =
fullTextEntityManager.createFullTextQuery(query, Item.class);
@SuppressWarnings("unchecked")
List<Item> result = persistenceQuery.getResultList();
transaction.commit();
return result;
答案 1 :(得分:0)
您可以像这样构建查询解析器:
Analyzer analyzer = fullTextSession.getSearchFactory().getAnalyzer(Item.class);
QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_31, fields, analyzer);
将使用正确的分析器,如Item类的注释中所定义:
@Field(name = "greekTitle" analyzer = @Analyzer(impl = GreekAnalyzer.class))
public void getGreekTitle(){
//...
}
@Field(name = "englishTitle" analyzer = @Analyzer(impl = StandardAnalyzer.class))
public void getEnglishTitle(){
//...
}