lucene 与IKAnalyzer实现中文分词查询 (最新jar及实现代码)

1 篇文章 0 订阅
1 篇文章 0 订阅

  资源地址:http://download.csdn.net/detail/qq540061627/4063916

lucene3.5 IKAnalyzer3.2.5 实例中文分词通过,目前在网上找的lucene 和IKAnalyzer 的最新版本测试通过。内含:示例代码,以及最新jar包。

lucene lucene3.5 IKAnalyzer IKAnalyzer3.2.5 jar 中文 分词

测试项目截图(测试项目所用jar可见):



创建索引:

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
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.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class FileIndexer {
	private File baseDir = new File("F:\\dateDir");
	private File indexDir2 = new File("F:\\indexDir");
	Analyzer luceneAnalyzer = new IKAnalyzer();
	Directory indexDir = null ;
	public FileIndexer() {
		if (!this.baseDir.exists() || !this.indexDir2.exists()) {
			return;
		}
	}

	@SuppressWarnings("deprecation")
	public void createIndex() {
		try {

			IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35,luceneAnalyzer);
			
			indexDir=new SimpleFSDirectory(indexDir2);
			config.setOpenMode(OpenMode.CREATE);
			IndexWriter indexWriter = null;
			
			indexWriter = new IndexWriter(indexDir,config);
			
			
			Document doc1 = new Document();
			doc1.add(new Field("id","1",Store.YES,Index.ANALYZED));
			doc1.add(new Field("name","liangge",Store.YES,Index.ANALYZED));
			doc1.add(new Field("content","我是中国人",Store.YES,Index.ANALYZED));
			Document doc2 = new Document();
			doc2.add(new Field("id","2",Store.YES,Index.ANALYZED));
			doc2.add(new Field("name","liangge is a girl",Store.YES,Index.ANALYZED));
			doc2.add(new Field("content","liangge am a good girl",Store.YES,Index.ANALYZED));
	        indexWriter.addDocument(doc1);
	        indexWriter.addDocument(doc2);
	        indexWriter.close();
			
			
			
			
			System.out.println("完毕");
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public static void main(String[] args) {
		FileIndexer lucene = new FileIndexer();
		lucene.createIndex();
	}
}

单Field搜索:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.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.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;


public class FileSearcher {

private File indexDir = new File("F:\\indexDir");;
	
	public List search(String fieldName, String keyword) {
		Directory directory = null;
		IndexSearcher is = null;
		TopDocs topDocs=null;

		try {


			Query query = IKQueryParser.parse(fieldName, keyword);  // 
			System.out.println(query.toString()+"----------"+QueryParser.escape(keyword));
			
			directory = SimpleFSDirectory.open(indexDir);
			 IndexReader reader = IndexReader.open(new SimpleFSDirectory(indexDir));
			is = new IndexSearcher(reader);
			is.setSimilarity(new IKSimilarity()); //
			
			topDocs = is.search(query,100);  // 

		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		for (int j = 0; j < topDocs.totalHits; j++) {
			Document dd=is.doc(scoreDocs[j].doc);
			System.out.println(dd.toString());
			System.out.println(dd.get("id"));
			System.out.println(dd.get("name"));
			System.out.println(dd.get("content"));
		}

		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {is.close();} 
					catch (IOException e) {e.printStackTrace();}
			}
			if (directory != null) {
				try {directory.close();} 
					catch (IOException e) {e.printStackTrace();}
			}
		}
		return null;
	}

	public static void main(String[] args) {
		FileSearcher search = new FileSearcher();
		search.search("content", "我是中国人");
	}
}


完整实例 资源地址:资源地址:http://download.csdn.net/detail/qq540061627/4063916

希望对你有帮助(正在进一步研究中,准备整合进一个现有的SSH框架,实现商品搜索,欢迎交流)。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq540061627

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值