用Lucene简单实现全文检索

简单实用Callable实现了Lucene简单的实现了全文检索功能,未考虑其他,只实现异步查找,如有不对请大牛指出~

 

pom文件:

     <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queries -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queries</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>4.0.0</version>
        </dependency>

 

Java代码:

package com.example.demo;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Created by Administrator on 2017/8/21.
 *
 * zhaofei
 *
 * Lucene  4.0.0
 */
public class Lucene {

    private static final StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
    private static final IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);



    class ContextBean {
        private String id;
        private String context;

        public ContextBean (String id, String context){
            this.id = id;
            this.context = context;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getContext() {
            return context;
        }

        public void setContext(String context) {
            this.context = context;
        }
    }


    private void addDoc(IndexWriter w, String id, String context) throws IOException {
        Document doc = new Document();
        doc.add(new StringField("id", id, Field.Store.YES));
        doc.add(new TextField("context", context, Field.Store.YES));
        w.addDocument(doc);
    }

    private void addInfo(IndexWriter w, List<ContextBean> list) throws IOException {
        if (list != null && list.size() > 0) {
            for (ContextBean context : list) {
                addDoc(w, context.getId(), context.getContext());
            }
        }
        w.close();
    }

    class QueryInfoThread implements Callable {

        private String queryStr;
        private Integer context;
        private Vector<String> vetor;
        private List<ContextBean> contextBeanList;


        public QueryInfoThread(String queryStr, Integer context, Vector<String> vetor, List<ContextBean> contextBeanList){
            this.queryStr = queryStr;
            this.context = context;
            this.vetor = vetor;
            this.contextBeanList = contextBeanList;
        }

        @Override
        public Object call() throws Exception {
            if (context == null) {
                context = 10;
            }
            Directory index = new RAMDirectory();
            IndexWriter indexWriter = new IndexWriter(index, config);

            // add info
            addInfo(indexWriter, contextBeanList);

            Query q = new QueryParser(Version.LUCENE_40, "context", analyzer).parse(queryStr);
            IndexReader reader = IndexReader.open(index);
            IndexSearcher searcher = new IndexSearcher(reader);
            TopScoreDocCollector collector = TopScoreDocCollector.create(context, true);
            searcher.search(q, collector);
            ScoreDoc[] contextArray = collector.topDocs().scoreDocs;

            if (contextArray.length > 0) {
                if (contextArray.length > 0) {
                    for (int i = 0; i < contextArray.length; ++i) {
                        int docId = contextArray[i].doc;
                        Document d = searcher.doc(docId);
                        vetor.add(d.get("id"));
                    }
                }
            }
            return vetor;
        }
    }

    public List<ContextBean> getList() {
        List<ContextBean> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        list.add(new ContextBean("193398817","Lucenes in Action"));
        list.add(new ContextBean("55320055Z","Lucene for Dummies"));
        list.add(new ContextBean("55063554A","Managing Gigabytes"));
        list.add(new ContextBean("99020333X","The Art of Computer Science"));
        list.add(new ContextBean("985563877","中国人的"));
        return list;
    }

    public static void main(String[] args) throws Exception{
        Vector<String> vetor = new Vector();

        Lucene lucene = new Lucene();
        // 创建一个线程池
        ExecutorService pool = Executors.newFixedThreadPool(2);
        List<ContextBean> contextBeanList = new Lucene().getList();
        Callable callable = lucene.new QueryInfoThread("人", 10, vetor, contextBeanList);
        Future future = pool.submit(callable);
        vetor = (Vector) future.get();

        for (String str : vetor) {
            System.out.println(str);
        }
        // 关闭线程池
        pool.shutdown();
    }
}

 

posted @ 2017-08-25 13:03  红枫诺雨  阅读(201)  评论(0)    收藏  举报