the usage of lucene

来源:互联网 发布:免费文档加密软件 编辑:程序博客网 时间:2024/06/03 02:27

1 create index

IndexWriter writer = new IndexWriter(index path,new SimpleAnalyzer(),true);

index path: location of index

new SimpleAnalyzer():

there are four analyers in lucene:

WhitespaceAnalyzer  : only remove white space ,not lowcase and not support chinese.

simpleAnalyzer: stronger than whitespaceAnalyzer,filter all string except letter and lowcase but not support chinese.

StopAnalyzer: StopAnalyzer的功能超越了SimpleAnalyzer,在SimpleAnalyzer的基础上.增加了去除StopWords的功能,不支持中文.类中使用一个static数组保存了ENGLISH_STOP_WORDS, 太常见不index的words
StandardAnalyzer: 用Javacc定义的一套EBNF,严禁的语法。有人说英文的处理能力同于StopAnalyzer.支持中文采用的方法为单字切分。未仔细比较,不敢确定。

其他的扩展:
ChineseAnalyzer:来自于Lucene的sand box.性能类似于StandardAnalyzer,缺点是不支持中英文混和分词.
CJKAnalyzer:chedong写的CJKAnalyzer的功能在英文处理上的功能和StandardAnalyzer相同.但是在汉语的分词上,不能过滤掉标点符号,即使用二元切分
TjuChineseAnalyzer: http://windshowzbf.bokee.com/3016397.html写的,功能最为强大.TjuChineseAnlyzer的功能相当强大,在中文分词方面由于其调用的为ICTCLAS的java接口.所以其在中文方面性能上同与ICTCLAS.其在英文分词上采用了Lucene的StopAnalyzer,可以去除 stopWords,而且可以不区分大小写,过滤掉各类标点符号.

the third parameter: true is that create new index or override the old index file.

false is that append new content to old file.

final Document doc = new Document();

doc.add();

writer.add(doc);

 

2 search content

IndexSearch search = new IndexSearch(index path);

final Query query = QueryParser.parse(queryString, KEYWORD_CONTENT, new SimpleAnalyzer());

queryString: the key word that need search.

KEYWORD_CONTENT:the default field for query terms.

hits = searcher.search(query);

 

3 get search content

hits.doc(i).get(FIELD_ID)

 

 

原创粉丝点击