Lucene 的使用

来源:互联网 发布:买房需要注意什么 知乎 编辑:程序博客网 时间:2024/05/20 04:48

1、lucene的原理
            网络爬虫把资源存入索引库,再交给页面调用。而Lucene只负责管理索引库。


2、建 Java 项目Lucene_Test

3、导包

            lucene-core-3.0.1.jar
            lucene-analyzers-3.0.1.jar
            lucene-highlighter-3.0.1.jar
            lucene-memory-3.0.1.jar   

4、建类HelloWorld.java,分别创建“穿件“创建索引”和“查询”方法

public  class  HelloWorld{// 创建索引@Testpublic  void  testCreateIndex()  throws  Exception{}// 搜索@Testpublic  void  testSearch()  throws Exception{}}

5、建类 Article.java 作为javabean保存数据
            public  class  Article{                private  Integer id;                private  String title;                private  String  content;                封装……            }

6、实现“建立索引”和“搜索”功能

            // 索引库,设为全局变量                private static Directory  directory;    // 索引库目录                private  static Analyzer  analyzer;    // 分词器                                static{        //Shift + Alt + A                    try{                        directory  = FSDirectory.open(new  File("./indexDir"));                        analyzer  = new StandardAnalyze(Version.LUCENE_3.0);                    }catch(Exception  e){                        throw  new RuntimeException(e);                    }                }            // 创建索引                @Test                public  void  testCreateIndex()  throws  Exception{                    Article   article  = new Article();                    article.setTitle("标题");                    article.setContent("内容……");                    // 放到 Lucene 中                    // 1、把Article 转为 Document                        Document  doc = new Document();                        String  idStr = article.getId().toString();                        doc.add( new Field("id", idStr , Store.YES,  Index.ANALYZED ));                        doc.add( new Field("title", article.getTitle(), Store.YES,  Index.ANALYZED ));                        doc.add( new Field("content", article.getContent(), Store.YES,  Index.ANALYZED ));                                            //2、把Document 放到索引库中(此处设为全局变量,见开头)                                                        IndexWriter  indexWriter = new  IndexWriter( directory, analyzer,  MaxFieldLength.LIMITED);                        indexWriter.addDocument(doc);                        indexWriter.close();                }            // 搜索                @Test                public  void  testSearch()  throws Exception{                    // 准备查询条件                        String  queryString = "lucene";                    // 执行搜索                        List<Article>  list = new ArrayList<Article>();                    //======================================================                        //1、把查询字符串转为Query对象(默认只从title中查询)                            QueryParser  queryParser  = new  QueryParser(Version.LUCENE_30, "title",  analyzer);    // “title”表示从标题中查询                            Query  query = queryParser.parse(queryString);                        //2、执行查询,得到中间结果,从指定的索引库中搜索东西                            IndexSearcher  indexSearcher = new IndexSearcher(directory);                            TopDocs  topDocs  = indexSearcher.search(query,  100);    // 返回前 100 条结果,分页用                            int  count = topDocs.totalHits;                            ScoreDoc[]  scoreDoc = topDocs.scoreDocs;                        // 3、处理结果                            for(int i =0; i<scoreDocs.length; i++){                                ScoreDoc  scoreDoc = scoreDocs[i];                                float  score = scoreDoc.score;        //相关度得分                                int  docId = scoreDoc.doc;        //Document的内部编号,唯一且自动生成的                                // 根据编号拿到Document数据                                    Document  doc = indexsearcher.doc(docId);                                //把Document转为Article                                    String  idStr = doc.get("id");    //等价于doc.getField("content").stringValue();                                    String  title = doc.getField("title").stringValue();                                    String  content = doc.get("content");                                    Article  article = new Article();                                    article.setId(integer.parseIn(idStr));                                    article.setTitle(title);                                    article.setContent(content);                                    list.add(article);                            }                            indexSearcher.close();                    //======================================================                    // 显示结果                        s.o.p("总结果数:"+list.size());                        s.o.p("---------------------------------------------");                        for(Aritcle  a : list){                            s.o.p("id = " + a.getId() );                            s.o.p("title = " + a.getTitle() );                            s.o.p("content = " + a.getContent() );                        }                }

7、运行Junit

       结果:在项目中出现 indexDir 文件夹,里面出现三个索引库文件。

            总结果数:1
            id = ……
            title =……
            content =……

0 0