我正在索引一些项目,其中包括title
和cost
作为字段。成本是双重价值。 我准备的查询,如:带范围的Lucene查询
(title:item~0.8) AND (cost:[0.0 TO 200.0])
解析后,query.toString()
看起来是这样的:
+title:item~0 +cost:[0.0 TO 200.0]
从返回的结果,很明显,cost
不考虑。 我知道cost
被索引,因为我可以检索它。 索引代码:
public void index(Set<Item> items) throws IOException {
String path = "D:\\lucenedata\\myproj";
Directory fsDir = FSDirectory.open(new File(path));
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig iwConf = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
iwConf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter indexWriter = new IndexWriter(fsDir, iwConf);
for (Item item : items) {
Document d = new Document();
if (item.getCost() != null) {
d.add(new DoubleField("cost", item.getCost().doubleValue(), Store.YES));
}
d.add(new TextField("title", item.getTitle(), Store.YES));
indexWriter.addDocument(d);
}
indexWriter.commit();
indexWriter.close();
System.out.println("Indexed " + items.size() + " items");
}
感谢您的解释。我还发现,这种方式并不适用。相反,我写了一个自定义的QueryParser,如果遇到“cost”列,它将创建新的NumericRange。我也会发布我的解决方案。 – isah 2015-01-05 13:03:22