package org.apache.lucene.demo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
public class Test {
public static void main(String[] args) throws CorruptIndexException,
LockObtainFailedException, IOException, ParseException {
List<PoJo> all = getList();
Directory directory = new RAMDirectory();
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
IndexWriter indexWriter = new IndexWriter(directory, analyzer, true,
IndexWriter.MaxFieldLength.LIMITED);
Document document = null;
Iterator<PoJo> it = all.iterator();
PoJo pojo = null;
while (it.hasNext()) {
pojo = it.next();
document = new Document();
document.add(new NumericField("id", Field.Store.YES, true)
.setIntValue(pojo.getId()));
document.add(new Field("name", pojo.getName(), Field.Store.YES,
Field.Index.ANALYZED));
document.add((new NumericField("order", Field.Store.YES, true)
.setIntValue(pojo.getOrder())));
indexWriter.addDocument(document);
}
indexWriter.optimize();
indexWriter.close();
IndexReader reader = IndexReader.open(directory, true);
Searcher searcher = new IndexSearcher(reader);
Sort sort = new Sort() ;
sort.setSort(new SortField("order", SortField.INT ,true)) ;
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "name",
analyzer);
Query query = parser.parse("ZB");
TopDocs results = searcher.search(query, null, 2000, sort);
for (int i = 0; i < results.scoreDocs.length; i++) {
Document doc = searcher.doc(results.scoreDocs[i].doc);
System.out.println(doc.get("id") + " " + doc.get("name")
+ " " + doc.get("order"));
}
}
public static List<PoJo> getList() {
List<PoJo> all = new ArrayList<PoJo>();
for (int i = 1; i < 2000; i++) {
String name = getName(i);
all.add(new PoJo(i, name + " " + new Random().nextInt(1000),
new Random().nextInt(1000)));
}
return all;
}
public static String getName(int i) {
String name = null;
switch (i % 5) {
case 0:
name = "HB";
break;
case 1:
name = "XJ";
break;
case 2:
name = "XZ";
break;
case 3:
name = "BJ";
break;
case 4:
name = "ZB";
break;
default:
name = "LJ";
}
return name;
}
}
class PoJo {
private int id;
private String name;
private int order;
public PoJo(int id, String name, int order) {
this.id = id;
this.name = name;
this.order = order;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
lucene 3.0 sort 类适用--个人看的没做注释
最新推荐文章于 2024-09-10 09:07:08 发布