4.Lucene3.案例介绍,创建索引,查询等操作验证


  1. 案例:

Article.java

package cn.toto.lucene.quickstart;

 

publicclassArticle {

  privateintid;

  private Stringtitle;

  private Stringcontent;

  /**

   * @return the id

   */

  publicint getId() {

     returnid;

  }

  /**

   * @param id the id to set

   */

  

  publicvoid setId(int id) {

     this.id = id;

  }

  /**

   * @return the title

   */

  public String getTitle() {

     returntitle;

  }

  /**

   * @param title the title to set

   */

  

  publicvoid setTitle(String title) {

     this.title = title;

  }

  /**

   * @return the content

   */

  public String getContent() {

     returncontent;

  }

  /**

   * @param content the content to set

   */

  

  publicvoid setContent(String content) {

     this.content = content;

  }

}

工具类Configuration.java

package cn.toto.lucene.util;

 

import java.io.File;

 

import org.apache.lucene.analysis.Analyzer;

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

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.util.Version;

 

/**

 * @brief Configuration.java配置对象,提供Lucene需要  索引目录,分词器

 * @attention

 * @author toto

 * @date 2014-12-7

 * @note begin modify by涂作权

 */

public class Configuration {

   private static Directory directory;

   private static Analyzer analyzer;

   private static Version version;

   

   static {

            try {

                      //设置磁盘目录,表示的是本地index目录

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

                  } catch (Exception e) {

                           e.printStackTrace();

                  }

            //表示LUCENE版本

            version = Version.LUCENE_36;

            //表示使用版本

            analyzer = new StandardAnalyzer(version);

   }

   

   //提供目录

   public static Directory getDirectory()

   {

            return directory;

   }

   

   //提供分词器

   public static Analyzer getAnalyzer()

   {

            return analyzer;

   }

   

   //获取版本

   public static Version getVersion()

   {

            return version;

   }

}

工具类LuceneUtils.java

package cn.toto.lucene.util;

import java.io.IOException;

 

import org.apache.lucene.index.CorruptIndexException;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.search.IndexSearcher;

 

// lucene工具类

public class LuceneUtils {

        private static IndexWriter indexWriter;

        static {

                  // 索引目录位置

                  try {

                           // 写入索引

                           IndexWriterConfig indexWriterConfig = new IndexWriterConfig(

                                              Configuration.getVersion(), Configuration.getAnalyzer());

                           indexWriter = new IndexWriter(Configuration.getDirectory(),

                                              indexWriterConfig);

 

                           // 绑定虚拟机退出事件,关闭IndexWriter

                           Runtime.getRuntime().addShutdownHook(new Thread() {

                                    @Override

                                    public void run() {

                                              try {

                                                       indexWriter.close();

                                              } catch (CorruptIndexException e) {

                                                       e.printStackTrace();

                                              } catch (IOException e) {

                                                       e.printStackTrace();

                                             }

                                    }

                           });

                  } catch (IOException e) {

                           e.printStackTrace();

                  }

        }

 

        // 提供获取IndexWriter对象

        public static IndexWriter getIndexWriter() {

                  return indexWriter;

        }

 

        // 获得查询IndexSeacher对象

        public static IndexSearcher getIndexSearcher() throws Exception {

                  return new IndexSearcher(IndexReader.open(Configuration.getDirectory()));

        }

}

LuceneTest.java

package cn.toto.lucene.api;

 

import java.io.File;

importjava.io.IOException;

 

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.Store;

import org.apache.lucene.index.CorruptIndexException;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.queryParser.QueryParser;

import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.Query;

import org.apache.lucene.search.ScoreDoc;

import org.apache.lucene.search.TopDocs;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.store.LockObtainFailedException;

import org.apache.lucene.store.RAMDirectory;

import org.apache.lucene.util.Version;

import org.junit.Test;

 

import cn.toto.lucene.quickstart.Article;

import cn.toto.lucene.util.LuceneUtils;

 

// API详细分析

publicclass LuceneTest {

  @Test

  //使用LuceneUtils解决 IndexWriter并发问题

  @SuppressWarnings("unused")

  publicvoid testLock2() {

     IndexWriter indexWriter2 = LuceneUtils.getIndexWriter();

     IndexWriter indexWriter1 = LuceneUtils.getIndexWriter();

  }

 

  @Test

  @SuppressWarnings("all")

  //使用两个IndexWrtier报错,锁使用问题

  publicvoid testLock()throws CorruptIndexException,

        LockObtainFailedException, IOException {

     //索引目录位置

     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);

 

     IndexWriterConfig indexWriterConfig2 = new IndexWriterConfig(

           Version.LUCENE_36, analyzer);

     IndexWriter indexWriter2 = new IndexWriter(directory,

           indexWriterConfig2);

  }

上面的运行结果如下:

 

  @Test

  //测试Store Index

  /*

   * Store.YES 存储Store.NO不存储 Index.NO不建立索引 Index.ANALYZED分词建立索引

   * Index.NOT_ANALYZED 不分词建立索引Index.ANALYZED_NO_NORMS 分词建立索引,不存放权重信息

   * Index.NOT_ANALYZED_NO_NORMS 不分词建立索引,不存放权重信息

   */

  publicvoid testIndex()throws Exception {

     //需要建立索引目标数据

     Article article = new Article();

     article.setId(100);

     article.setTitle("学习全文检索");

     article.setContent("lucene是搜索引擎开发技术lucene并不是一个现成的产品,Apache提供");

 

     //将索引数据转换 Document对象lucene要求)

     Document document = new Document();

     document.add(new Field("id", article.getId() + "", Store.YES,

           Field.Index.NOT_ANALYZED));//对于id通常不分词

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

           Field.Index.ANALYZED_NO_NORMS));

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

           Field.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();

  }

上面的单元测试的结果

 

 

  @Test

  //查询索引库 ,查看norms效果

  publicvoid testQuery()throws Exception {

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

     String queryStrng = "检索";

     Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

     QueryParser queryParser = new QueryParser(Version.LUCENE_36,"title",

           analyzer);

     Query query = queryParser.parse(queryStrng);

 

     //根据Query查找

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

     IndexSearcher indexSearcher = new IndexSearcher(

           IndexReader.open(directory));

     //执行查询获得满足结果前多少条记录

     TopDocs topDocs = indexSearcher.search(query, 100);//查询满足结果前100条数据

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

 

     //获得每个结果

     ScoreDoc[] scoreDocs = topDocs.scoreDocs;

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

        //打印得分

        System.out.println("得分:" + scoreDocs[i].score);

        //获得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();

  }

上面的运行单元测试之后的结果如下:

 

  @SuppressWarnings("unused")

  @Test

  //测试路径写法

  publicvoid testDirectory()throwsIOException {

     //磁盘路径

     FSDirectory.open(new File("index"));//当前工程index目录,相对路径

     FSDirectory.open(new File("d:\\index"));//绝对路径

     //类路径 WEB-INF/classes

     FSDirectory.open(new File(LuceneTest.class.getResource("/").getFile()));

 

     //内存路径

     Directory directory = new RAMDirectory();

  }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值