码迷,mamicode.com
首页 > 其他好文 > 详细

简单全文检索

时间:2017-03-01 21:09:43      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:hits   mdi   write   res   mem   hit   text   index   for   

public static void main(String[] args) throws IOException, ParseException { 

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
// Store the index in memory:
Directory directory = new RAMDirectory();
// To store an index on disk, use this instead:
// Directory directory = FSDirectory.open(new File("C:/luceneIndex"));
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
IndexWriter iwriter = new IndexWriter(directory, config);
DirectoryReader ireader = DirectoryReader.open(directory);
Document doc = new Document();
String text = "doc"
+ "cgp 陈桂萍 to be text";
doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
iwriter.addDocument(doc);
iwriter.close();
System.out.println("建立索引 ok");
// Now search the index:
IndexSearcher isearcher = new IndexSearcher(ireader);
// Parse a simple query that searches for "text":
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer);
Query query = parser.parse("陈桂");
ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
System.out.println(hits.length == 1);
// Iterate through the results:
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
System.out.println(hitDoc.get("fieldname"));
}
ireader.close();
directory.close();

简单全文检索

标签:hits   mdi   write   res   mem   hit   text   index   for   

原文地址:http://www.cnblogs.com/lingasher/p/6485893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!