-->
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.  [ 3 posts ] 
Author Message
 Post subject: Lucene Custom sorting
PostPosted: Tue Feb 28, 2012 11:05 pm 
Regular
Regular

Joined: Tue May 17, 2011 1:45 am
Posts: 52
Hi,

In my index I have 2 fields

1. field 1 : City name 1 (cityname1)
2. field 2 : City name 2 (cityname2)

After indexing the index looks like this

Document 1 : cityname1= Los Angeles, cityname2=San Francisco
Document 1 : cityname1=New York , cityname2=Cupertino


In the UI I must show all cities (cityname1 or cityname2) in alphabetical order

so ideally I should get

Cupertino
Los Angeles
New York
San Francisco

In my code I am using new Sort(new SortField("cityname1",SortField.STRING),new SortField("cityname2",SortField.STRING));

But the outcome is that Lucene is sorting each sort field individually and coming like this
Los Angeles
San Fransisco
Cupertino
New York

Is there a way I can achieve the desired result or I have to write a custom sort?


Top
 Profile  
 
 Post subject: Re: Lucene Custom sorting
PostPosted: Wed Feb 29, 2012 4:41 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
why don't you sort each city name in a separate Document ?

If you have an index containing
Quote:
Document 1 : cityname1= Los Angeles, cityname2=San Francisco
Document 2 : cityname1=New York , cityname2=Cupertino


your resulting list will always be of two elements, nor Hibernate Search nor Lucene will be able to sort it in a list of 4 elements.

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


Top
 Profile  
 
 Post subject: Re: Lucene Custom sorting
PostPosted: Wed Feb 29, 2012 4:48 am 
Newbie

Joined: Wed Feb 29, 2012 4:21 am
Posts: 2
I would recommend you to implement the sort yourself. But Lucene.Net has an abstract class PriorityQueue that can be used in custom collectors (it is used internally in Lucene.Net while sorting (instead of collecting all results and then applying a sort on them) )

public class MyQueue : Lucene.Net.Util.PriorityQueue<int>
{
public MyQueue(int MaxSize) : base()
{
Initialize(MaxSize);
}

public override bool LessThan(int a, int b)
{
return a < b;
}
}

int queueSize = 3;
MyQueue pq = new MyQueue(queueSize);
pq.InsertWithOverflow(1);
pq.InsertWithOverflow(9);
pq.InsertWithOverflow(8);
pq.InsertWithOverflow(3);
pq.InsertWithOverflow(5);

int i1 = pq.Pop();
int i2 = pq.Pop();
int i3 = pq.Pop();


Hope this will help you ).

_________________
It is very important to stay afloat in our economically hard time. That is why getting short term cash advance with bad credit may help you fight money difficulties.


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