Looking for software experts?
Need an expert advice on software development? Need consulting work done in time and at high standards? Tremend has the right solution for you.

We can provide expertise in:
  •    » high traffic and complex content website infrastructures using Java, PHP or .NET. More here ...
  •    » mobile applications for iPhone, Android or J2ME. More here ...

For an enquiry, send an email to contact [at] tremend [dot] ro.

A-Z 0-9 custom sorting in Lucene

May 17th, 2007 by Ioan Cocan in Java, General

The natural sort order for String fields is 0-9 A-Z so it seems a custom sorter is needed. I guess this is a normal request for any application that does a little sorting so I am surprised nothing came up during a search. Here is the AZ09Comparator:

/**
 * Custom Lucene sorting A-Z 0-9
 *
 * @author icocan
 */
public class AZ09Comparator implements SortComparatorSource {

    private static final Logger logger = Logger.getLogger(AZ09Comparator.class);

    public ScoreDocComparator newComparator(final IndexReader indexReader, final String str) throws IOException {
        return new ScoreDocComparator() {
            public int compare(ScoreDoc scoreDoc1, ScoreDoc scoreDoc2) {
                try {
                    final Document doc1 = indexReader.document(scoreDoc1.doc);
                    final Document doc2 = indexReader.document(scoreDoc2.doc);
                    final String strVal1 = doc1.get(str);
                    final String strVal2 = doc2.get(str);

                    boolean c1 = Character.isDigit(strVal1.charAt(0));
                    boolean c2 = Character.isDigit(strVal2.charAt(0));

                    if (c1 && c2) {
                        return strVal1.compareTo(strVal2);
                    } else if (c1) {
                        return 1;
                    } else if (c2) {
                        return -1;
                    } else {
                        return strVal1.compareTo(strVal2);
                    }
                } catch (IOException e) {
                    logger.error("Cannot read doc", e);
                }
                return 0;
            }

            public Comparable sortValue(ScoreDoc scoreDoc) {
                return new Float(scoreDoc.doc);
            }

            public int sortType() {
                return SortField.CUSTOM;
            }
        };
    }
}
DZoneGoogle ReaderYahoo MessengerRedditEmailDelicious

Related posts

.

3 Responses

  1. Pavan Says:

    Thanks a lot for the code.. It was really useful in Custom Sort Implementation

  2. Dmitry Says:

    The code is a good one.
    But since then a lot has changed.

    Now I’m trying to implement custom sorting and it doesn’t work with FieldComparatorSource on multiple shards (I’m actually using Solr and need to have sort order which is different from the natural one for field values).

  3. Lucene exact ordering « « Programmers Goodies Programmers Goodies Says:

    [...] suggest you separate the search from the sort and use a CustomSorter for the sorting: Here’s a blog entry describing a custom sorter. The classic Lucene book describes this [...]

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.