java如何使用lucene索引查询国家代码?
我正在为citynames和countrycodes创建一个lucene索引(取决于彼此)。我希望国家代码是小写搜索和精确匹配
首先,我现在尝试查询单个countrycode并查找与该代码匹配的所有索引元素。我的结果总是空的
//prepare
VERSION = Version.LUCENE_4_9;
IndexWriterConfig config = new IndexWriterConfig(VERSION, new SimpleAnalyzer());
//index
Document doc = new Document();
doc.add(new StringField("countryCode", countryCode, Field.Store.YES));
writer.addDocument(doc);
//lookup
Query query = new QueryParser(VERSION, "countryCode", new SimpleAnalyzer()).parse(countryCode);
结果:
当我查询诸如“IT”、“DE”、“EN”等coutrycode时,结果总是空的。为什么?
对于两个字母的国家代码,SimpleAnalyzer
是否来自
# 1 楼答案
我有点困惑。我假设您的索引编写器是在代码中未提供的某些部分中初始化的,但是您不是将
Version
传递到SimpleAnalyzer
吗?SimpleAnalyzer
没有参数构造函数,从3开始就没有。十、 无论如何这是我看到的唯一真正的问题。下面是一个使用您的代码的工作示例:
# 2 楼答案
对于StringField,可以使用TermQuery而不是QueryParser