Lucene 6.5 demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36187372/article/details/71242994

根据Lucene 6.5及JDK 1.8最新版本,写的Demo

package com.lyl.lucene.lucene;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.IndexableFieldType;
import org.apache.lucene.queryparser.classic.ParseException;
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.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;

public class LuceneTest {

    public void createIndex(){
        Path path = FileSystems.getDefault().getPath("F://lucene/index");
        try {
            //1:指定索引库的地址
            FSDirectory directory = FSDirectory.open(path);
            //2 创建分词器,标准分词器
            StandardAnalyzer analyzer = new StandardAnalyzer();
            //3:创建IndexWriterConfig
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            //4:创建IndexWriter
            IndexWriter writer = new IndexWriter(directory, config);
            writer.deleteAll();//清除以前的index
            File file = new File("F://lucene/data");

            File[] listFiles = file.listFiles();

            for (File f : listFiles) {
                Document document = new Document();
                document.add(new TextField("content",new FileReader(f)));
                document.add(new TextField("name", f.getName(), Field.Store.YES));
                document.add(new TextField("path",f.getAbsolutePath(),Field.Store.YES));

                writer.addDocument(document);
            }

            writer.close();

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

    public void doSearch(){
        Path path = FileSystems.getDefault().getPath("F://lucene/index");
        try {
            //1:指定索引库的地址
            FSDirectory directory = FSDirectory.open(path);
            // 2、创建IndexReader  
            DirectoryReader reader = DirectoryReader.open(directory);

            System.out.println(reader.getVersion());
            System.out.println(reader.maxDoc());
            System.out.println(reader.numDocs());
            // 3、根据IndexReader创建IndexSearch 
            IndexSearcher searcher = new IndexSearcher(reader);
            StandardAnalyzer analyzer = new StandardAnalyzer();
            // 使用QueryParser搜索时,需要指定分词器,搜索时的分词器要和索引时的分词器一致
            // 第一个参数:默认搜索的域的名称
            QueryParser parser = new QueryParser("content", analyzer);
            // 4、创建搜索的Query 
            // 创建Query表示搜索域为content包含hello的文档  
            Query query = parser.parse("hello");
            // 5、根据searcher搜索并且返回TopDocs
            TopDocs topDocs = searcher.search(query, 10);
             // 根据查询条件匹配出的记录总数
            int count = topDocs.totalHits;
            System.out.println("匹配出的记录总数:" + count);

            //6、根据TopDocs获取ScoreDoc对象  
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;

            for (ScoreDoc scoreDoc : scoreDocs) {
                int docID = scoreDoc.doc;
                // 7、根据searcher和ScoreDoc对象获取具体的Document对象
                Document doc = searcher.doc(docID);
                 // 8、根据Document对象获取需要的值  
                System.out.println(doc.get("content"));
                System.out.println(doc.get("name"));
                System.out.println(doc.get("path"));
            }

            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页