You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Andy Yu <uk...@gmail.com> on 2012/12/02 09:46:44 UTC

Re: sort by field and score

CustomComparatorSource is a class which extends FieldComparatorSource I
just want to custom the sort and add some business logic into the
comparator
actually my desirable situation is
firt sort by  CustomComparatorSource
and then sort by the score

thanks man


2012/11/30 Ian Lea <ia...@gmail.com>

> Using a TopFieldCollector works fine for me in a little test program.
> My program sorts on a simple String field rather than your
> CustomComparatorSource, whatever that is.
>
> SortField sortField[] = {
>             new SortField("cat", SortField.STRING),
>             SortField.FIELD_SCORE
>         };
> Sort sort = new Sort(sortField);
>
> Query q = whatever;
>
> TopFieldCollector tfc = TopFieldCollector.create(sort,
>                                                          1000,
>                                                          true,
>                                                          true,
>                                                          true,
>                                                          true);
> searcher.search(q, tfc);
> TopDocs td = tfc.topDocs();
>
>
> I suggest you break your code down into a simple standalone program
> and post that if it still doesn't work.
>
>
> --
> Ian.
>
> On Thu, Nov 29, 2012 at 4:20 AM, Andy Yu <uk...@gmail.com> wrote:
> > I revise the code to
> >
> >         SortField sortField[] = {new SortField("id", new
> > CustomComparatorSource(bitSet)),SortField.FIELD_SCORE};
> >
> >         Sort sort = new Sort(sortField);
> >
> >         TopFieldCollector topFieldCollector =
> > TopFieldCollector.create(sort, 1000, true, true, true, true);
> >         indexSearcher.search(query, topFieldCollector);
> >         TopDocs topDocs = topFieldCollector.topDocs();
> >
> > but I got the same result with the previous code, need I custom the
> > class TopFieldCollector?
> >
> > thank you lan
> >
> >
> > 2012/11/27 Ian Lea <ia...@gmail.com>
> >
> >> What are you getting for the scores?  If it's NaN I think you'll need
> >> to use a TopFieldCollector.  See for example
> >> http://www.gossamer-threads.com/lists/lucene/java-user/86309
> >>
> >>
> >> --
> >> Ian.
> >>
> >>
> >> On Tue, Nov 27, 2012 at 3:51 AM, Andy Yu <uk...@gmail.com> wrote:
> >> > Hi All,
> >> >
> >> >
> >> > Now  I want to sort by a field and the relevance
> >> > For example
> >> >
> >> > SortField sortField[] = {new SortField("id", new
> >> > CustomComparatorSource(bitSet)),SortField.FIELD_SCORE};
> >> > Sort sort = new Sort(sortField);
> >> > TopDocs topDocs = indexSearcher.search(query, 10,sort);
> >> >
> >> > if (0 < topDocs.totalHits) {
> >> >             for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
> >> >
> >> > System.out.println(indexSearcher.doc(scoreDoc.doc).get("id"));
> >> >                 System.out.println("score is " + scoreDoc.score);
> >> >
> >> >  System.out.println(indexSearcher.doc(scoreDoc.doc).get("name"));
> >> >             }
> >> >         }
> >> >
> >> > I found that the search result sort just by [new SortField("id", new
> >> > CustomComparatorSource(bitSet))]
> >> > [SortField.FIELD_SCORE] does not work at all
> >> >
> >> >
> >> > PS: my lucene version is 3.6
> >> >
> >> > does anybodu know the reason or how to solve it ?
> >> >
> >> >
> >> > Thanks ,
> >> > Andy
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> >> For additional commands, e-mail: java-user-help@lucene.apache.org
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>