lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Huang <metapr...@yahoo.com>
Subject Re: Sort by relevance+distance
Date Sat, 17 Sep 2005 23:00:09 GMT
I use a custom collector:

class ResultCollector extends HitCollector
{
  SortedSet set = new TreeSet();
  IndexSearcher searcher;
  Location me;

  ResultCollector(IndexSearcher searcher, Location me)
  {
    this.me = me;
    this.searcher = searcher;
  }

  public void collect(int id, float score) {
    try {
      Document doc = helper.searcher.doc(id);
      String zc = doc.get("zipcode");
      SearchResult sr = new SearchResult(
         score, zc, getDistance(me, zc));
      // The score in SearchResult is adjusted:
      // score *= 1.0 - distance/200.0;
      set.add(sr);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  int getResult(int startindex, SearchResult[] result)
  {
    Iterator iter = set.iterator();
    int idx = 0;
    for (int i=0; iter.hasNext() && idx <
result.length; ++i) {
      Object o = iter.next();
      if (i >= startindex)
        result[idx++] = (SearchResult)o;
    }
    return set.size();
  }
}

The SearchResult extends Comparable.
Then, use IndexSearcher.search(qry, collector);

This seems to work. What I wish for is that sorting is
done by the search engine itself, hoping for a better
performance (and cleaner code).

Previously, I have created a DistanceComparatorSource
(similar to that in LIA-ch6); sorting by distance
works but relevance is lost.

-James

--- Erik Hatcher <erik@ehatchersolutions.com> wrote:

> 
> On Sep 17, 2005, at 4:10 PM, James Huang wrote:
> 
> > Hi,
> >
> > I can sort the search results by distance now.
> But,
> > the relevance is lost.
> >
> > I like to have the results sorted by relevance +
> > distance, i.e., relevance first; for results of
> > similar relevance, order by distance. How to do
> that?
> 
> How are you currently sorting?   You can use
> multiple sort fields  
> within a Sort.
> 
>      Erik
>


		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message