Lucene初学——1. 创建索引,添加目录,文档

1.maven依赖

<dependencies>
  	<!-- lucene-queryparser -->
	<dependency>
	    <groupId>org.apache.lucene</groupId>
	    <artifactId>lucene-queryparser</artifactId>
	    <version>${lucene-version}</version>
	</dependency>
	
	<!-- lucene-highlighter -->
	<dependency>
	    <groupId>org.apache.lucene</groupId>
	    <artifactId>lucene-highlighter</artifactId>
	    <version>${lucene-version}</version>
	</dependency>
	
  	<!-- lucene-analyzers-smartcn -->
	<dependency>
	    <groupId>org.apache.lucene</groupId>
	    <artifactId>lucene-analyzers-smartcn</artifactId>
	    <version>${lucene-version}</version>
	</dependency>
	  	
  </dependencies>

这些依赖中已经包含对核心包等必要jar的依赖

2.demo

public static void main(String[] args) {
		// Analyzer类负责分析一个文件,并从将被索引的文本获取令牌/字。不加分析完成后,IndexWriter不能创建索引。
		Analyzer analyzer = new StandardAnalyzer();
		// 此类充当创造/在索引过程中更新指标的核心组成部分
		IndexWriterConfig config = new IndexWriterConfig(analyzer);
		// 模式:创建或追加
		config.setOpenMode(OpenMode.CREATE_OR_APPEND);
		// Directory类表示索引的存储位置,并通常是文件的列表。这些文件被称为索引文件。索引文件通常创建一次,然后用于读操作或可以被删除。
		Directory directory = null;
		IndexWriter writer = null;
		try {
			// 创建
			directory = FSDirectory.open(Paths.get("E:\\document\\【进阶之路】\\技术\\lucene\\index"));
			writer = new IndexWriter(directory, config);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		// 文档表示一个虚拟文档与字段,其中字段是可包含在物理文档的内容,它的元数据等的对象。Analyzer只能理解文档。 
		Document doc1 = new Document();
		/*
		 * YES:
		 * Store the original field value in the index. 
		 * This is useful for short texts like a document's title which should be displayed with the results. 
		 * The value is stored in its original form, i.e. no analyzer is used before it is stored
		 * NO:
		 * Do not store the field value in the index.
		 */
		doc1.add(new StringField("id", "a", Store.YES));
		doc1.add(new TextField("content", "lucene学习案例", Store.YES));
		// 数值类型的添加需要对应的Point类加上StoredField
		doc1.add(new IntPoint("num", 1));
		doc1.add(new StoredField("num", 1));
		
		try {
			writer.addDocument(doc1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Document doc2 = new Document();
		doc2.add(new StringField("id", "c", Store.YES));
		doc2.add(new TextField("content", "Lucene案例开发", Store.YES));
		doc2.add(new IntPoint("num", 2));
		doc2.add(new StoredField("num", 2));
		
		try {
			writer.addDocument(doc2);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			writer.commit();
			// 关闭资源
			writer.close();
			directory.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

3.导包

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

4.查看,结果查询,使用luke工具


附luke地址:

https://github.com/DmitryKey/luke/releases

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章