• lucene demo代码


    package com.xl.lucene;

    import java.io.File;

    import org.apache.commons.io.FileUtils;
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.*;
    import org.apache.lucene.index.*;
    import org.apache.lucene.search.*;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    import org.junit.Test;

    /**
    * 创建索引
    * 查询索引
    */
    public class FirstLucene {

    @Test
    public void testIndex() throws Exception {
    /*第一步创建一个Java工程导入Java包
    * 第二部创建要给index writer对象
    * 1.指定索引库的存放位置Directory对象
    * 2.指定要给分析器,对文档内容进行分析
    * 第三步创建document对象
    * 第四步创建field对象,将field添加到document对象中
    * 第五步使用index writer对象将document对象写入索引库,此过程进行索引创建.并将索引和document对象写入索引库
    * 第六步关闭index writer对象
    * */
    // 第二步
    Directory directory = FSDirectory.open(new File("E:\WorkSpace\allOtherItem\luceneresult"));
    // Directory directory = new RAMDirectory();//保存到内存中
    Analyzer analyzer = new StandardAnalyzer();//官方推荐
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, analyzer);
    IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
    // 第三步

    File f = new File("E:\WorkSpace\allOtherItem\luceneIndex");
    File[] listFiles = f.listFiles();
    for (File file : listFiles) {
    Document document = new Document();
    String fileName = file.getName();
    Field fieldNameField = new TextField("fileName", fileName, Field.Store.YES);

    long file_size = FileUtils.sizeOf(file);
    LongField fileSizeField = new LongField("fileSize", file_size, Field.Store.YES);

    String filePath = file.getPath();
    Field fieldPathField = new StoredField("filePath", filePath);

    String fileContent = FileUtils.readFileToString(file);
    Field fileContentField = new TextField("fileContent", fileContent, Field.Store.NO);

    document.add(fieldNameField);
    document.add(fileSizeField);
    document.add(fieldPathField);
    document.add(fileContentField);
    // 第四步
    indexWriter.addDocument(document);
    }
    indexWriter.close();

    }

    // 搜索索引
    @Test
    public void testSearch() throws Exception {
    //第一步创建一个Direction对象,也就是索引库存放位置
    Directory directory = FSDirectory.open(new File("E:\WorkSpace\allOtherItem\luceneresult"));
    // 第二步创建一个indexReader对象,需要指定Directory对象 流对象
    IndexReader indexReader = DirectoryReader.open(directory);
    // 第三步创建一个indexsearcher对象,需要指定Index Reader对象
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    // 第四步创建一个Term Query对象,指定查询的域和查询的关键词
    Query query = new TermQuery(new Term("fileName", "java.txt"));
    // 第五步执行查询
    TopDocs topDocs = indexSearcher.search(query, 2);

    // 第六步返回查询结果.遍历查询结果并输出
    ScoreDoc[] scoreDocs = topDocs.scoreDocs;
    if (scoreDocs == null) {
    System.out.println("读取失败");
    } else {
    for (ScoreDoc scoreDoc : scoreDocs) {
    int doc = scoreDoc.doc;
    Document document = indexSearcher.doc(doc);
    //文件名称
    String fileName = document.get("fileName");
    //文件内容
    String fileContent = document.get("fileContent");
    //文件大小
    String fileSize = document.get("fileSize");
    //文件路径
    String filePath = document.get("filePath");
    System.out.println(fileName);
    System.out.println(fileContent);
    System.out.println(fileSize);
    System.out.println(filePath);
    System.out.println("----------------------------------------------------------");

    }
    }

    // 第七步关闭Index Reader对象
    System.out.println("结束");
    indexReader.close();
    }
    }
  • 相关阅读:
    关于postgresql——常用操作指令
    linux 下查看机器是cpu是几核的
    ASP.NET跨平台实践:无需安装Mono的Jexus“独立版”
    .NET平台开源项目速览(4).NET文档生成工具ADB及使用
    Hadoop学习---安装部署
    c# 模拟表单提交,post form 上传文件、大数据内容
    半小时学会上传本地项目到github
    统计网卡TX(发送)RX(接受)流量脚本
    mysql mysqldump只导出表结构或只导出数据的实现方法
    psutil--跨平台的进程管理
  • 原文地址:https://www.cnblogs.com/lovetl/p/11896849.html
Copyright © 2020-2023  润新知