| Back to logs list
10013702 2009 年 03 月 17 日 21:22 Reading (loading. ..) Comments (0) Category: Personal Diary
in Lucene.net implement custom sorting,
monster beats headphones, two Lucene.Net.Search need to implement two interfaces:
public interface SortComparatorSource
{
ScoreDocComparator NewComparator (IndexReader reader , System.String fieldname);
}
public interface ScoreDocComparator
{
int Compare (ScoreDoc i, ScoreDoc j);
System. IComparable SortValue (ScoreDoc i);
int SortType ();
}
relates to a class:
public class ScoreDoc
{
public float score;
public int doc;
public ScoreDoc (int doc,
monster in-ear headphones, float score)
{
this.doc = doc;
this.score = score;
}
}
Lucene.net 2.0 contains SortType are:
defined in the Lucene.Net.Search.SortField:
public class SortField
{
public const int SCORE = 0; / / relevance
public const int DOC = 1; ; / / text block No.
public const int AUTO = 2; / / Automatic Identification
public const int STRING = 3; / / char
public const int INT = 4; / / int
public const int FLOAT = 5; / / float
; public const int CUSTOM = 9; / / custom
..
}
less DateTime, DateTime types would be to achieve a custom sort to test under :
Lucene.Net.Search.ScoreDocComparator interface implementation class:
public class DateDocComparator: Lucene.Net.Search.ScoreDocComparator
{
private string fieldname = null;
; private System.IComparable [] cachedValues;
public DateDocComparator (System.IComparable [] cachedValues,
dr dre headphones, string fieldname)
; {
this.cachedValues = cachedValues;
this.fieldname = string.Intern (fieldname);
}
public int Compare (ScoreDoc i, ScoreDoc j)
{
return this.cachedValues.CompareTo (this.cachedValues [j.doc ]);
}
public System.IComparable SortValue (ScoreDoc i)
{
; return this.cachedValues;
}
public int SortType ()
{
return Lucene.Net.Search.SortField.CUSTOM;
}
}
Lucene.Net.Search.SortComparatorSource interface implementation class:
public class DateSortComparatorSource: Lucene.Net.Search.SortComparatorSource
{
public ScoreDocComparator NewComparator (Lucene.Net.Index.IndexReader reader, System.String field)
; {
return new DateDocComparator (GetCustom (reader,
beats by dre studio, field), field);
}
protected virtual System.IComparable [] GetCustom (Lucene. Net.Index.IndexReader reader, System.String field)
{
; System.IComparable [] retArray = new System.IComparable [reader.MaxDoc ()];
; Lucene.Net.Index.TermDocs termDocs = reader.TermDocs ();
; Lucene.Net.Index.TermEnum termEnum = reader.Terms (new Lucene.Net.Index.Term (field, \);
try
; {
; do
; {
; Lucene.Net.Index.Term term = termEnum.Term ();
; if (term == null | | term.Field (),
beats headphones! = field)
; ; break;
System.IComparable termval = Lucene.Net.Documents.DateTools.StringToDate (term.Text ());
; termDocs.Seek (termEnum);
; while (termDocs.Next ())
{
retArray [termDocs.Doc ()] = termval;
}
}
while (termEnum.Next ());
}
finally
{
termDocs.Close ();
; termEnum.Close ();
}
return retArray;
}
}
use:
Sort sort = new Sort (new SortField (\