lucene4.6索引创建和搜索例子

最近无事,闲来看了看lucene4.6,也来学习一下,写了些小例子总的来说lucene全文检索步骤大体上有两方面:索引过程和搜索过程,具体如下:

1.索引过程

<1>创建IndexWriter,它的作用是用来写索引文件

可以将IndexWriter看做是一个特定类型的数据库,用来存放各种表,可以将Document看做是一张张的表

IndexWriter iw=new IndexWriter(Directory dire, IndexWriterConfigiwc);可见,该方法有两个参数,第一个参数为索引存放位置,参数类型为Directory,第二个参数为IndexWriter的配置类,

@1.Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));通过Directory的创建指定索引存放位置

@2.IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer);通过IndexWriterConfig的创建指定索引版本和语言词汇分析器

@3.Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);通过Analyzer 的创建指定索引语言词汇的分析器

<2>创建Document,代表要索引的文档,

可以将Document看做是数据库中的一张张的表,而每个field都是表中的一个colum用来存放各种类型的信息,如标题、作者、时间等等

@1.Document doc=new Document();通过创建Document指定要索引的文档

@2.doc.add(new TextField("content", content, Store.YES));向Document文档中添加Field信息,不同类型的信息用不同类型的Field来表示

<3>将Document添加到IndexWriter中并且提交

表(Document)创建好之后,当然要添加到数据库(IndexWriter)中,同时commit

iw.addDocument(doc);通过该方法添加

2.搜索过程

<!>首先创建语言词汇解析器Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);通过该方法指定语言词汇解析器

<2>指定搜索目录Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));通过该方法指定搜索目录

<3>创建IndexReader将搜索目录读取到内存,IndexReader ir=DirectoryReader.open(dire);通过该方法创建IndexWriter并为IndexWriter指定搜索目录

<4>创建IndexSearcher准备搜索IndexSearcher is=new IndexSearcher(ir);通过该方法创建IndexSearcher

<5>创建QueryParser对查询语句进行语法分析QueryParser parser = new QueryParser(Version.LUCENE_46, "path", analyzer);通过该方法创建

<6>创建Query生成查询语法树Query query = parser.parse("test");通过调用QueryParser 的parse("");方法来生成Query以及查询语法树

<7>获取搜索结果TopDocs td=is.search(query, 1000);ScoreDoc[] sds =td.scoreDocs;通过该方法即可获取搜索结果

<8>对搜索结果遍历操作

3.以下是个人测试例子

public class Constants {public final static String INDEX_FILE_PATH = "F:\\lucene\\test"; //索引的文件的存放路径 测试时可以在本目录下自行建一些文档,内容自行编辑即可public final static String INDEX_STORE_PATH = "F:\\lucene\\index"; //索引的存放位置}package com.lucene.jl;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;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.TextField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.queryparser.classic.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;public class LuceneIndex {/** * 创建索引 * @param analyzer * @throws Exception */public static void createIndex(Analyzer analyzer) throws Exception{Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer);IndexWriter iw=new IndexWriter(dire, iwc);LuceneIndex.addDoc(iw);iw.close();}/** * 动态添加Document * @param iw * @throws Exception */public static void addDoc(IndexWriter iw) throws Exception{File[] files=new File(Constants.INDEX_FILE_PATH).listFiles();for (File file : files) {Document doc=new Document();String content=LuceneIndex.getContent(file);String name=file.getName();String path=file.getAbsolutePath();doc.add(new TextField("content", content, Store.YES));doc.add(new TextField("name", name, Store.YES));doc.add(new TextField("path", path,Store.YES));System.out.println(name+"==="+content+"==="+path);iw.addDocument(doc);iw.commit();}}/** * 获取文本内容 * @param file * @return * @throws Exception */@SuppressWarnings("resource")public static String getContent(File file) throws Exception{FileInputStream fis=new FileInputStream(file);InputStreamReader isr=new InputStreamReader(fis);BufferedReader br=new BufferedReader(isr);StringBuffer sb=new StringBuffer();String line=br.readLine();while(line!=null){sb.append(line+"\n");line=null;}return sb.toString();}/** * 搜索 * @param query * @throws Exception */private static void search(Query query) throws Exception {Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));IndexReader ir=DirectoryReader.open(dire);IndexSearcher is=new IndexSearcher(ir);TopDocs td=is.search(query, 1000);System.out.println("共为您查找到"+td.totalHits+"条结果");ScoreDoc[] sds =td.scoreDocs;for (ScoreDoc sd : sds) {Document d = is.doc(sd.doc);System.out.println(d.get("path") + ":["+d.get("path")+"]"); }}public static void main(String[] args) throws Exception, Exception {Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);LuceneIndex.createIndex(analyzer);QueryParser parser = new QueryParser(Version.LUCENE_46, "content", analyzer); Query query = parser.parse("人");LuceneIndex.search(query);}}4.磁盘文件检索如上述,对于数据库的检索页应该是差不多,应该也是先从数据库查出数据,然后对查出的数据做索引然后搜索,大致一样吧5.以上都是个人拙见,初学lucene,肯定有想法欠妥的地方,有错误之处,请高手多多指正

,三亚呀——赴一个蓝天碧海。

lucene4.6索引创建和搜索例子

相关文章:

你感兴趣的文章:

标签云: