• 索引创建和搜索


    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"));
            }
        }
    }
    

  • 相关阅读:
    使用一行Python代码从图像读取文本
    FastAI 简介
    OpenCV-Python 轮廓:更多属性 | 二十四
    Numpy和OpenCV中的图像几何变换
    从云计算到边缘计算
    OpenCV-Python 轮廓属性 | 二十三
    c# GetType()和typeof()的区别
    C# 子类父类方法同名,三种处理方式
    C# string 与 String的区别
    decimal
  • 原文地址:https://www.cnblogs.com/i-hard-working/p/9568920.html
Copyright © 2020-2023  润新知