Lucene demo演示搜索查询歌手,歌名,歌词

2024-10-09 08:48:11 浏览数 (1)

1.导入pom jar文件

代码语言:javascript复制
<dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>8.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>8.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>8.11.1</version>
        </dependency>

2. 

代码语言:javascript复制
LuceneService.java类
代码语言:javascript复制
import com.alibaba.fastjson.JSONObject;
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.TextField;
import org.apache.lucene.index.*;
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.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;

public class LuceneService {
    public static void main(String[] args) throws IOException, ParseException {
        String indexDir = "D://temp//lucence_data//products";
        //创建索引
//        createIndex(indexDir);

        //中文搜索不出来
        String jsonDoc = "{"songName":"hello world is chinese","songer":"liudehua hk","lyrics":"lyricslyricslyricslyrics 2022"}";
//        String jsonDoc = "{"aaa":"111"}";
        indexDoc(indexDir,jsonDoc,"chinese");

    }

    //创建索引
    private static void createIndex(String indexDir) throws IOException {
        //准备目录
        Directory dir  = FSDirectory.open(Paths.get(indexDir));
        //准备分词器
        Analyzer analyzer = new StandardAnalyzer();
        //配置
        IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
        //创建索引
        IndexWriter iw = new IndexWriter(dir,iwConfig);
        //关闭索引
        iw.close();
    }

    private static void indexDoc(String indexDir,String jsonDoc,String queryStr) throws IOException, ParseException {
        //准备目录
        Directory dir  = FSDirectory.open(Paths.get(indexDir));
        //准备分词器
        Analyzer analyzer = new StandardAnalyzer();
        //配置
        IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
        //创建索引
        IndexWriter iw = new IndexWriter(dir,iwConfig);
        //json转document对象
        Document doc = Json2Doc(jsonDoc);
        iw.addDocument(doc);


        //IndexReader实例
        IndexReader ir = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(ir);

        //查询索引
        QueryParser parser = new QueryParser("songName",analyzer);
        Query query = parser.parse(queryStr);

        TopDocs hits = searcher.search(query,10);
        String result = "结果=";
        for (ScoreDoc scoreDoc : hits.scoreDocs){
            //拿到文档
            Document rdoc = searcher.doc(scoreDoc.doc);
            List<IndexableField> fields = rdoc.getFields();
            for (IndexableField f : fields){
                result  = f.name()   ":" f.stringValue() ",rn";
            }
            System.out.println("result="   result);
        }
        System.out.println("总的查询结果result="   result);
        ir.close();

        //关闭索引
        iw.close();
    }

    /**
     *输出结果
     总的查询结果result=结果=songName:hello world is chinese,
     lyrics:lyricslyricslyricslyrics 2022,
     songer:liudehua hk,
     *
     */

    private static Document Json2Doc(String jsonDoc) {
        Document doc = new Document();
        JSONObject jsonObject = JSONObject.parseObject(jsonDoc);
        Set<String> keys = jsonObject.keySet();
        for (String key : keys){
            doc.add(new TextField(key, jsonObject.getString(key), Field.Store.YES ));
        }
        return doc;
    }



}

 3.本地目录生成的文件

0 人点赞