索引创建和搜索

Posted i-hard-working

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了索引创建和搜索相关的知识,希望对你有一定的参考价值。

D:luceneexample下有a.txt、b.txt和c.txt三个文件,分别写入“hello java”、“hello lucene”和“hello elasticsearch”。

package com.ant.jdk8.lucene7;

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.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

public class LuceneIndex {
    public static void main(String[] args) throws IOException, ParseException {
        LuceneIndex luceneIndex = new LuceneIndex();
        String indexPath = "D:\\lucene\\index";
        String filePath = "D:\\lucene\\example";
        luceneIndex.index(indexPath,filePath).search(indexPath);
    }

    public LuceneIndex index(String indexPath,String filePath) throws IOException {
        Directory directory = FSDirectory.open(Paths.get(indexPath));
        //Directory directory = new RAMDirectory();
        IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer());
        //iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        IndexWriter writer = new IndexWriter(directory,iwc);
        Document doc = null;
        DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get(filePath));
        for(Path path : paths){
            File file = path.toFile();
            doc = new Document();
            doc.add(new TextField("content",new FileReader(file)));
            doc.add(new StringField("filename",file.getName(), Field.Store.YES));
            doc.add(new StringField("path",file.getAbsolutePath(),Field.Store.YES));
            writer.addDocument(doc);
        }
        writer.close();
        return this;
    }

    public void search(String indexPath) throws IOException {
        Directory directory = FSDirectory.open(Paths.get(indexPath));
        DirectoryReader reader = DirectoryReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);
        Query query = new TermQuery(new Term("content","java"));
        TopDocs tds = searcher.search(query,10);
        ScoreDoc[] scoreDocs = tds.scoreDocs;
        for(ScoreDoc sd : scoreDocs){
            Document doc = searcher.doc(sd.doc);
            System.out.println(doc.get("filename")+"--->"+doc.get("path"));
        }
    }
}

技术分享图片

以上是关于索引创建和搜索的主要内容,如果未能解决你的问题,请参考以下文章

从搜索文档中查找最小片段的算法?

SQL Server中的全文搜索

如何创建片段以重复变量编号中的代码行

从ViewPager android替换片段

GitHub痛改代码搜索引擎,18小时给155亿个文档创建索引,背后技术原理已公开...

如何使用 DSE 搜索 6.8 创建搜索索引