`
384444165
  • 浏览: 255644 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

(From原博客)Lucene实现以索引文件简单的搜索

阅读更多
承接上次的txt文本索引,Lucene操作很固定化,看看源码其实就是很好的学习过程,在这跟大家分享基本的搜索方法实现,之后对索引和搜索方法会写些复杂点的操作给大家,这个可以作为一个入门和兴趣培养啊,哪里不懂google搜索一下几分钟就能看懂:

package testLucene;

import java.util.Date;

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

public class Search {

    private IndexSearcher search=null;
    
    private Query query=null;
    
    //搜索关键字方法 返回Hits
    public final Hits SearchKeyword(String keyword){
        System.out.println("正在搜索关键字:"+keyword);
        try{
            query=QueryParser.parse(keyword, "contents", new StandardAnalyzer());
            
            Date start=new Date();
            Hits hits=search.search(query);
            Date end=new Date();
            System.out.println("检索用时:"+(end.getTime()-start.getTime())+"毫秒");
            return hits;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
    
    //构造函数 初始化search
    public Search(){
        try{
            search=new IndexSearcher(IndexReader.open(Constants.Index_Store_Path));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //输出Hits的结果
    public void printResult(Hits h){
        if(h.length()==0){
            System.out.println("对不起,没有找到搜索结果");
        }else{
            for(int i=0;i<h.length();i++){
                try{
                    Document doc=h.doc(i);
                    System.out.print("这是第"+i+"个搜索到的结果,文件名为:");
                    System.out.println(doc.get("path"));
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
        System.out.println();
    }
    
    //main方法 使用上述方法,实现此次搜索
    public static void main(String[] args){
        Search sea=new Search();
        Hits h=null;
        //检索“叫做”
        h=sea.SearchKeyword("叫做");
        sea.printResult(h);
        //检索“文件”
        h=sea.SearchKeyword("文件");
        sea.printResult(h);
        //检索“新建”
        h=sea.SearchKeyword("新建");
        sea.printResult(h);
        //检索“我”
        h=sea.SearchKeyword("我");
        sea.printResult(h);
    }
}



其中上次四次文件的内容分别是:
a.txt:我新建的文件叫做a
b.txt:新建的文件叫做b
c.txt:文件叫做c
d.txt:叫做d

运行结果:
正在搜索关键字:叫做
检索用时:110毫秒
这是第0个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\d.txt
这是第1个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\c.txt
这是第2个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\a.txt
这是第3个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\b.txt

正在搜索关键字:文件
检索用时:15毫秒
这是第0个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\c.txt
这是第1个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\a.txt
这是第2个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\b.txt

正在搜索关键字:新建
检索用时:0毫秒
这是第0个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\a.txt
这是第1个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\b.txt

正在搜索关键字:我
检索用时:16毫秒
这是第0个搜索到的结果,文件名为:E:\E\Java\Lucene\testUse\test1\a.txt
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics