我正在索引一些項目,其中包括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