Lucene中的自定義排序功能和Java集合中的自定義排序的實現(xiàn)方法差不多,都要實現(xiàn)一下比較接口. 在Java中只要實現(xiàn)Comparable接口就可以了.但是在Lucene中要實現(xiàn)SortComparatorSource接口和ScoreDocComparator接口.在了解具體實現(xiàn)方法之前先來看看這兩個接口的定義吧. SortComparatorSource接口的功能是返回一個用來排序ScoreDocs的comparator(Expert: returns a comparator for sorting ScoreDocs).該接口只定義了一個方法.如下: Java代碼 /** * Creates a comparator for the field in the given index. * @param reader - Index to create comparator for. * @param fieldname - Field to create comparator for. * @return Comparator of ScoreDoc objects. * @throws IOException - If an error occurs reading the index. */ public ScoreDocComparator newComparator(IndexReader reader,String fieldname) throws IOException view plaincopy to clipboardprint? /** * Creates a comparator for the field in the given index. * @param reader - Index to create comparator for. * @param fieldname - Field to create comparator for. * @return Comparator of ScoreDoc objects. * @throws IOException - If an error occurs reading the index. */ public ScoreDocComparator newComparator(IndexReader reader,String fieldname) throws IOException /** * Creates a comparator for the field in the given index. * @param reader - Index to create comparator for. * @param fieldname - Field to create comparator for. * @return Comparator of ScoreDoc objects. * @throws IOException - If an error occurs reading the index. */ public ScoreDocComparator newComparator(IndexReader reader,String fieldname) throws IOException 該方法只是創(chuàng)造一個ScoreDocComparator 實例用來實現(xiàn)排序.所以我們還要實現(xiàn)ScoreDocComparator 接口.來看看ScoreDocComparator 接口.功能是比較來兩個ScoreDoc 對象來排序(Compares two ScoreDoc objects for sorting) 里面定義了兩個Lucene實現(xiàn)的靜態(tài)實例.如下: Java代碼 //Special comparator for sorting hits according to computed relevance (document score). public static final ScoreDocComparator RELEVANCE; //Special comparator for sorting hits according to index order (document number). public static final ScoreDocComparator INDEXORDER; view plaincopy to clipboardprint? //Special comparator for sorting hits according to computed relevance (document score). public static final ScoreDocComparator RELEVANCE; //Special comparator for sorting hits according to index order (document number). public static final ScoreDocComparator INDEXORDER; //Special comparator for sorting hits according to computed relevance (document score). public static final ScoreDocComparator RELEVANCE;
//Special comparator for sorting hits according to index order (document number). public static final ScoreDocComparator INDEXORDER; 有3個方法與排序相關,需要我們實現(xiàn) 分別如下: Java代碼 /** * Compares two ScoreDoc objects and returns a result indicating their sort order. * @param i First ScoreDoc * @param j Second ScoreDoc * @return -1 if i should come before j; * 1 if i should come after j; * 0 if they are equal */ public int compare(ScoreDoc i,ScoreDoc j); /** * Returns the value used to sort the given document. The object returned must implement the java.io.Serializable interface. This is used by multisearchers to determine how to collate results from their searchers. * @param i Document * @return Serializable object */ public Comparable sortValue(ScoreDoc i); /** * Returns the type of sort. Should return SortField.SCORE, SortField.DOC, SortField.STRING, SortField.INTEGER, SortField.FLOAT or SortField.CUSTOM. It is not valid to return SortField.AUTO. This is used by multisearchers to determine how to collate results from their searchers. * @return One of the constants in SortField. */ public int sortType(); view plaincopy to clipboardprint? /** * Compares two ScoreDoc objects and returns a result indicating their sort order. * @param i First ScoreDoc * @param j Second ScoreDoc * @return -1 if i should come before j; * 1 if i should come after j; * 0 if they are equal */ public int compare(ScoreDoc i,ScoreDoc j); /** * Returns the value used to sort the given document. The object returned must implement the java.io.Serializable interface. This is used by multisearchers to determine how to collate results from their searchers. * @param i Document * @return Serializable object */ public Comparable sortValue(ScoreDoc i); /** * Returns the type of sort. Should return SortField.SCORE, SortField.DOC, SortField.STRING, SortField.INTEGER, SortField.FLOAT or SortField.CUSTOM. It is not valid to return SortField.AUTO. This is used by multisearchers to determine how to collate results from their searchers. * @return One of the constants in SortField. */ public int sortType(); /** * Compares two ScoreDoc objects and returns a result indicating their sort order. * @param i First ScoreDoc * @param j Second ScoreDoc * @return -1 if i should come before j; * 1 if i should come after j; * 0 if they are equal */ public int compare(ScoreDoc i,ScoreDoc j); /** * Returns the value used to sort the given document. The object returned must implement the java.io.Serializable interface. This is used by multisearchers to determine how to collate results from their searchers. * @param i Document * @return Serializable object */ public Comparable sortValue(ScoreDoc i); /** * Returns the type of sort. Should return SortField.SCORE, SortField.DOC, SortField.STRING, SortField.INTEGER, SortField.FLOAT or SortField.CUSTOM. It is not valid to return SortField.AUTO. This is used by multisearchers to determine how to collate results from their searchers. * @return One of the constants in SortField. */ public int sortType(); 看個例子吧! 該例子為Lucene in Action中的一個實現(xiàn),用來搜索距你最近的餐館的名字. 餐館坐標用字符串"x,y"來存儲. Java代碼 package com.nikee.lucene; import java.io.IOException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermDocs; import org.apache.lucene.index.TermEnum; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.ScoreDocComparator; import org.apache.lucene.search.SortComparatorSource; import org.apache.lucene.search.SortField; //實現(xiàn)了搜索距你最近的餐館的名字. 餐館坐標用字符串"x,y"來存儲 //DistanceComparatorSource 實現(xiàn)了SortComparatorSource接口 public class DistanceComparatorSource implements SortComparatorSource { private static final long serialVersionUID = 1L; // x y 用來保存 坐標位置 private int x; private int y; public DistanceComparatorSource(int x, int y) { this.x = x; this.y = y; } // 返回ScoreDocComparator 用來實現(xiàn)排序功能 public ScoreDocComparator newComparator(IndexReader reader, String fieldname) throws IOException { return new DistanceScoreDocLookupComparator(reader, fieldname, x, y); } //DistanceScoreDocLookupComparator 實現(xiàn)了ScoreDocComparator 用來排序 private static class DistanceScoreDocLookupComparator implements ScoreDocComparator { private float[] distances; // 保存每個餐館到指定點的距離 // 構造函數(shù) , 構造函數(shù)在這里幾乎完成所有的準備工作. public DistanceScoreDocLookupComparator(IndexReader reader, String fieldname, int x, int y) throws IOException { System.out.println("fieldName2="+fieldname); final TermEnum enumerator = reader.terms(new Term(fieldname, "")); System.out.println("maxDoc="+reader.maxDoc()); distances = new float[reader.maxDoc()]; // 初始化distances if (distances.length > 0) { TermDocs termDocs = reader.termDocs(); try { if (enumerator.term() == null) { throw new RuntimeException("no terms in field " + fieldname); } int i = 0,j = 0; do { System.out.println("in do-while :" + i ++); Term term = enumerator.term(); // 取出每一個Term if (term.field() != fieldname) // 與給定的域不符合則比較下一個 break; //Sets this to the data for the current term in a TermEnum. //This may be optimized in some implementations. termDocs.seek(enumerator); //參考TermDocs Doc while (termDocs.next()) { System.out.println(" in while :" + j ++); System.out.println(" in while ,Term :" + term.toString()); String[] xy = term.text().split(","); // 去處x y int deltax = Integer.parseInt(xy[0]) - x; int deltay = Integer.parseInt(xy[1]) - y; // 計算距離 distances[termDocs.doc()] = (float) Math.sqrt(deltax * deltax + deltay * deltay); } } while (enumerator.next()); } finally { termDocs.close(); } } } //有上面的構造函數(shù)的準備 這里就比較簡單了 public int compare(ScoreDoc i, ScoreDoc j) { if (distances[i.doc] distances[j.doc]) return -1; if (distances[i.doc] > distances[j.doc]) return 1; return 0; } // 返回距離 public Comparable sortValue(ScoreDoc i) { return new Float(distances[i.doc]); } //指定SortType public int sortType() { return SortField.FLOAT; } } public String toString() { return "Distance from (" + x + "," + y + ")"; } } view plaincopy to clipboardprint? package com.nikee.lucene; import java.io.IOException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermDocs; import org.apache.lucene.index.TermEnum; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.ScoreDocComparator; import org.apache.lucene.search.SortComparatorSource; import org.apache.lucene.search.SortField; //實現(xiàn)了搜索距你最近的餐館的名字. 餐館坐標用字符串"x,y"來存儲 //DistanceComparatorSource 實現(xiàn)了SortComparatorSource接口 public class DistanceComparatorSource implements SortComparatorSource { private static final long serialVersionUID = 1L; // x y 用來保存 坐標位置 private int x; private int y; public DistanceComparatorSource(int x, int y) { this.x = x; this.y = y; } // 返回ScoreDocComparator 用來實現(xiàn)排序功能 public ScoreDocComparator newComparator(IndexReader reader, String fieldname) throws IOException { return new DistanceScoreDocLookupComparator(reader, fieldname, x, y); } //DistanceScoreDocLookupComparator 實現(xiàn)了ScoreDocComparator 用來排序 private static class DistanceScoreDocLookupComparator implements ScoreDocComparator { private float[] distances; // 保存每個餐館到指定點的距離 // 構造函數(shù) , 構造函數(shù)在這里幾乎完成所有的準備工作. public DistanceScoreDocLookupComparator(IndexReader reader, String fieldname, int x, int y) throws IOException { System.out.println("fieldName2="+fieldname); final TermEnum enumerator = reader.terms(new Term(fieldname, "")); System.out.println("maxDoc="+reader.maxDoc()); distances = new float[reader.maxDoc()]; // 初始化distances if (distances.length > 0) { TermDocs termDocs = reader.termDocs(); try { if (enumerator.term() == null) { throw new RuntimeException("no terms in field " + fieldname); } int i = 0,j = 0; do { System.out.println("in do-while :" + i ++); Term term = enumerator.term(); // 取出每一個Term if (term.field() != fieldname) // 與給定的域不符合則比較下一個 break; //Sets this to the data for the current term in a TermEnum. //This may be optimized in some implementations. termDocs.seek(enumerator); //參考TermDocs Doc while (termDocs.next()) { System.out.println(" in while :" + j ++); System.out.println(" in while ,Term :" + term.toString()); String[] xy = term.text().split(","); // 去處x y int deltax = Integer.parseInt(xy[0]) - x; int deltay = Integer.parseInt(xy[1]) - y; // 計算距離 distances[termDocs.doc()] = (float) Math.sqrt(deltax * deltax + deltay * deltay); } } while (enumerator.next()); } finally { termDocs.close(); } } } //有上面的構造函數(shù)的準備 這里就比較簡單了 public int compare(ScoreDoc i, ScoreDoc j) { if (distances[i.doc] distances[j.doc]) return -1; if (distances[i.doc] > distances[j.doc]) return 1; return 0; } // 返回距離 public Comparable sortValue(ScoreDoc i) { return new Float(distances[i.doc]); } //指定SortType public int sortType() { return SortField.FLOAT; } } public String toString() { return "Distance from (" + x + "," + y + ")"; } } package com.nikee.lucene; import java.io.IOException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermDocs; import org.apache.lucene.index.TermEnum; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.ScoreDocComparator; import org.apache.lucene.search.SortComparatorSource; import org.apache.lucene.search.SortField; //實現(xiàn)了搜索距你最近的餐館的名字. 餐館坐標用字符串"x,y"來存儲 //DistanceComparatorSource 實現(xiàn)了SortComparatorSource接口 public class DistanceComparatorSource implements SortComparatorSource { private static final long serialVersionUID = 1L;
// x y 用來保存 坐標位置 private int x; private int y;
public DistanceComparatorSource(int x, int y) { this.x = x; this.y = y; }
// 返回ScoreDocComparator 用來實現(xiàn)排序功能 public ScoreDocComparator newComparator(IndexReader reader, String fieldname) throws IOException { return new DistanceScoreDocLookupComparator(reader, fieldname, x, y); }
// 構造函數(shù) , 構造函數(shù)在這里幾乎完成所有的準備工作. public DistanceScoreDocLookupComparator(IndexReader reader, String fieldname, int x, int y) throws IOException { System.out.println("fieldName2="+fieldname); final TermEnum enumerator = reader.terms(new Term(fieldname, ""));
System.out.println("maxDoc="+reader.maxDoc()); distances = new float[reader.maxDoc()]; // 初始化distances if (distances.length > 0) { TermDocs termDocs = reader.termDocs(); try { if (enumerator.term() == null) { throw new RuntimeException("no terms in field " + fieldname); } int i = 0,j = 0; do { System.out.println("in do-while :" + i ++); Term term = enumerator.term(); // 取出每一個Term if (term.field() != fieldname) // 與給定的域不符合則比較下一個 break;
//Sets this to the data for the current term in a TermEnum. //This may be optimized in some implementations. termDocs.seek(enumerator); //參考TermDocs Doc while (termDocs.next()) { System.out.println(" in while :" + j ++); System.out.println(" in while ,Term :" + term.toString());
String[] xy = term.text().split(","); // 去處x y int deltax = Integer.parseInt(xy[0]) - x; int deltay = Integer.parseInt(xy[1]) - y; // 計算距離 distances[termDocs.doc()] = (float) Math.sqrt(deltax * deltax + deltay * deltay); } } while (enumerator.next()); } finally { termDocs.close(); } } } //有上面的構造函數(shù)的準備 這里就比較簡單了 public int compare(ScoreDoc i, ScoreDoc j) { if (distances[i.doc] distances[j.doc]) return -1; if (distances[i.doc] > distances[j.doc]) return 1; return 0; }
// 返回距離 public Comparable sortValue(ScoreDoc i) { return new Float(distances[i.doc]); }
//指定SortType public int sortType() { return SortField.FLOAT; } }