lucene 搜索
public static void main(String args[]) throws CorruptIndexException,IOException, ParseException {
??? ??? // IndexSearcher是实现搜索的关键
??? ??? IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File("X:/jspublisher/doc/entryindex")), true);
??? ??? /*??? ???
???? ??? //多条件
??? ??? ??? BooleanQuery bQuery = new BooleanQuery();
??? ??? ??? Query query1 = null;
??? ??? ??? BooleanClause.Occur[] flags = new BooleanClause.Occur[] {BooleanClause.Occur.SHOULD,BooleanClause.Occur.SHOULD};
??? ??? ??? query1 = MultiFieldQueryParser.parse(Version.LUCENE_29,"如何", new String[] {"ENTRY_NAME","DESC1"}, flags, new IKAnalyzer());
??? ??? ??? bQuery.add(query1, Occur.MUST);
??? ??? */
??? ??? //模糊查询
??? ?? Term term1 = new Term("ENTRY_NAME","数"+"~");
??? ? // Term term1 = new Term("DESC0","goog");
??? ?? FuzzyQuery query = new FuzzyQuery(term1,0.1f,1);
??? ??
??? ??? //精确匹配
/*??? ??? // query代表查询对象
??? ??? Query query = null;
??? ??? // 查询解析器, 将查询content字段
??? ??? QueryParser parser = new QueryParser(Version.LUCENE_29,"DESC", new IKAnalyzer());
??? ??? query = parser.parse(queryStr);
??? ??? // 搜索结果保存在TopScoreDocCollector.
*/??? ??? TopScoreDocCollector c = TopScoreDocCollector.create(100, true);
??? ??? // 搜索
??? ??? searcher.search(query, c);
???
??? ??? // 得到满足条件的文档总数
??? ??? System.out.println("总共搜索到 " + c.getTotalHits() + " 个资源。");
??? ??? // 得到文档集合
??? ??? ScoreDoc[] docs = c.topDocs(0, c.getTotalHits()).scoreDocs;
??? ???
??? ??? //存储符合条件的记录?
??? ??? int startIndex = 0,endIndex = 10; //startIndex、endIndex做为分页用
??? ???
??? ??? ArrayList<String> list=new ArrayList<String>();?
??? ??? for (int i = startIndex;i < endIndex && i < docs.length; i++) {
??? ??? ??? Document documents = searcher.doc(i);
??? ??? ??? int sum = Integer.parseInt(documents.get("DESC_COUNT"));
??? ??? ??? //注解
??? ??? ??? for(int a = 0 ;a < sum ;a++ ){
??? ??? ??? ??? //Document documentss = searcher.doc(a);
??? ??? ??? ??? System.out.println("count_desc--"+documents.get("DESC"+a));
??? ??? ??? }
??? ??? ??????? Document document=searcher.doc(docs[i].doc);
??? ??? ??????? int did = docs[i].doc;
??? ??? ??????? String d = String.valueOf(did);
??? ??? ??????? list.add(d);
??? ??? ??????? System.out.println("文档id: " + docs[i].doc+" 评分: "+docs[i].score+" 内容: "+document.get("ENTRY_NAME"));
??? ??? }
??? ??? System.out.println(list);
??? }