日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/9/27 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4.Lucene3.案例介绍,创建索引,查询等操作验证 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • 案例:

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

    ??}

    }

    ?

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的4.Lucene3.案例介绍,创建索引,查询等操作验证的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。