-->
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.  [ 7 posts ] 
Author Message
 Post subject: Sort order
PostPosted: Wed Sep 22, 2010 8:09 am 
Beginner
Beginner

Joined: Thu Feb 25, 2010 11:33 am
Posts: 36
Hi everybody,
I've a little question about the Sort object. Before the question, I'm using lucene 2.9.3.
So, in the Hibernate Search in Action book, page 194-195 it's shown how to implement a sorting in the search results. I've tried to make it work, and it works almost perfectly. The only problem is that the sorting is in the wrong order. It's sorted from Z to A, and I want the results to be sorted from A to Z.

I've tried to reverse that, but I don't find anything ...

My actual code is this :
Code:
FullTextQuery query = ftSession.createFullTextQuery(luceneQuery, entityType);
SortField sorting = new SortField("enterprise_name", SortField.STRING_VAL, true);
Sort s = new Sort(sorting );
query.setSort(s);


Thanks in advance for your replies ...


Top
 Profile  
 
 Post subject: Re: Sort order
PostPosted: Thu Sep 23, 2010 4:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Have you tried to drop the last boolean parameter or set it to false? It is actually there reverse flag.

--Hardy


Top
 Profile  
 
 Post subject: Re: Sort order
PostPosted: Thu Sep 23, 2010 4:36 am 
Beginner
Beginner

Joined: Thu Feb 25, 2010 11:33 am
Posts: 36
Yes I tried to omit it and to set it to false, it doesn't change anything...
I've logged the return of the query.list() method, and I can see that that return is in the wrong order (independently of that boolean parameter), so it's not a bad manipulation from me with the returned elements.
But, in the case of a bug in my code, I put you my whole logic to interrogate lucene.
Code:
   this.results = 0;
   List<?> allResultsList = null;
   QueryParser parser = null;
   parser = new QueryParser(Version.LUCENE_29, this.defaultSearchAttribute, new SimpleAnalyzer());
   if (this.defaultOperator.equals("AND")) {
       parser.setDefaultOperator(Operator.AND);
   } else {
       parser.setDefaultOperator(Operator.OR);
   }
   Query luceneQuery = null;
   Session session = sessionFactory.openSession();
   Transaction transaction = null;
   try {
       luceneQuery = parser.parse(this.search);
       logger.debug("Lucene Generated query = " + luceneQuery.toString());
       transaction = session.beginTransaction();
       logger.debug("Getting Session");
       FullTextSession ftSession = Search.getFullTextSession(session);
       logger.debug("Creating full text Query");

       FullTextQuery query = ftSession.createFullTextQuery(luceneQuery, entityType);

       this.mustSortByName = true;
       if (this.mustSortByName) {
      SortField field = new SortField("enterprise_name", SortField.STRING, false);
      Sort s = new Sort(field);
      query.setSort(s);
       }

       this.results = query.getResultSize();

       query.setFirstResult(this.pageNumber * this.maxResults).setMaxResults(this.maxResults + 1);

       allResultsList = query.list();

       if (allResultsList != null) {
      for (Object o : allResultsList) {
          logger.debug("Found : " + o);
      }
       }

       logger.debug("Lucene query result size :" + allResultsList.size());
       int results = allResultsList.size();
       if (results > this.maxResults) {
      this.setHasNextPage(true);
      allResultsList.remove(this.maxResults);
       } else {
      this.setHasNextPage(false);
       }
       transaction.commit();
   } catch (ParseException ex) {
       logger.warn("Unable to parse query ", ex);
   } catch (HibernateException ex) {
       logger.warn("Hibernate Error", ex);
       transaction.rollback();
   }
   session.close();
   return allResultsList;

If you see anything strange / bad, do not hesitate :) Thanks


Top
 Profile  
 
 Post subject: Re: Sort order
PostPosted: Thu Sep 23, 2010 6:17 am 
Newbie

Joined: Thu Sep 23, 2010 4:43 am
Posts: 10
Hi,

You can try SortOption.ASCENDING,SortOption.DESCENDING

new SortField(null, SortField.SCORE, SortOption.ASCENDING.equals(args.getSortOrder()))


Top
 Profile  
 
 Post subject: Re: Sort order
PostPosted: Thu Sep 23, 2010 6:50 am 
Beginner
Beginner

Joined: Thu Feb 25, 2010 11:33 am
Posts: 36
What is args in this line ?
I also suppose that your line is about to generate the correct boolean to fit ... but I've tried both true and false, and it does not change anything. My results are always sorted in the wrong order ....


Top
 Profile  
 
 Post subject: Re: Sort order
PostPosted: Thu Sep 23, 2010 7:54 am 
Newbie

Joined: Thu Sep 23, 2010 4:43 am
Posts: 10
hi,

does your entity have an attribute called "enterprise_name"?


Top
 Profile  
 
 Post subject: Re: Sort order
PostPosted: Thu Sep 23, 2010 8:14 am 
Beginner
Beginner

Joined: Thu Feb 25, 2010 11:33 am
Posts: 36
Yes, declared like this :
Code:
    @Column(name = "ENTR_NAME", length = 100)
    @Fields( { @Field(index = Index.TOKENIZED, store = Store.YES),
       @Field(name = "enterprise_name", index = Index.UN_TOKENIZED) })
    private String name;


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.