Lucene參與項目持久層中對於索引庫的增刪改查

本文主要介紹的是Lucene在參與項目中持久層的時候對於索引庫增刪改查的詳細使用;(爲了更好地使用Lucene,本文中使用的版本是Lucene4.04,使用的分詞器是IKAnalyzer2012FF)


1、Lucene實現增刪改查準備工作
2、Lucene持久層詳細實現


1、Lucene實現增刪改查準備工作

  • 第一步:創建Java工程,也可以創建pom.xml工程,需要五個jar包:
    common-io.jar
    IKAnalyzer2012FF_u1.jar
    lucene-analyzers-common-4.4.0.jar
    lucene-core-4.4.0.jar
    lucene-queryparser-4.4.0.jar
  • 第二步:我們需要一個JavaBean對象用於存儲網上新聞或者文章;網上的新聞或者文章主要是有文章題目;文章作者;文章目錄;文章鏈接四個方面組成所以,所以創建Article文章Bean類如下:

Article.java

/**
 * JAVABEAN用於方便存儲抓取的文章索引庫中的單個對象
 */
public class Article {

    private int id;

    private String title; 

    private String author;

    private String content;

    private String link;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }

    @Override
    public String toString() {
        return "Article [id=" + id + ", title=" + title + ", author=" + author
                + ", content=" + content + ", link=" + link + "]";
    }
}
  • 第三步:創建Lucene工具類(用於準備Lucene中最重要的indexWriter和indexReader兩個對象):

LuceneUtils.java

public class LuceneUtils {

    private static Directory directory;

    private static IndexWriterConfig indexWriterConfig;

    private static Version matchVersion = Version.LUCENE_44;

    private static Analyzer analyzer = new IKAnalyzer();

    static {

        try {

            directory = FSDirectory.open(new File(Contants.INDEXURL));

            indexWriterConfig = new IndexWriterConfig(matchVersion, analyzer);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 獲取返回用於操作索引的對象
     * @return
     * @throws IOException
     */
    public static IndexWriter getIndexWriter() throws IOException {

        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

        return indexWriter;
    }

    /**
     * 獲取用於查詢索引庫的對象
     * @return
     * @throws Exception
     */
    public static IndexSearcher getIndexSearcher() throws Exception {

        IndexReader indexReader = DirectoryReader.open(directory);

        IndexSearcher indexSearcher = new IndexSearcher(indexReader);

        return indexSearcher;
    }

    // 獲取索引庫地址
    public static Directory getDirectory() {
        return directory;
    }

    // 獲取當前的版本
    public static Version getMatchVersion() {
        return matchVersion;
    }

    // 獲取分詞分析器
    public static Analyzer getAnalyzer() {
        return analyzer;
    }

}

Contants.java

public interface Contants {
    // 存儲地址
    public static final String INDEXURL = "index/news";

}
  • 第四步:創建Article向Document轉化的工具類:

ArticleUtils.java

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;

import com.lucene.bean.Article;

/**
 * article 的轉化類
 */
public class ArticleUtils {

    /**
     * 將article轉化爲document
     * @param article
     * @return
     */
    public static Document articleDocument(Article article) {

        Document document = new Document();

        IntField IDField = new IntField("id", article.getId(), Store.YES);

        StringField titleField = new StringField("title", article.getTitle(), Store.YES);

        TextField contentField = new TextField("content", article.getContent(), Store.YES);

        StringField authorField= new StringField("author", article.getAuthor(), Store.YES);

        StringField urlField   = new StringField("link", article.getLink(), Store.YES);

        document.add(IDField);
        document.add(titleField);
        document.add(contentField);
        document.add(authorField);
        document.add(urlField);

        return document;
    }

}

2、Lucene持久層詳細實現

LuceneDao.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.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.junit.Test;

import com.lucene.bean.Article;
import com.lucene.utils.ArticleUtils;
import com.lucene.utils.LuceneUtils;

/**
 * Lucene操作索引庫(Dao層操作)
 */
public class LuceneDao {
    /**
     * 增刪改索引都通過indexWriter完成
     * @throws IOException 
     */
    /**
     * 添加索引庫
     * @param article
     * @throws IOException
     */
    @Test
    public void addIndex(Article article) throws IOException {

        IndexWriter indexWriter = LuceneUtils.getIndexWriter();

        Document document = ArticleUtils.articleDocument(article);

        indexWriter.addDocument(document);

        indexWriter.close();
    }

    /**
     * 根據字段刪除索引,刪除對應的值
     * @param fieldName
     * @param fieldValue
     * @throws Exception
     */
    public void delIndex(String fieldName, String fieldValue) throws Exception {
        IndexWriter indexWriter = LuceneUtils.getIndexWriter();

        Term term = new Term(fieldName, fieldValue);

        indexWriter.deleteDocuments(term);
        indexWriter.commit();
        indexWriter.close();
    }

    /**
     * 更新索引庫中的內容
     * @param fieldName
     * @param fieldValue
     * @param article
     * @throws IOException
     */
    public void updateIndex(String fieldName, String fieldValue,Article article) throws IOException {
        IndexWriter indexWriter = LuceneUtils.getIndexWriter();

        Term term = new Term(fieldName, fieldValue);

        Document document = ArticleUtils.articleDocument(article);

        indexWriter.updateDocument(term, document);

        indexWriter.commit();

        indexWriter.close();
    }

    /**
     * 
     * 分頁數據
     * 
     * 顯示數據進行分頁 0 , 10
     * 
     * 顯示數據進行分頁11 , 20 
     * 
     * 在索引庫中根據關鍵字查找
     * @param keywords
     * @return
     * @throws Exception
     */
    public List<Article> findIndex(String keywords, int start, int row) throws Exception {

        IndexSearcher indexSearcher = LuceneUtils.getIndexSearcher();

        // 需要根據那幾個字段進行檢索
        String fields[] = {"title","content"};
//      String fields[] = {"author"};

        QueryParser queryParser = new MultiFieldQueryParser(LuceneUtils.getMatchVersion(), fields, LuceneUtils.getAnalyzer());

        // 不同的規則構造不同的子類
        // title:keywords , content:keywords
        Query query = queryParser.parse(keywords);

        TopDocs topDocs = indexSearcher.search(query, start+row);

        System.out.println("總記錄數====total===="+topDocs.totalHits);

        ScoreDoc scoreDocs[] = topDocs.scoreDocs;

        Article article = null;

        List<Article> articlelist = new ArrayList<Article>();

        int endResult = Math.min(scoreDocs.length, start+row);

        for (int i = start; i < endResult; i++) {
            int docID = scoreDocs[i].doc;
            article = new Article();
            Document document = indexSearcher.doc(docID);
            article.setId(Integer.parseInt(document.get("id")));
            article.setTitle(document.get("title"));
            article.setContent(document.get("content"));
            article.setLink(document.get("link"));
            article.setAuthor(document.get("author"));

            articlelist.add(article);
        }

        return articlelist;

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章