设为首页 加入收藏

TOP

自己动手写搜索引擎(常搜吧历程四#分词#)(Java、Lucene、hadoop)(四)
2014-11-24 12:04:51 来源: 作者: 【 】 浏览:162
Tags:自己 手写 搜索引擎 常搜吧 历程 #分词 Java Lucene hadoop
ield.Store.YES, Field.Index.ANALYZED));
document.add(new Field("contents", strBuffer.toString(), Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(document);
fs.close();
reader.close();
}
writer.close();
dir.close();
}
}
然后是对之前索引的检索类:
[java]
package com.qianyan.file;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class TestSearch {
public static void main(String[] args) throws IOException {
String indexDir = "E:/luceneindex";
Directory dir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new IndexSearcher(dir);
ScoreDoc[] hits = null;
Term term = new Term("fileName", "星期");
TermQuery query = new TermQuery(term);
TopDocs topDocs = searcher.search(query, 100);
hits = topDocs.scoreDocs;
for(int i = 0; i < hits.length; i++){
Document doc = searcher.doc(hits[i].doc);
System.out.print(hits[i].score + " ");
System.out.println(doc.get("fileName") + " ");
System.out.println(doc.get("contents") + " ");
} www.2cto.com
searcher.close();
dir.close();
}
}
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇设计模式详解之建造者模式 下一篇Java中的Annotation(1)----三个基..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: