zoukankan      html  css  js  c++  java
  • 创建索引之代码开发

    【创建索引库】

    使用indexwriter对象创建索引。

    【实现步骤】

    (1)创建一个java工程,并导入jar包。

    (2)创建一个indexwriter对象。

            1)指定索引库的存放位置Directory对象。

            2)指定一个分析器,对文档内容进行分析。

    (3)创建Document对象

    (4)创建filed对象,将field添加到Document对象中。

    (5)使用indexwriter对象将Document对象写入到索引库,此过程进行索引创建,并将索引和Document对象写入索引库。

    (6)关闭IndexWriter对象。

    FirstLucene.java:

      1 package com.hk.lucene;
      2 
      3 import static org.junit.Assert.*;
      4 import java.io.File;
      5 import org.apache.commons.io.FileUtils;
      6 import org.apache.lucene.analysis.Analyzer;
      7 import org.apache.lucene.analysis.TokenStream;
      8 import org.apache.lucene.analysis.cjk.CJKAnalyzer;
      9 import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
     10 import org.apache.lucene.analysis.standard.StandardAnalyzer;
     11 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
     12 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
     13 import org.apache.lucene.document.Document;
     14 import org.apache.lucene.document.Field;
     15 import org.apache.lucene.document.Field.Store;
     16 import org.apache.lucene.document.LongField;
     17 import org.apache.lucene.document.StoredField;
     18 import org.apache.lucene.document.TextField;
     19 import org.apache.lucene.index.DirectoryReader;
     20 import org.apache.lucene.index.IndexReader;
     21 import org.apache.lucene.index.IndexWriter;
     22 import org.apache.lucene.index.IndexWriterConfig;
     23 import org.apache.lucene.index.Term;
     24 import org.apache.lucene.search.IndexSearcher;
     25 import org.apache.lucene.search.Query;
     26 import org.apache.lucene.search.ScoreDoc;
     27 import org.apache.lucene.search.TermQuery;
     28 import org.apache.lucene.search.TopDocs;
     29 import org.apache.lucene.store.Directory;
     30 import org.apache.lucene.store.FSDirectory;
     31 import org.apache.lucene.store.RAMDirectory;
     32 import org.apache.lucene.util.Version;
     33 import org.junit.Test;
     34 import org.wltea.analyzer.lucene.IKAnalyzer;
     35 
     36 public class FirstLucene {
     37 
     38     // 创建索引
     39     @Test
     40     public void testIndex() throws Exception {
     41         // 第一步:创建一个java工程,并导入jar包。
     42         // 第二步:创建一个indexwriter对象。
     43         Directory directory = FSDirectory.open(new File("D:\temp\index"));
     44         // Directory directory = new RAMDirectory();//保存索引到内存中 (内存索引库)
     45         //Analyzer analyzer = new StandardAnalyzer();// 官方推荐
     46         Analyzer analyzer = new IKAnalyzer();// 官方推荐
     47         IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
     48         IndexWriter indexWriter = new IndexWriter(directory, config);
     49         // 1)指定索引库的存放位置Directory对象
     50         // 2)指定一个分析器,对文档内容进行分析。
     51         // 第三步:创建field对象,将field添加到document对象中。
     52         File f = new File("D:\Lucene&solr\searchsource");
     53         File[] listFiles = f.listFiles();
     54         for (File file : listFiles) {
     55             // 第三步:创建document对象。
     56             Document document = new Document();
     57             // 文件名称
     58             String file_name = file.getName();
     59             Field fileNameField = new TextField("fileName", file_name, Store.YES);
     60             // 文件大小
     61             long file_size = FileUtils.sizeOf(file);
     62             Field fileSizeField = new LongField("fileSize", file_size, Store.YES);
     63             // 文件路径
     64             String file_path = file.getPath();
     65             Field filePathField = new StoredField("filePath", file_path);
     66             // 文件内容
     67             String file_content = FileUtils.readFileToString(file);
     68             Field fileContentField = new TextField("fileContent", file_content, Store.NO);
     69 
     70             document.add(fileNameField);
     71             document.add(fileSizeField);
     72             document.add(filePathField);
     73             document.add(fileContentField);
     74             // 第四步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。
     75             indexWriter.addDocument(document);
     76 
     77         }
     78         // 第五步:关闭IndexWriter对象。
     79         indexWriter.close();
     80     }

    运行结果:

    在D: empindex下:

    每接触一个新领域,我就像一块掉进水里的海绵,四面八方的养分都让我不断充实。O(∩_∩)O~
  • 相关阅读:
    PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket 'MySQL' (2)
    Nginx 开启PATHINFO支持ThinkPHP框架实例
    《征服 C 指针》笔记6:练习——挑战那些复杂的声明
    《征服 C 指针》摘录4:函数 与 指针
    《征服 C 指针》摘录5:函数形参 和 空的下标运算符[]
    《征服 C 指针》摘录3:数组 与 指针
    《征服 C 指针》摘录2:C变量的 作用域 和 生命周期(存储期)
    《征服 C 指针》摘录1:什么是空指针?区分 NULL、0 和 ''
    自定义 array_map() 对应的递归函数 array_map_recursive()
    【C语言入门教程】7.5 枚举
  • 原文地址:https://www.cnblogs.com/zhzcode/p/9784241.html
Copyright © 2011-2022 走看看