zoukankan      html  css  js  c++  java
  • 索引创建和搜索

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

  • 相关阅读:
    二进制拆分线段树
    2017 初赛PJ 错题解析
    线段树基操
    2015 初赛PJ 错题解析
    2016 初赛TG 错题解析
    拓扑排序找最大环最小环
    长乐集训合集
    java读取网页
    java下socket传图片
    java下socket传文件
  • 原文地址:https://www.cnblogs.com/i-hard-working/p/9568920.html
Copyright © 2011-2022 走看看