zoukankan      html  css  js  c++  java
  • lucene学习-创建索引

       本文的lucene是基于lucene3.5版本.

       使用lucene实现搜索引擎开发,核心的部分是建立索引和搜索。本节主要是记录创建索引部分的内容。

       创建的索引结构如图所示。

       

    创建索引的步骤分为以下几个步骤:

    1、建立索引器IndexWriter

    2、创建文档对象Document

    3、建立信息对象字段Field

    4、将Field对象添加到Document

    5、将Document对象添加到IndexWriter对象中

    下面简要介绍几个核心对象。

    (1)、创建IndexWriter对象。

    IndexWriter writer=new IndexWriter(directory, iwc)。

    directory是创建的索引要保存的路径,如果要保存在硬盘中则使用Directory directory = FSDirectory.open(new File(path))创建一个directory对象。

    如果要保存在内存中则使用:RAMDirectory directory=new RAMDirectory()创建一个directory对象。

    (2)、创建Document对象。

    Document doc =new Document();创建了一个不含有任何Field的空Document,如果要要Field添加到Document中,则使用add(Field)方法即可实现。

    doc.add(field)。

    (3)、创建Field对象。

    Field field=new Field(Field名称,Field内容,存储方式,索引方式);

    存储方式分为3种:1、完全存储(Field.Store.YES);2、不存储(Field.Store.NO);3、压缩存储(Field.Store.COMPRESS)。

    索引方式分为4种:1、不索引(Field.Index.NO);2、 Field.Index.ANALYZED ;3、 Field.Index.NOT_ANALYZED;4、Field.Index.NOT_ANALYZED_NO_NORMS

    创建一个简单的索引程序代码如下所示:

    public void Index() {
    		String[] ids = { "1", "2", "3", "4" };
    		String[] names = { "aa", "bb", "cc", "dd" };
    		String[] contents = {
    				"Using AbstractJExcelView to export data to Excel file via JExcelAPI library",
    				"Using AbstractPdfView to export data to Pdf file via Bruno Lowagie’s iText library. ",
    				"Example to integrate Log4j into the Spring MVC application. ",
    				"Using Hibernate validator (JSR303 implementation) to validate bean in Spring MVC. " };
    		IndexWriter writer = null;
    		try {
    			Directory directory = FSDirectory.open(new File(path));
    			// RAMDirectory directory=new RAMDirectory();
    			IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
    					new StandardAnalyzer(Version.LUCENE_35));
    			writer = new IndexWriter(directory, iwc);
    			Document doc = null;
    			for (int i = 0; i < ids.length; i++) {
    				doc = new Document();
    				doc.add(new Field("id", ids[i], Field.Store.YES,
    						Field.Index.NOT_ANALYZED_NO_NORMS));
    				doc.add(new Field("name", names[i], Field.Store.YES,
    						Field.Index.NOT_ANALYZED_NO_NORMS));
    				doc.add(new Field("contents", contents[i], Field.Store.YES,
    						Field.Index.ANALYZED));
    				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    				doc.add(new Field("date", sdf.format(new Date()),
    						Field.Store.YES, Field.Index.NOT_ANALYZED));
    				// Field.Index.ANALYZED;
    
    				writer.addDocument(doc);
    				writer.commit();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			if (writer != null) {
    				try {
    					writer.close();
    				} catch (CorruptIndexException e) {
    					e.printStackTrace();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    
  • 相关阅读:
    .net测试篇之Moq行为配置
    .net测试篇之Moq框架简单使用
    .net测试篇之测试神器Autofixture在几个复杂场景下的使用示例以及与Moq结合
    .net测试篇之测试神器Autofixture Generator使用与自定义builder
    .net测试篇之测试神器Autofixture基本配置一
    .net测试篇之单元测试/集成测试神器Autofixture
    .netcore持续集成测试篇之web项目验收测试
    .netcore持续集成测试篇之 .net core 2.1项目集成测试
    .netcore持续集成测试篇之MVC层单元测试
    .netcore持续集成测试篇之测试方法改造
  • 原文地址:https://www.cnblogs.com/gyouxu/p/3767745.html
Copyright © 2011-2022 走看看