Hibernate搜索失败,双精度类型大于5位
我在实体上有以下字段:Hibernate搜索失败,双精度类型大于5位,hibernate,lucene,double,hibernate-search,Hibernate,Lucene,Double,Hibernate Search,我在实体上有以下字段: @Field(index = Index.TOKENIZED, store = Store.YES) @Column(name = "total_credit_amount", nullable = false) @FieldBridge(impl = RoundedDoubleBridge.class) private Double totalCreditAmount; @Field(index = Index.TOKENIZED, store = Store.YES
@Field(index = Index.TOKENIZED, store = Store.YES)
@Column(name = "total_credit_amount", nullable = false)
@FieldBridge(impl = RoundedDoubleBridge.class)
private Double totalCreditAmount;
@Field(index = Index.TOKENIZED, store = Store.YES)
@Column(name = "total_debit_amount", nullable = false)
@FieldBridge(impl = RoundedDoubleBridge.class)
private Double totalDebitAmount;
双到字符串桥接器的实现如下所示:
public class RoundedDoubleBridge
implements StringBridge
{
@Override
public String objectToString(Object value)
{
// Do not index null strings
if (value == null)
{
return null;
}
if (value instanceof Double)
{
long price = round((Double) value);
return Long.toString(price);
}
else
{
throw new IllegalArgumentException(
RoundedDoubleBridge.class + " used on a non double type: "
+ value.getClass());
}
}
private long round(double price)
{
double rounded = Math.floor(price / 3) * 3;
if (rounded != price)
{
rounded += 3; //we round up
}
return (long) rounded;
}
}
private long round(double price)
{
double rounded = Math.floor(price / 3) * 3;
if (rounded != price)
{
rounded += 3; //we round up
}
return (long) rounded;
}
因此,这里的问题是,每当TotalDebitMount或totalCreditAmount上的值小于100000时,每当它们大于或等于100000时,我执行的搜索都会检索结果。。谢谢你的帮助
以下是我进行搜索的方式:
public List<AbstractRecord> doSearch(String stringToFind)
{
List<AbstractRecord> result = null;
// Test Search for specific values of an Abstract Record
// Aim to return the number of retreived results
em = emf.createEntityManager();
FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
//em.getTransaction().begin();
String[] fields = // Fields to be reviewed
new String[]
{
....,
"totalCreditAmount", "totalDebitAmount",
....
};
//Create a multi-field Lucene query
StandardAnalyzer stdAnalyzer = new StandardAnalyzer(Version.LUCENE_30);
MultiFieldQueryParser parser =
new MultiFieldQueryParser(Version.LUCENE_30, fields, stdAnalyzer);
org.apache.lucene.search.Query query = null;
try
{
query = parser.parse(stringToFind);
}
catch (ParseException ex)
{
Logger.getLogger(SearchFacade.class.getName()).log(Level.SEVERE, null, ex);
}
long time1 = System.currentTimeMillis();
// Wrap Lucene query in a javax.persistence.Query
javax.persistence.Query persistenceQuery =
fullTextEntityManager.createFullTextQuery(query);
// Execute search
result = (List<AbstractRecord>) persistenceQuery.getResultList();
em.close();
return result;
}
public List doSearch(字符串stringToFind)
{
列表结果=空;
//对抽象记录的特定值进行测试搜索
//目的返回检索结果的数量
em=emf.createEntityManager();
FullTextEntityManager FullTextEntityManager=
org.hibernate.search.jpa.search.getFullTextEntityManager(em);
//em.getTransaction().begin();
String[]字段=//要查看的字段
新字符串[]
{
....,
“totalCreditAmount”、“totalDebitAmount”,
....
};
//创建多字段Lucene查询
StandardAnalyzer stdAnalyzer=新的StandardAnalyzer(版本.LUCENE_30);
多字段QueryParser解析器=
新的多字段查询分析器(Version.LUCENE_30,fields,stdAnalyzer);
org.apache.lucene.search.Query Query=null;
尝试
{
query=parser.parse(stringToFind);
}
捕获(解析异常)
{
Logger.getLogger(SearchFacade.class.getName()).log(Level.SEVERE,null,ex);
}
长时间1=System.currentTimeMillis();
//在javax.persistence.query中包装Lucene查询
javax.persistence.Query persistenceQuery=
fullTextEntityManager.createFullTextQuery(查询);
//执行搜索
结果=(列表)persistenceQuery.getResultList();
em.close();
返回结果;
}
我觉得自己有点傻。
我发现了我的问题。。
它具体是在我的FieldBridge实现的舍入功能
桥的相关片段如下所示:
public class RoundedDoubleBridge
implements StringBridge
{
@Override
public String objectToString(Object value)
{
// Do not index null strings
if (value == null)
{
return null;
}
if (value instanceof Double)
{
long price = round((Double) value);
return Long.toString(price);
}
else
{
throw new IllegalArgumentException(
RoundedDoubleBridge.class + " used on a non double type: "
+ value.getClass());
}
}
private long round(double price)
{
double rounded = Math.floor(price / 3) * 3;
if (rounded != price)
{
rounded += 3; //we round up
}
return (long) rounded;
}
}
private long round(double price)
{
double rounded = Math.floor(price / 3) * 3;
if (rounded != price)
{
rounded += 3; //we round up
}
return (long) rounded;
}
注意价格=100000,变量四舍五入=99999
检查if条件后,因为它与价格不同,它将添加3,因此索引100002而不是100000,因此如果我查找100002,我将找到相应的记录
在此吸取的经验教训:
- 如果您正在实现一个定制的 垫块和/或旋转的双桥 确保舍入函数满足以下条件: 要避免的所有数据值范围 像我遇到的那样的问题
- 如果需要在屏幕中显示 在普通记数法中向上加倍,而不是 科学,你将需要实施 一种双向架桥机,可执行 ObjectToString转换填充 四舍五入,然后在 StringToObject转换返回一个 纯符号双精度且不在其上 科学符号
希望这篇文章能帮助任何有类似问题的人。问候失败,因为in返回零结果?抛出错误?嗨,返回零结果。我认为这与分析器有关,但不确定,我是一个Hibernate搜索和Lucene查询新手。如果有人能给我点灯,我将不胜感激。编辑:使用6位或更多数字的整数值进行测试搜索成功。。。所以我猜我的网桥实现是搜索失败的原因??谢天谢地:我同事的假设表明,应用于双重类型的科学符号是导致这个问题的原因。。。有什么想法吗?