接http://www.javaeye.com/topic/39876
下面是搜索的例子:
[code]
public void SearchSort1() throws IOException, ParseException
{
IndexSearcher indexSearcher = new IndexSearcher("C://indexStore");
QueryParser queryParser = new QueryParser("sort",new StandardAnalyzer());
Query query = queryParser.parse("4");
Hits hits = indexSearcher.search(query);
System.out.println("有"+hits.length()+"個結果");
Document doc = hits.doc(0);
System.out.println(doc.get("sort"));
}
public void SearchSort2() throws IOException, ParseException
{
IndexSearcher indexSearcher = new IndexSearcher("C://indexStore");
Query query = new RangeQuery(new Term("sort","1"),new Term("sort","9"),true);//這個地方前面沒有提到,它是用於範圍的Query可以看一下幫助文檔.
Hits hits = indexSearcher.search(query,new Sort(new SortField("sort",new MySortComparatorSource())));
System.out.println("有"+hits.length()+"個結果");
for(int i=0;i
{
Document doc = hits.doc(i);
System.out.println(doc.get("sort"));
}
}
public class MyScoreDocComparator implements ScoreDocComparator
{
private Integer[]sort;
public MyScoreDocComparator(String s,IndexReader reader, String fieldname) throws IOException
{
sort = new Integer[reader.maxDoc()];
for(int i = 0;i
{
Document doc =reader.document(i);
sort[i]=new Integer(doc.get("sort"));
}
}
public int compare(ScoreDoc i, ScoreDoc j)
{
if(sort[i.doc]>sort[j.doc])
return 1;
if(sort[i.doc]