-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Search no retreiving Double type more than 5 digit long..
PostPosted: Tue Feb 15, 2011 3:59 pm 
Newbie

Joined: Tue Jan 25, 2011 1:15 pm
Posts: 8
Hi, I have the following fields on an entity:

Code:
@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;


The bridge implementation is the following:

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


So whenever the values on my database of totalDebitAmount or totalCreditAmount have more than 5 digits, e.g. 100001 the retreival of results fail, however, when any of the previously mentioned is lower or equal than 99999 search actually works..

Any comments?? Thanks


Top
 Profile  
 
 Post subject: Re: Search no retreiving Double type more than 5 digit long..
PostPosted: Tue Feb 15, 2011 4:18 pm 
Newbie

Joined: Tue Jan 25, 2011 1:15 pm
Posts: 8
Edit, perhaps its covenient to show how I am making the search.
Code:
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;
    }


EDIT:
Tested with Integer type more than 6 digit long and the search retrieves results... The problem according to different hypotheses is if double from 100000 and on applies scientific notation to the value.. (Using DB2 BTW)


Top
 Profile  
 
 Post subject: Re: Search no retreiving Double type more than 5 digit long..
PostPosted: Tue Feb 15, 2011 8:55 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I'm not sure what you are asking or trying to do. what is your goal?
Did you consider using @NumericField ?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Search no retreiving Double type more than 5 digit long..
PostPosted: Wed Feb 16, 2011 8:15 am 
Newbie

Joined: Tue Jan 25, 2011 1:15 pm
Posts: 8
Hi thanks for your reply.
What I am simply doing is, indexing those Double properties namely totalCreditAmount and totalDebitAmount in order to retrieve them via Hibernate Search and Lucene.
Since they are Double type, without a customized bridge like the one I implemented, the search would have to be done like say totalCreditAmount = 105547 on my DataBase, however the Default objectToString method of the default Built In Bridge indexes 105547.0 and not 105547.
However having done so, whenever the values of totalCreditAmount or totalDebitAmount are greater or equal to 100000 the search fails (i.e 0 results are returned) and whenever they are less than or equal to 99999 the search is done OK (i.e number of expected results returned successfully).
I am wanting to know what have I done wrong..
I will try @NumericField as you suggested and update.
Thanks for your attention

UPDATE: I tested using the customized Bridge and it works for numbers greater than 100000, however I must perform the search as 100000.0 and not 100000 similarly for other quantities greater than 100000, any Ideas, clearly the problem is now my customized bridge..


Top
 Profile  
 
 Post subject: Re: Search no retreiving Double type more than 5 digit long..
PostPosted: Wed Feb 16, 2011 9:18 am 
Newbie

Joined: Tue Jan 25, 2011 1:15 pm
Posts: 8
Well I feel kind of dumb.
I found my problem..
It is specifically at the rounding function of the implementation of my FieldBridge
The relevant snippet of the bridge is the following:
Code:
private long round(double price)
    {
        double rounded = Math.floor(price / 3) * 3;

        if (rounded != price)
        {
            rounded += 3; //we round up
        }

        return (long) rounded;
    }

Notice for price = 100000 the variable rounded is 99999
After checking the if condition since its different from price, it will add 3, hence indexing 100002 instead of 100000, therefore If I look up for 100002 I will find the appropriate record..
Lesson learned here:
[*]If you are implementing a customized DoubleBridge that pads and/or rounds make sure the rounding function meets all your data value ranges to avoid problems like the one I had..
[*]If you need to show in screen rounded up Doubles in plain notation and not scientific, you will need to implemnt a TwoWayStringBridge that performs the ObjectToString conversion padding and rounding and later on the StringToObject conversion returning a plain notation Double and not on its scientific notation.

Hope this post can help anyone with similar issues. Greets


Top
 Profile  
 
 Post subject: Re: Search no retreiving Double type more than 5 digit long..
PostPosted: Wed Feb 16, 2011 11:22 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
great you found the bug.
Still I'd highly recommend to use @NumericField instead, encoding an high number of different terms this way in the index will either not perform as good as it could, or get you in trobule with RangeQueries pointing to too many terms.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.