Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092844
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类:

2009-08-05 14:49:31

import org.apache.lucene.document.Document;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;

import java.io.FileReader;
import java.io.BufferedReader;

public class TestSearch
{
    public static void main(String[] args) throws Exception
    {
        IndexWriter writer = new IndexWriter("/root/index", new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);

        Document doc = new Document();
        FileReader reader = new FileReader("/root/store/email.txt");
        Field field = new Field("content", reader);
        Field field2 = new Field("filename", "/root/store/email.txt", Field.Store.YES, Field.Index.TOKENIZED);
        
        doc.add(field);
        doc.add(field2);
        
        writer.addDocument(doc);
        writer.close();
        
        IndexSearcher searcher = new IndexSearcher("/root/index");
        QueryParser parser = new QueryParser("content", new StandardAnalyzer());
        Query query = parser.parse("haoyuan");
        
        Hits hits = searcher.search(query);
        for (int i = 0; i < hits.length(); i++)
        {
            Document ret = hits.doc(i);
            String str = ret.get("filename");
            
            FileReader freader = new FileReader(str);
            BufferedReader in = new BufferedReader(freader);
            
            String line;
            while ((line = in.readLine()) != null)
                System.out.println(line);
            in.close();    
        }
    }
}

阅读(1083) | 评论(0) | 转发(0) |
0

上一篇:java 索引分词

下一篇:lucene 排序查找结果

给主人留下些什么吧!~~