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

  • 相关阅读:
    学习网页栅格系统的几篇好文
    [转载]iis6配置使用页面Gzip压缩提速
    img标签的src=""会引起的Page_Load多次执行
    基于sliverlight + wcf的web 文字版IM 示例
    Enterprise Library 4.1学习笔记8缓存应用程序块之FileDependency
    windows 2008上启用防火墙后sqlserver 2005经常出现连接超时的解决办法
    负载均衡环境下的web服务器处理
    Ado.Net连接池的速度测试
    [转载]网页栅格系统研究(1):960的秘密
    css基础:把所有背景图都集成在一张图片上,减少图片服务器请求次数
  • 原文地址:https://www.cnblogs.com/i-hard-working/p/9568920.html
Copyright © 2011-2022 走看看