lucene测试demo

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] 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值