rg.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldCollector;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
// From chapter 1
/**
* This code was originally written for searcher
*
*/
public class Searcher {
public static void main(String[] args) throws IllegalArgumentException,
IOException, ParseException {
final String indexDir = "e:\\soso\\soso";
String q = " ";//输入你添加的所以 进行模糊搜索
docs = query(indexDir, q)
}
public static void search(String indexDir, String q)
throws IOException, ParseException {
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDir)));
// Directory dir = FSDirectory.open(new File(indexDir)); //3
IndexSearcher is = new IndexSearcher(reader); //3
QueryParser parser = new QueryParser(Version.LUCENE_47,"contents",new SmartChineseAnalyzer(Version.LUCENE_47));
Query query = parser.parse(q); //4
long start = System.currentTimeMillis();
TopDocs hits = is.search(query, 500); //5
//ScoreDoc[] hits = is.search(query, null, 10).scoreDocs;
long end = System.currentTimeMillis();
System.err.println("Found " + hits.totalHits + //6
" document(s) (in " + (end - start) + // 6
" milliseconds) that matched query '" + // 6
q + "':"); // 6
for(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc); //7
System.out.println(doc.get("contents"));
}
reader.close();
}
private static List query(String indexDir, String searcher) throws IOException, ParseException{
if (searcher == null || searcher.length() == -1) {
return null;
}
searcher = searcher.trim();
if (searcher.length() == 0) {
return null;
}
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexDir)));//open the index
//IndexReader reader = DirectoryReader.open(SimpleFSDirectory.open(new File(indexDir)));//open the index
IndexSearcher is = new IndexSearcher(reader);//find the content
QueryParser parser = new QueryParser(Version.LUCENE_47, "contents", new SmartChineseAnalyzer(Version.LUCENE_47));//parser the content
Query query = parser.parse(searcher);
TopFieldDocs hits = is.search(query, 100, new Sort(new SortField("contents", SortField.Type.SCORE, false)));
TopDocs hits1 = is.search(query, 200);//搜索出前200条数据 按照评分进行排序
List list = new ArrayList();
for(ScoreDoc scoreDoc : hits.scoreDocs){
Document doc = is.doc(scoreDoc.doc);
list.add(doc.get("contents"));
}
reader.close();
return list;
}
}
//这里我主要给文档中的文本进行添加了索引 ,你也可以在Field 中给路径 等等一些属性进行添加索引 具体你可以搜索lucene api
进行使用 里面的一些方法。我这里说的比较粗,有问题欢迎讨论。