资源地址: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框架,实现商品搜索,欢迎交流)。