Lucene的快速入门

Lucene快速入门代码

package cn.itcast.javaee.lucene.firstapp;

import cn.itcast.javaee.lucene.entity.Article;
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.document.Field.Index;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
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.util.Version;
import org.junit.Test;

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


public class FirstApp {

    @Test
    //创建词汇索引库!
    public void createIndexDB() throws Exception{
        //创建实体对象
        Article article = new Article(1,"培训","是一个培训机构");

        //创建文本对象
        Document document = new Document();
        /*Store.YES 存储到词库中*/
        /*Index.ANALYZED) 分词*/
        /*name名称可以与实体对象的属性名一一对应也可以不对应*/
        document.add(new Field("xid",article.getId().toString(),Store.YES, Index.ANALYZED));
        document.add(new Field("xtitle",article.getTitle(),Store.YES, Index.ANALYZED));
        document.add(new Field("xcontent",article.getContent(),Store.YES, Index.ANALYZED));


        //将document对象写入Lucene索引库
        Directory directory = FSDirectory.open(new File("E:/indexDBDBDB"));   //索引库存放硬盘的位置

        //Ctrl+alt+b 查看一个接口的所有实现类
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);  //采用什么策略来拆分文字 参数为采用指定的版本

        MaxFieldLength maxFieldLength= IndexWriter.MaxFieldLength.LIMITED; //最将文本拆分多少个词汇 limited可以最多拆分1万个词

        IndexWriter indexWriter = new IndexWriter(directory,analyzer,maxFieldLength);
        //将文本对象document写入lucene索引库中
        indexWriter.addDocument(document);

        //关闭资源
        indexWriter.close();

    }




    //创建查询 从索引库中查询符合条件的数据
    @Test
    public void findIndexDB() throws Exception{
        String keywords = "培训";
        List<Article> articleList = new ArrayList<Article>();
        //将document对象写入Lucene索引库
        Directory directory = FSDirectory.open(new File("E:/indexDBDBDB"));   //索引库存放硬盘的位置
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);  //采用什么策略来拆分文字 参数为采用指定的版本


        //创建indexSwarcher流对象
        IndexSearcher indexSearcher = new IndexSearcher(directory); //寻找词库的位置

        //创建查询对象
        /*对应的参数意思是: 1版本号, 2搜索的范围只再document中的Xcontent文本类搜索,3用的是那种解析器s*/
        QueryParser queryParser = new QueryParser(Version.LUCENE_30,"xcontent",analyzer);
        Query query = queryParser.parse(keywords);

        /*如果搜索出来的关键数据过多 只取出来Max_Record对应值的个数*/
        int MAX_RECORD = 100;
        TopDocs topDocs = indexSearcher.search(query, MAX_RECORD);

        for(int i = 0; i<topDocs.scoreDocs.length; i++){
            //取出每一个编号
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            int no = scoreDoc.doc;

            //封装到doc中
            Document document = indexSearcher.doc(no);
            String xid = document.get("xid");
            String xtitle = document.get("xtitle");
            String xcontent = document.get("xcontent");

            //封装到Artilce bean对象中
            Article article = new Article(Integer.parseInt(xid),xtitle,xcontent);
            articleList.add(article);
        }
        //遍历搜索到内容集合
        for(Article a :articleList){
            System.out.println(a);
        }
    }
}

 

 

Lucene的增删改查

package cn.itcast.javaee.lucene.crud;


import cn.itcast.javaee.lucene.entity.Article;
import cn.itcast.javaee.lucene.utils.LuceneUtil;
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.index.IndexWriter;
import org.apache.lucene.index.Term;
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.util.Version;
import org.junit.Test;

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

/*增删改查*/
public class ArticleDao {

