2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程

简介:  1  Lucen目录介绍 2  lucene-core-3.6.2.jar是lucene开发核心jar包    contrib  目录存放,包含一些扩展jar包 3  案例 建立第一个Lucene项目:lucene3_day1    (1)需要先将数据转换成为Document对象,每一个数据信息转换成为Field(String


1  Lucen目录介绍

2  lucene-core-3.6.2.jarlucene开发核心jar

   contrib  目录存放,包含一些扩展jar

3  案例

建立第一个Lucene项目:lucene3_day1

   1)需要先将数据转换成为Document对象,每一个数据信息转换成为Field(String name, String value, Field.Store store, Field.Indexindex)

   2)指定索引库位置Directorydirectory = FSDirectory.open(new File("index"));// 当前Index目录

   3)分词器Analyzeranalyzer = new StandardAnalyzer(Version.LUCENE_36);

   4)写入索引:

IndexWriterConfig indexWriterConfig = new IndexWriterConfig(

            Version.LUCENE_36, analyzer);

IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);

     

//document数据写入索引库

indexWriter.addDocument(document);

//关闭索引

indexWriter.close();

案例编写:

案例目录:

Article.java

package cn.toto.lucene.quickstart;

 

public class Article {

   private int id;

   private String title;

   private String content;

   /**

    * @return the id

    */

   public int getId() {

      return id;

   }

   /**

    * @param id the id to set

    */

   public void setId(int id) {

      this.id = id;

   }

   /**

    * @return the title

    */

   public String getTitle() {

      return title;

   }

   /**

    * @param title the title to set

    */

   public void setTitle(String title) {

      this.title = title;

   }

   /**

    * @return the content

    */

   public String getContent() {

      return content;

   }

   /**

    * @param content the content to set

    */

   public void setContent(String content) {

      this.content = content;

   }

}

package cn.toto.lucene.quickstart;

 

import java.io.File;

 

import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.document.Field.Index;

import org.apache.lucene.document.Field.Store;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.util.Version;

import org.junit.Test;

 

/**

 * @brief LuceneTest.java 测试Lucene的案例

 * @attention

 * @author toto-pc

 * @date 2014-12-7

 * @note begin modify by 涂作权 2014/12/07 null

 */

public class LuceneTest {

   @Test

   public void buildIndex() throws Exception {

      Article article = new Article();

      article.setId(100);

      article.setTitle("Lucene快速入门");

      article.setContent("Lucene是提供了一个简单却强大的应用程式接口,"

            + "能够做全文检索索引和搜寻,在Java开发环境里Lucene" +

            "一个成熟的免费的开放源代码工具。");

 

      // 将索引数据转换成为Document对象(Lucene要求)

      Document document = new Document();

      document.add(new Field("id", // 字段

            article.getId() + "", Store.YES, // 是否建立索引

            Index.ANALYZED // 表示使用分词索引

      ));

      document.add(new Field("title", article.getTitle(), Store.YES,Index.ANALYZED));

      document.add(new Field("content", article.getContent(), Store.YES, Index.ANALYZED));

 

      // 建立索引库

      // 索引目录位置

      Directory directory = FSDirectory.open(new File("index"));// 当前Index目录

      // 分词器

      Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

      // 写入索引

      IndexWriterConfig indexWriterConfig = new IndexWriterConfig(

            Version.LUCENE_36, analyzer);

      IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

 

      // document数据写入索引库

      indexWriter.addDocument(document);

      // 关闭索引

      indexWriter.close();

   }

}

运行单元测试后的结果:

运行后index目录下的结果:

4  可以通过luke工具查看索引库中内容(它是一个jar包)

下载网址:http://code.google.com/p/luke/

打开方式:

如果用这种方式打不可以,可以用命令的方式打开文件,进入这个目录,选中Shift+鼠标右键—>此处打开命令窗口—>输入命令:java -jar lukeall-3.5.0.jar

工具的截图如下:

点击OK后的结果:

通过overview可以查看到索引信息,通过Document可以查看文档对象信息

5  查找

和上面的并集的query代码如下:

@Test

public void searchIndex() throws Exception

{

   //建立Query对象--根据标题

   String queryString = "Lucene";

   //第一个参数,版本号

   //第二个参数,字段

   //第三个参数,分词器

   Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

   QueryParser queryParser = new QueryParser(Version.LUCENE_36,"title",analyzer);

    Query query = queryParser.parse(queryString);

      

    //根据Query查找

    // 索引目录位置

   Directory directory = FSDirectory.open(new File("index"));

    IndexSearcher indexSearcher = new IndexSearcher(IndexReader.open(directory));

   //查询满足结果的前100条数据

   TopDocs topDocs = indexSearcher.search(query, 100);

    System.out.println("满足结果记录条数:" + topDocs.totalHits);

      

    //获取结果

    ScoreDoc[] scoreDocs = topDocs.scoreDocs;

    for (int i = 0; i < scoreDocs.length; i++) {

      //先获得Document下标

       int docID = scoreDocs[i].doc;

       Document document = indexSearcher.doc(docID);

       System.out.println("id:" + document.get("id"));

       System.out.println("title:" + document.get("title"));

       System.out.println("content:" + document.get("content"));

   }

 

    indexSearcher.close();

}

运行结果:

 

  1.  Luke查看的索引库内容:

索引库中信息,包括两大部分:

A 索引词条信息

B 文档对象信息

  1.  每个Field中都存在一个Store和一个Index

  2.  索引内容和Document内容有什么关系

查找时,通过索引内容  查找  文档对象信息

 

  1. 索引的查找过程

 

目录
相关文章
|
6月前
|
索引
07Lucene索引库的修改
07Lucene索引库的修改
15 0
|
6月前
|
索引
06Lucene索引库的删除
06Lucene索引库的删除
16 0
|
6月前
|
SQL 数据库 索引
08Lucene索引库查询 - 介绍
08Lucene索引库查询 - 介绍
25 0
|
存储 数据采集 自然语言处理
lucene 索引流程详细分析|学习笔记
快速学习 lucene 索引流程详细分析
101 0
lucene 索引流程详细分析|学习笔记
|
存储 自然语言处理 关系型数据库
Lucene的查询过程
Lucene的查询过程
167 0
|
搜索推荐 Java 测试技术
Lucene7.2.1系列(二)luke使用及索引文档的基本操作
它有以下功能: - 查看文档并分析其内容(用于存储字段) - 在索引中搜索 - 执行索引维护:索引运行状况检查;索引优化(运行前需要备份) - 从hdfs读取索引 - 将索引或其部分导出为XML格式 - 测试定制的Lucene分析工具 - 创建自己的插件
2428 0