zoukankan      html  css  js  c++  java
  • 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();
    }
    }
  • 相关阅读:
    【LeetCode】- Valid Palindrome(右回文)
    高榕资本宾悦:未使用的企业家Testin云测试服务类故障
    2015第17周三专注
    2015第17周二
    2015第17周一
    2015第16周日
    2015第16周六学习java建议
    2015第16周五
    2015第16周四自控力
    2015第16周三知道做到
  • 原文地址:https://www.cnblogs.com/lovetl/p/11896849.html
Copyright © 2011-2022 走看看