    @Test
    public void add() throws Exception{
        Article article = new Article(1,"培训","传智是一家java培训机构");
        //创建文本对象
        Document document = new Document();
        document.add(new Field("id",article.getId().toString(), Field.Store.YES, Field.Index.ANALYZED));
        document.add(new Field("title",article.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
        document.add(new Field("content",article.getContent(), Field.Store.YES, Field.Index.ANALYZED));

        Directory directory = FSDirectory.open(new File("E:/indexDBDBDB")); //Field.Store.YES
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
        IndexWriter.MaxFieldLength maxFieldLength = IndexWriter.MaxFieldLength.LIMITED;

        //创建indexWriter对象
        IndexWriter indexWriter = new IndexWriter(directory,analyzer,maxFieldLength);
        indexWriter.addDocument(document);
        indexWriter.close();

    }

    @Test
    public void addAll() throws Exception{
        IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());

        Article article1 = new Article(1,"培训","传智是一家java培训机构");
        Document document1 = LuceneUtil.javabean2document(article1);
        indexWriter.addDocument(document1);

        Article article2 = new Article(2,"培训","传智是一家net培训机构");
        Document document2 = LuceneUtil.javabean2document(article2);
        indexWriter.addDocument(document2);

        Article article3 = new Article(3,"培训","传智是一家php培训机构");
        Document document3 = LuceneUtil.javabean2document(article3);
        indexWriter.addDocument(document3);


        Article article4 = new Article(4,"培训","传智是一家ios培训机构");
        Document document4 = LuceneUtil.javabean2document(article4);
        indexWriter.addDocument(document4);

        Article article5 = new Article(5,"培训","传智是一家ui培训机构");
        Document document5 = LuceneUtil.javabean2document(article5);
        indexWriter.addDocument(document5);

        Article article6 = new Article(6,"培训","传智是一家c++培训机构");
        Document document6 = LuceneUtil.javabean2document(article6);
        indexWriter.addDocument(document6);

        Article article7 = new Article(7,"培训","传智是一家c培训机构");
        Document document7 = LuceneUtil.javabean2document(article7);
        indexWriter.addDocument(document7);

        indexWriter.close();
    }

    @Test
    public void update() throws Exception{
        Article newArticle7 = new Article(7,"培训","传智是一家s培训机构");
        Document document = LuceneUtil.javabean2document(newArticle7);

        IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
        indexWriter.updateDocument(new Term("id","7"),document);

        indexWriter.close();
    }


    @Test
    public void delete() throws Exception{
        IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
        indexWriter.deleteDocuments(new Term("id","7"));
        indexWriter.close();
    }

    public void deleteAll() throws Exception{
        IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
        indexWriter.deleteAll();
        indexWriter.close();
    }



    @Test
    public void findAll() throws Exception{
        String keywords = "培训";
        List<Article> articleList = new ArrayList<Article>();
        //将document对象写入Lucene索引库
        Directory directory = FSDirectory.open(new File("E:/indexDBDBDB"));   //索引库存放硬盘的位置
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);  //采用什么策略来拆分文字 参数为采用指定的版本

        //创建indexSwarcher流对象
        IndexSearcher indexSearcher = new IndexSearcher(directory); //寻找词库的位置
        //创建查询对象
        /*对应的参数意思是: 1版本号, 2搜索的范围只再document中的Xcontent文本类搜索,3用的是那种解析器s*/
        QueryParser queryParser = new QueryParser(Version.LUCENE_30,"content",analyzer);
        Query query = queryParser.parse(keywords);

        /*如果搜索出来的关键数据过多 只取出来Max_Record对应值的个数*/
        int MAX_RECORD = 100;
        TopDocs topDocs = indexSearcher.search(query, MAX_RECORD);

        for(int i = 0; i<topDocs.scoreDocs.length; i++){
            //取出每一个编号
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            int no = scoreDoc.doc;

            //封装到doc中
            Document document = indexSearcher.doc(no);
            String id = document.get("id");
            String title = document.get("title");
            String content = document.get("content");

            //封装到Artilce bean对象中
            Article article = new Article(Integer.parseInt(id),title,content);
            articleList.add(article);
        }
        //遍历搜索到内容集合
        for(Article a :articleList){
            System.out.println(a);
        }
    }
}

 

 

 

LuceneUtil

Lucene的快速入门
package cn.itcast.javaee.lucene.util;

import java.io.File;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
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.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * 工具类
 * @author AdminTC
 */
public class LuceneUtil {
    
    private static Directory directory;
    private static Version version;
    private static Analyzer analyzer;
    private static MaxFieldLength maxFieldLength;
    
    static{
        try {
            directory =  FSDirectory.open(new File("E:/IndexDBDBDB"));
            version = Version.LUCENE_30;
            analyzer = new StandardAnalyzer(version);
            maxFieldLength = MaxFieldLength.LIMITED;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    public static Directory getDirectory() {
        return directory;
    }

    public static Version getVersion() {
        return version;
    }

    public static Analyzer getAnalyzer() {
        return analyzer;
    }

    public static MaxFieldLength getMaxFieldLength() {
        return maxFieldLength;
    }

    //不让外界new该帮助类
    private LuceneUtil(){}    
    
    //将JavaBean转成Document对象
    public static Document javabean2document(Object obj) throws Exception{
        //创建Docuemnt对象
        Document document = new Document();
        //获取obj引用的对象字节码
        Class clazz = obj.getClass();
        //通过对象字节码获取私有的属性
        java.lang.reflect.Field[] reflectFields = clazz.getDeclaredFields();
        //迭代
        for(java.lang.reflect.Field reflectField : reflectFields){
            //强力反射
            reflectField.setAccessible(true);
            //获取属性名,id/title/content
            String name = reflectField.getName();
            //人工拼接方法名
            String methodName = "get" + name.substring(0,1).toUpperCase()+name.substring(1);
            //获取方法,例如:getId()/getTitle()/getContent()
            Method method = clazz.getMethod(methodName,null);
            //执行方法
            String value = method.invoke(obj,null).toString();
            //加入到Document对象中去,这时javabean的属性与document对象的属性相同
            document.add(new Field(name,value,Store.YES,Index.ANALYZED));
        }
        //返回document对象
        return document;
    }
    
    //将Document对象转成JavaBean对象
    public static Object document2javabean(Document document,Class clazz) throws Exception{
        Object obj = clazz.newInstance();
        java.lang.reflect.Field[] reflectFields = clazz.getDeclaredFields();
        for(java.lang.reflect.Field reflectField : reflectFields){
            reflectField.setAccessible(true);
            String name = reflectField.getName();//id/title/content
            String value = document.get(name);//1/培训/传智是一家培训机构
            BeanUtils.setProperty(obj,name,value);//封装javabean对应的属性中去,通过setXxx()方法
        }
        return obj;
    }

    
}
View Code

 

Lucene的快速入门

上一篇:appium原理及api


下一篇:第14天: Web 开发 Flask 介绍