Lucene介绍
Lucene的定位是,核心索引和搜索的模块,并非完整的搜索程序。
搜索程序的组件
其中阴影部分的可以由Lucene完成。
索引组件
为何需要索引操作?
假设有一个3GB大小的文本,需要找出“程序”两个字,一种最简单的方式是顺序扫描,但这种扫描明显效率很低。所以就要选中一种高效的数据结构来存储,使得查询速度达到最快,这个过程就叫做索引操作
,它的输出就叫索引
。
索引的流程
搜索组件
搜索质量
搜索的流程
索引过程核心类
搜索过程核心类
实例程序
创建索引
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.FileReader;
import java.nio.file.Paths;
public class IndexTest {
//索引写入对象
private IndexWriter writer;
public static void main(String[] args) throws Exception {
String rootPath = "/searchengine/surfbird-search";
String indexDir = rootPath + "/index"; //1 指定目录创建索引
String dataDir = rootPath + "/data"; //2 对指定目录中的*.txt文件进行索引
long start = System.currentTimeMillis();
IndexTest indexer = new IndexTest(indexDir);
int numIndexed;