Hibernate搜索失败,双重类型大于5位

时间:2011-02-15 20:05:13

标签: 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)
@Column(name = "total_debit_amount", nullable = false)
@FieldBridge(impl = RoundedDoubleBridge.class)
private Double totalDebitAmount;

Double to String桥接器实现如下:

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;
    }
}

所以这里的问题是,当totalDebitAmount或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;
    }

1 个答案:

答案 0 :(得分:1)

嗯,我觉得有点愚蠢。 我发现了我的问题.. 它具体是我的 FieldBridge 实现的舍入功能 桥梁的相关片段如下:

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,我会找到相应的记录..

这里吸取的教训:

  • 如果您要实施自定义 垫子和/或圆形的 DoubleBridge 确保舍入功能符合 要避免的所有数据值范围 像我这样的问题..
  • 如果您需要在屏幕上显示四舍五入 用普通符号表示双打,而不是 科学,你需要实施 执行的 TwoWayStringBridge ObjectToString 转换填充 四舍五入,后来 StringToObject 转换返回a 简单的符号Double而不是它的 科学记谱法。

希望这篇文章可以帮助任何有类似问题的人。迎接