gpt4 book ai didi

java - 如何在 hibernate 搜索中实现对 int 值的搜索?

转载 作者:行者123 更新时间:2023-12-01 16:35:22 25 4
gpt4 key购买 nike

我正在尝试搜索整数值。我已经用 @Field 注释了参数,如下所示:

@Field(name = "confirmedCount", index = UN_TOKENIZED, store = Store.YES)
public int getConfirmedCount() {
return stuff.size();
}

然后我使用 luke 执行范围搜索:

confirmedCount:[5 TO 100]

我返回的结果是空的。然后我尝试:

confirmedCount:[1 TO 2]

结果是:

name confirmedCount
b 1
a 1
d 19
c 2

所以我的问题是:为什么我会收到此回复以及如何解决?我使用hibernate搜索3.0.1.GA

最佳答案

好吧,我想我的问题的答案是 RTFM!文档明确指出:

Numbers are converted into their string representation. Note that numbers cannot be compared by Lucene (ie used in ranged queries) out of the box: they have to be padded

所以我们需要实现一个类桥:

public class PaddedIntegerBridge implements StringBridge {

private int PADDING = 5;

public String objectToString(Object object) {
String rawInteger = ( (Integer) object ).toString();
if (rawInteger.length() > PADDING)
throw new IllegalArgumentException( "Try to pad on a number too big" );
StringBuilder paddedInteger = new StringBuilder( );
for ( int padIndex = rawInteger.length() ; padIndex < PADDING ; padIndex++ ) {
paddedInteger.append('0');
}
return paddedInteger.append( rawInteger ).toString();
}
}

然后我们需要注释该字段,以便它被索引:

@Field(name = "confirmedCount", index = UN_TOKENIZED, store = Store.YES, bridge = @FieldBridge(impl = PaddedIntegerBridge.class))
public int getConfirmedCount() {
return stuff.size();
}

然后在我的搜索中,我只需要在创建查询时使用这个桥,瞧它有效 =)

与 Luke 的一些测试:

confirmedCount:[00005 TO 00100]

name confirmedCount
g 00006
d 00019

关于java - 如何在 hibernate 搜索中实现对 int 值的搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9788666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com