`
geshenyi
  • 浏览: 98445 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

lucene第一步--1.简单的入门demo

阅读更多
Lucene不是一个完整的全文索引应用,而是是一个用Java写的全文索引引擎工具包,它可以方便的嵌入到各种应用中实现针对应用的全文索引/检索功能。

Lucene的作者:Lucene的贡献者Doug Cutting是一位资深全文索引/检索专家,曾经是V-Twin搜索引擎(Apple的Copland操作系统的成就之一)的主要开发者,后在Excite担任高级系统架构设计师,目前从事于一些INTERNET底层架构的研究。他贡献出的Lucene的目标是为各种中小型应用程序加入全文检索功能。

Lucene的发展历程:早先发布在作者自己的www.lucene.com,后来发布在SourceForge,2001年年底成为APACHE基金会jakarta的一个子项目:http://jakarta.apache.org/lucene/

已经有很多Java项目都使用了Lucene作为其后台的全文索引引擎,比较著名的有:

    * Jive:WEB论坛系统;
    * Eyebrows:邮件列表HTML归档/浏览/查询系统,本文的主要参考文档“TheLucene search engine: Powerful, flexible, and free”作者就是EyeBrows系统的主要开发者之一,而EyeBrows已经成为目前APACHE项目的主要邮件列表归档系统。
    * Cocoon:基于XML的web发布框架,全文检索部分使用了Lucene
    *

      Eclipse:基于Java的开放开发平台,帮助部分的全文索引使用了Lucene

对于中文用户来说,最关心的问题是其是否支持中文的全文检索。但通过后面对于Lucene的结构的介绍,你会了解到由于Lucene良好架构设计,对中文的支持只需对其语言词法分析接口进行扩展就能实现对中文检索的支持。
下面是一个入门demo

package demo.first;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.LockObtainFailedException;


public class FirstDemo {
	String path = "D://workspace//fwk//lucenedemo//firstLuceneIndex";
	public static void main(String[] args) {
		FirstDemo fd = new FirstDemo();
		//创建
		fd.createLuceneIndex();
		System.out.println("-------------------");
		//Hits.通过遍历Hits可获取返回的结果的Document,通过Document则可获取Field中的相关信息了。
		//测试
		Hits hits = fd.searchLuceneIndex("索*");
		for(int i = 0 ;i<hits.length();i++){
			try {
				System.out.println(hits.id(i));
				System.out.println(hits.doc(i).get("content"));
				System.out.println(hits.score(i));
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	/**
	 * 创建索引
	 */
	public void createLuceneIndex(){
		try {
			//IndexWriter,通过它建立相应的索引表,相当于数据库中的table
			//IndexWriter(索引路径, 分词器, 是否覆盖已存在)
			IndexWriter iwriter = new IndexWriter(path, new StandardAnalyzer(), true);
			//Document,有点类似数据库中table的一行记录
			Document doc1 = new Document();
			//Field,这个和数据库中的字段类似
			//Store {COMPRESS: 压缩保存。用于长文本或二进制数据,YES :保存,NO :不保存}
			//Index {NO :不 建索引,TOKENIZED :分词, 建索引,UN_TOKENIZED :不分词, 建索引,
			//NO_NORMS :不分词, 建索引。但是Field的值不像通常那样被保存,而是只取一个byte,这样节约存储空间}
			Field field1 = new Field("content", "搜索引擎", Store.YES, Index.TOKENIZED);
			doc1.add(field1);
			Document doc2 = new Document();
			Field field2 = new Field("content","创建索引",Store.YES,Index.TOKENIZED);
			doc2.add(field2);
			iwriter.addDocument(doc1);
			iwriter.addDocument(doc2);
			iwriter.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 检索索引
	 * @param word 关键字
	 * @return
	 */
	public Hits searchLuceneIndex(String word){
		//Query,Lucene提供了几种经常可以用到的
		//Query:TermQuery、MultiTermQuery、BooleanQuery、WildcardQuery、PhraseQuery、PrefixQuery、PhrasePrefixQuery、FuzzyQuery、RangeQuery、SpanQuery,
		//Query其实也就是指对于需要查询的字段采用什么样的方式进行查询,
		//如模糊查询、语义查询、短语查询、范围查询、组合查询等,还有就是QueryParser,
		//QueryParser可用于创建不同的Query,还有一个MultiFieldQueryParser支持对于多个字段进行同一关键字的查询,
		//IndexSearcher概念指的为需要对何目录下的索引文件进行何种方式的分析的查询,有点象对数据库的哪种索引表进行查询并按一定方式进行记录中字段的分解查询的概念,
		//通过IndexSearcher以及Query即可查询出需要的结果
		Query query=new WildcardQuery(new Term("content",word));
		Searcher search = null;
		try {
			search=new IndexSearcher(path);
			return search.search(query);
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics