Lucene-使用

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.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
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.util.Version;
import org.junit.Test;

import com.itheima11.lucene.bean.Article;
import com.itheima11.lucene.utils.DocumentUtils;
import com.itheima11.lucene.utils.LuceneUtils;

public class ArticleIndex {
    /**
     * 创建一个索引
     */
    @Test
    public void testCreateIndex() throws Exception{
        //创建一个article对象
        Article article = new Article();
        article.setAid(1L);
        article.setTitle("lucene是一个全文检索引擎");
        article.setContent("baidu,google都是很好的全文检索引擎");
        //创建一个indexWriter
        IndexWriter indexWriter = 
                    new IndexWriter(LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
        //把article转化成document
        Document document = DocumentUtils.article2Document(article);
        //把document加入到索引库中
        indexWriter.addDocument(document);
        indexWriter.close();
    }

    @Test
    public void testSearchIndex() throws Exception{
         IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
         QueryParser queryParser = 
                    new MultiFieldQueryParser(
                            Version.LUCENE_30,
                            new String[]{"title","content"},
                            LuceneUtils.analyzer);
         Query query = queryParser.parse("lucene");
         TopDocs topDocs = indexSearcher.search(query, 1);
         ScoreDoc[] scoreDocs = topDocs.scoreDocs;
         List
articles = new ArrayList
(); for (ScoreDoc scoreDoc : scoreDocs) { int index = scoreDoc.doc; Document document = indexSearcher.doc(index); Article article = DocumentUtils.document2Article(document); articles.add(article); } for (Article article : articles) { System.out.println(article.getAid()); System.out.println(article.getTitle()); System.out.println(article.getContent()); } } /** * 删除 * 不是把原来的cfs文件删除掉了,而是多了一个del文件 */ @Test public void testDelete() throws Exception{ /** * 关键词对象 * 要查找一些字段:字段的名称为"title",字段中含有"lucene"的关键词 */ Term term = new Term("title","lucene"); IndexWriter indexWriter = new IndexWriter( LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED); indexWriter.deleteDocuments(term); indexWriter.close(); } /** * 修改 * 先删除后增加 */ @Test public void testUpdate() throws Exception{ Term term = new Term("title","lucene"); IndexWriter indexWriter = new IndexWriter( LuceneUtils.directory, LuceneUtils.analyzer, MaxFieldLength.LIMITED); Article article = new Article(); article.setAid(1L); article.setTitle("lucene是一个全文检索引擎"); article.setContent("-----------"); Document doc = DocumentUtils.article2Document(article); /** * term参数是用来删除的 * doc是用来增加的 */ indexWriter.updateDocument(term, doc); indexWriter.close(); } }

你可能感兴趣的:(lucene)