zoukankan      html  css  js  c++  java
  • *lucene索引_创建_域选项

    【索引建立步骤】

    【创建Directory】

     

    【创建writer】

    【创建文档并添加索引】

    文档和域的概念很重要

           文档相当于表中的每一条记录,域相当于表中的每一个字段。

    【查询索引的基本信息】

    使用IndexReader进行查询。

    【实践】

    附:

    IndexUtil.java:

     1 package cn.hk.index;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 
     6 import org.apache.lucene.analysis.standard.StandardAnalyzer;
     7 import org.apache.lucene.document.Document;
     8 import org.apache.lucene.document.Field;
     9 import org.apache.lucene.index.CorruptIndexException;
    10 import org.apache.lucene.index.IndexReader;
    11 import org.apache.lucene.index.IndexWriter;
    12 import org.apache.lucene.index.IndexWriterConfig;
    13 import org.apache.lucene.store.Directory;
    14 import org.apache.lucene.store.FSDirectory;
    15 import org.apache.lucene.store.LockObtainFailedException;
    16 import org.apache.lucene.util.Version;
    17 
    18 public class IndexUtil {
    19     private String[] ids = {"1","2","3","4","5","6"};
    20     private String[] emails = {"aa@hk.arg","bb@hk.org","cc@hk.arg",
    21                                "dd@hk.org","ee@hk.org","ff@hk.org"};
    22     private String[] content = {
    23             "welcome to visited the space","hello boy","my name is aa","i like football",
    24             "I like football and I like Basketball too","I like movie and swim"
    25     };
    26     private int[] attachs = {2,3,1,4,5,5};
    27     private String[] names = {"zhangsan","lisi","john","mike","jetty","jake"};
    28     
    29     private Directory directory = null;
    30     
    31     public IndexUtil(){
    32         try {
    33             directory = FSDirectory.open(new File("d://lucene/index02"));
    34         } catch (IOException e) {
    35             e.printStackTrace();
    36         }
    37     }
    38     
    39     public void query(){
    40         try {
    41             IndexReader reader = IndexReader.open(directory);
    42             //通过reader可以获取文档的数量
    43             System.out.println("numDocs:" + reader.numDocs());
    44             System.out.println("maxDocs" + reader.maxDoc());
    45         } catch (CorruptIndexException e) {
    46             
    47             e.printStackTrace();
    48         } catch (IOException e) {
    49             
    50             e.printStackTrace();
    51         }
    52     }
    53     
    54     
    55     public void index(){
    56         IndexWriter writer = null;
    57         try {
    58             writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
    59             Document doc = null;
    60             for(int i=0;i<ids.length;i++){
    61                 doc = new Document();
    62                 doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
    63                 doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
    64                 doc.add(new Field("content",content[i],Field.Store.NO,Field.Index.ANALYZED));
    65                 doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
    66                 writer.addDocument(doc);
    67             }
    68         } catch (CorruptIndexException e) {
    69             e.printStackTrace();
    70         } catch (LockObtainFailedException e) {
    71             e.printStackTrace();
    72         } catch (IOException e) {
    73             e.printStackTrace();
    74         }finally{
    75                 if(writer != null)
    76                     try {
    77                         writer.close();
    78                     } catch (CorruptIndexException e) {
    79                         
    80                         e.printStackTrace();
    81                     } catch (IOException e) {
    82                         
    83                         e.printStackTrace();
    84                     }
    85             
    86         }
    87     }
    88 
    89 }

    TestIndex.java:

     1 package cn.hk.test;
     2 
     3 import org.junit.Test;
     4 
     5 import cn.hk.index.IndexUtil;
     6 
     7 public class TestIndex {
     8     
     9     @Test
    10     public void testIndex(){
    11         IndexUtil iu = new IndexUtil();
    12         iu.index();
    13     }
    14     
    15     @Test
    16     public void testQuery(){
    17         IndexUtil iu = new IndexUtil();
    18         iu.query();
    19     }
    20 }

    index()运行结果:

    query()运行结果

  • 相关阅读:
    Python协程
    Python3常用标准库
    温故而知新--day3
    温故而知新--day2
    解决 WPF 绑定集合后数据变动界面却不更新的问题
    WPF 消息框 TextBox 绑定新数据时让光标和滚动条跳到最下面
    C# 枚举转列表
    真・WPF 按钮拖动和调整大小
    使用 GB28181.Solution + ZLMediaKit + MediaServerUI 进行摄像头推流和播放
    将 .NET Framework 项目转换为 .NET Standard 项目
  • 原文地址:https://www.cnblogs.com/zhzcode/p/9883955.html
Copyright © 2011-2022 走看看