您的位置:首页 > 其它

lucene测试demo

2014-05-19 21:50 267 查看
1、在http://lucene.apache.org/下lucene4.8.0版本

2、新建工程依赖lucene-core、lucene-analyzers-common两个jar包

3、测试代码(从《lucene实战》里面弄出来的,稍微适配一下版本)

package search.basictest;

import java.io.IOException;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.util.Version;

public class BasicTest {
private static final String[] examples = {
"The quick brown fox jumped over the lazy dog",
"XY&Z Corporation = xyz@example.com" };
private static final Analyzer[] analyzers = new Analyzer[] {
new WhitespaceAnalyzer(Version.LUCENE_48),
new SimpleAnalyzer(Version.LUCENE_48),
new StopAnalyzer(Version.LUCENE_48),
new StandardAnalyzer(Version.LUCENE_48) };

public static void main(String[] args) throws IOException {
String[] strs = examples;
if (args.length > 0) {
strs = args;
}
for (String text : strs) {
analyze(text);
}
}

private static void analyze(String text) throws IOException {

System.out.println("\n\nAnalyzing \"" + text + "\"");
for (Analyzer analyzer : analyzers) {
String name = analyzer.getClass().getSimpleName();
System.out.println("\n  " + name + ":");
System.out.print("   ");
displayTokens(analyzer, text);
}
}

private static void displayTokens(Analyzer analyzer, String text)
throws IOException {
TokenStream t = analyzer.tokenStream("contents, ", new StringReader(
text));
t.reset();
displayTokens(t);
t.close();
}

private static void displayTokens(TokenStream tokenStream)
throws IOException {
CharTermAttribute term = tokenStream.addAttribute(CharTermAttribute.class);
while (tokenStream.incrementToken()) {
System.out.print("[" + term.toString() + "] ");
}
}
}
4、输出

Analyzing "The quick brown fox jumped over the lazy dog"

WhitespaceAnalyzer:
[The] [quick] [brown] [fox] [jumped] [over] [the] [lazy] [dog]
SimpleAnalyzer:
[the] [quick] [brown] [fox] [jumped] [over] [the] [lazy] [dog]
StopAnalyzer:
[quick] [brown] [fox] [jumped] [over] [lazy] [dog]
StandardAnalyzer:
[quick] [brown] [fox] [jumped] [over] [lazy] [dog]

Analyzing "XY&Z Corporation = xyz@example.com"

WhitespaceAnalyzer:
[XY&Z] [Corporation] [=] [xyz@example.com]
SimpleAnalyzer:
[xy] [z] [corporation] [xyz] [example] [com]
StopAnalyzer:
[xy] [z] [corporation] [xyz] [example] [com]
StandardAnalyzer:
[xy] [z] [corporation] [xyz] [example.com]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: