lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From legrand thomas <thomaslegran...@yahoo.fr>
Subject Date sorting problem [ IndexSearcher | Hits | Sort | Float ]
Date Sat, 08 Mar 2008 18:57:56 GMT
Dear all,

I'm trying to sort query results using a date criteria. My dates are stored as "long" in the
database (I cannot change this) and indexed as untokenized. The sorted resuIts I get aren't
consistent. This problem does not occur if the number are "smaller".

Am I doing something wrong ? Is it possible to sort using "long" type ? What else should I
do ?

Regards & thanks in advance,
Tom


public void testDateSort(){
        System.out.println("[testDateSort][begin]");        
        
        IndexWriter mAdIndexWriter=null;
        Directory adIndexDir=null;
        
         Document doc0 = new Document();
         doc0.add(new Field("ID","doc0", Field.Store.YES,Field.Index.TOKENIZED));        
         doc0.add(new Field("DATE","1205000950000", Field.Store.YES,Field.Index.UN_TOKENIZED));
        
         Document doc1= new Document();
         doc1.add(new Field("ID","doc1", Field.Store.YES,Field.Index.TOKENIZED));        
         doc1.add(new Field("DATE","1205000950001", Field.Store.YES,Field.Index.UN_TOKENIZED));
         
         Document doc2 = new Document();
         doc2.add(new Field("ID","doc2", Field.Store.YES,Field.Index.TOKENIZED));        
         doc2.add(new Field("DATE","1205000950002", Field.Store.YES,Field.Index.UN_TOKENIZED));
         
        Analyzer analyser = new SimpleAnalyzer(); 
        try{
            adIndexDir=FSDirectory.getDirectory("C:\\YourFavoriteDirectory");
            mAdIndexWriter = new IndexWriter(adIndexDir, analyser, true);        

            mAdIndexWriter. addDocument(doc0); 
            mAdIndexWriter. addDocument(doc2); 
            mAdIndexWriter. addDocument(doc1); 
            
            mAdIndexWriter.optimize();
            mAdIndexWriter.close();
            
            IndexReader mAdIndexReader= IndexReader.open(adIndexDir); 
            IndexSearcher searcher = new IndexSearcher(mAdIndexReader);
            
            Query  query=new FuzzyQuery(new Term("ID","doc"),Float.parseFloat("0.8"));
            Sort timeSorter=new Sort(new SortField("DATE",SortField.FLOAT, false));      
     
            Hits allTheHits=searcher.search(query,timeSorter);
            
            for(int i = 0; i <allTheHits.length(); i++){ 
                System.out.println("Date n°" + i + " = " +allTheHits.doc(i).get("DATE"));
            }
            
            mAdIndexReader.close();
        }catch(Exception ex){
            ex.printStackTrace();
            fail();
        }
        System.out.println("[testDateSort][end]");        
    }


       
---------------------------------
 Envoyé avec Yahoo! Mail.
Une boite mail plus intelligente. 
Mime
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message