在我的3.6代码中,我将数字字段添加到索引中,如下所示:
public void addNumericField(IndexField field, Integer value) {
addField(field, NumericUtils.intToPrefixCoded(value));
}
但是现在你需要传递一个BytesRef参数,并且完全不清楚你对下一个值的意图,所以我把它改为(正在进行中)
public void addNumericField(IndexField field, Integer value) {
FieldType ft = new FieldType();
ft.setStored(true);
ft.setIndexed(true);
ft.setNumericType(FieldType.NumericType.INT);
doc.add(new IntField(field.getName(), value, ft));
}
似乎更整洁
在3.6中我还添加了覆盖queryparser以使其适用于数值范围搜索,
package org.musicbrainz.search.servlet;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.util.NumericUtils;
import org.musicbrainz.search.LuceneVersion;
import org.musicbrainz.search.index.LabelIndexField;
import org.musicbrainz.search.servlet.mmd1.LabelType;
public class LabelQueryParser extends MultiFieldQueryParser {
public LabelQueryParser(java.lang.String[] strings, org.apache.lucene.analysis.Analyzer analyzer)
{
super(LuceneVersion.LUCENE_VERSION, strings, analyzer);
}
protected Query newTermQuery(Term term) {
if(
(term.field() == LabelIndexField.CODE.getName())
){
try {
int number = Integer.parseInt(term.text());
TermQuery tq = new TermQuery(new Term(term.field(), NumericUtils.intToPrefixCoded(number)));
return tq;
}
catch (NumberFormatException nfe) {
//If not provided numeric argument just leave as is,
//won't give matches
return super.newTermQuery(term);
}
} else {
return super.newTermQuery(term);
}
}
/**
*
* Convert Numeric Fields
*
* @param field
* @param part1
* @param part2
* @param inclusive
* @return
*/
@Override
public Query newRangeQuery(String field,
String part1,
String part2,
boolean inclusive) {
if (
(field.equals(LabelIndexField.CODE.getName()))
)
{
part1 = NumericUtils.intToPrefixCoded(Integer.parseInt(part1));
part2 = NumericUtils.intToPrefixCoded(Integer.parseInt(part2));
}
TermRangeQuery query = (TermRangeQuery)
super.newRangeQuery(field, part1, part2,inclusive);
return query;
}
}
所以我把所有这些都搞清楚了,我不再需要了它,但遗憾的是这个IntField现在没有任何疑问。
进一步阅读似乎Intfields仅用于范围查询,所以我不知道你是如何做匹配查询,以及NumericRangeQuery是否可以与我正在使用的经典Query Parser兼容。
然后我又回去尝试将我的数值添加为编码字符串
public void addNumericField(IndexField field, Integer value) {
FieldType fieldType = new FieldType();
fieldType.setStored(true);
fieldType.setIndexed(true);
BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
NumericUtils.intToPrefixCoded(value, 0, bytes);
doc.add(new Field(field.getName(),bytes, fieldType));
}
但是在运行时我现在收到错误!
java.lang.IllegalArgumentException: Fields with BytesRef values cannot be indexed
但是我需要索引字段,所以请问我如何像3.6中那样索引数字字段,以便我可以搜索它们。
答案 0 :(得分:2)
关于如何使用lucene 4.7进行预测:
索引时,我只是按照以下方式执行:
document.add(new IntField("int_field", int_value, Field.Store.YES));
搜索:
public class MyQueryParser extends QueryParser {
public MyQueryParser(Version matchVersion, String field, Analyzer anlayzer) {
super(matchVersion, field, anlayzer);
}
@Override
protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) throws ParseException {
if ("int_field".equals(field)) {
return NumericRangeQuery.newIntRange(field, Integer.parseInt(part1), Integer.parseInt(part2), startInclusive, endInclusive);
} else {
return super.getRangeQuery(field, part1, part2, startInclusive, endInclusive);
}
}
@Override
protected Query newTermQuery(Term term)
{
if ("int_field".equals(term.field())) {
try {
int number = Integer.parseInt(term.text());
BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
NumericUtils.intToPrefixCoded(number, 0, bytes);
TermQuery tq = new TermQuery(new Term(term.field(), bytes.utf8ToString()));
return tq;
} catch (NumberFormatException nfe) {
//If not provided numeric argument just leave as is, won't give matches
return super.newTermQuery(term);
}
} else {
return super.newTermQuery(term);
}
}
}
通过这样做,查询
int_field: 1
int_field: [1 TO 5]
按预期工作。
答案 1 :(得分:1)
只需使用适当的字段即可。例如IntField
,LongField
等
参见例如http://lucene.apache.org/core/4_1_0/core/org/apache/lucene/document/IntField.html
要查询这些字段,请参阅Lucene LongField exact search with Query
答案 2 :(得分:0)
所以我有这个工作,不管这是否是做我不知道的事情的最佳方式。
将数字添加到索引作为字符串
FieldType fieldType = new FieldType();
fieldType.setStored(true);
fieldType.setIndexed(true);
BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
NumericUtils.intToPrefixCoded(value, 0, bytes);
doc.add(new Field(field.getName(),bytes.utf8ToString(), fieldType));
QueryParser,需要使用equals()检查fieldname,以前可以使用==
protected Query newTermQuery(Term term)
{
if (term.field().equals(LabelIndexField.CODE.getName()))
{
try
{
int number = Integer.parseInt(term.text());
BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
NumericUtils.intToPrefixCoded(number, 0, bytes);
TermQuery tq = new TermQuery(new Term(term.field(), bytes.utf8ToString()));
return tq;
}
catch (NumberFormatException nfe)
{
//If not provided numeric argument just leave as is, won't give matches
return super.newTermQuery(term);
}
}
else
{
return super.newTermQuery(term);
}
}
此外,newRangeQuery()的签名已更改,还有额外的参数endInclusive
public Query newRangeQuery(String field,
String part1,
String part2,
boolean startInclusive,
boolean endInclusive)
{
if (
(field.equals(LabelIndexField.CODE.getName()))
)
{
BytesRef bytes1 = new BytesRef(NumericUtils.BUF_SIZE_INT);
BytesRef bytes2 = new BytesRef(NumericUtils.BUF_SIZE_INT);
NumericUtils.intToPrefixCoded(Integer.parseInt(part1), 0, bytes1);
NumericUtils.intToPrefixCoded(Integer.parseInt(part2), 0, bytes2);
part1 = bytes1.utf8ToString();
part2 = bytes2.utf8ToString();
}
TermRangeQuery query = (TermRangeQuery)
super.newRangeQuery(field, part1, part2, startInclusive, endInclusive);
return query;
}
您可以从索引中获取原始的intgeral值,如下所示
NumericUtils.prefixCodedToInt(new BytesRef(code))