Lucene 3.0.3数字术语查询 [英] Lucene 3.0.3 Numeric term query
问题描述
我在Lucene 3.0.3中有一个数字字段,它可以很好地处理范围查询。如果我们切换到TermQuery,它不会产生任何结果。例如:
I have a numeric field in Lucene 3.0.3 and it works perfectly fine with the range queries. If we switch to the TermQuery it doesnt produce any result. For example:
Document doc = new Document();
String name = "geolongitude";
NumericField numericField = new NumericField(name);
double value = 29.0753505;
String valueAsString = "29.0753505";
numericField.setDoubleValue(value);
doc.add(numericField);
indexWriter.addDocument(doc);
indexWriter.commit();
indexWriter.close();
IndexSearcher indexSearcher = new IndexSearcher(open);
Query termQ = new TermQuery(new Term(name, valueAsString));
TopDocs search = indexSearcher.search(termQ, 10);
在这种情况下,我得不到任何结果。我试图弄清楚是否存在任何NumericTermQuery但却无法找到。我可以做一些棘手的事情(对我正在搜索的术语进行范围查询)但我不喜欢这个解决方案。
In this case I dont get any result. I tried to figure out whether exist any "NumericTermQuery" but couldnt find that. I could do something tricky (make a range query for the term that I am searching) but I dont like the solution.
谢谢!
推荐答案
好的,我找到了不同的解决方案,
Ok, I have figure out a different solution,
String doubleToPrefixCoded = NumericUtils.doubleToPrefixCoded(value);
Query termQ = new TermQuery(new Term(name, doubleToPrefixCoded));
我发现它来自: http://www.gossamer-threads.com/lists/lucene/java-user/88516 并且它可以正常工作
I found it out from : http://www.gossamer-threads.com/lists/lucene/java-user/88516 and it works correctly
这篇关于Lucene 3.0.3数字术语查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!