[align=center][size=x-large][color=red]lucene3.0.0 入门实例 创建索引 查询.[/color][/size][/align]package com.txt.test2;
import java.io.File; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; 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.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; 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.SimpleFSDirectory; import org.apache.lucene.util.Version; import org.junit.Test; public class LuceneTest { String txt1 = "holle world chengdu"; String txt2 = "holle world sichuang chengdu"; String txt3 = "holle world sichuang chengdu chengdu"; String txt4 = "holle world sichuang chengdu chengdu chengdu"; private File file = new File("f:" + File.separator + "indexDir6"); @Test public void create() throws Exception { // 索引库存储目录 Directory directory = new SimpleFSDirectory(file); // 创建词元分析器 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); // 创建索引器 IndexWriter writer = new IndexWriter(directory, analyzer, true, MaxFieldLength.LIMITED); // 创建文档 /** * 参数1 : 字段名称 相当于数据库的字段名称 参数2 :数据源名称 参数3 : 是否要把数据源名称存储到索引库中Store.YES 参数4 * : 是否要使用分词器作分析Index.ANALYZED */ Document document = new Document(); document.add(new Field("content", txt1, Store.YES, Index.ANALYZED)); Document document2 = new Document(); document2.add(new Field("content", txt2, Store.YES, Index.ANALYZED)); Document document3 = new Document(); document3.add(new Field("content", txt3, Store.YES, Index.ANALYZED)); Document document4 = new Document(); document4.add(new Field("content", txt4, Store.YES, Index.ANALYZED)); writer.addDocument(document); writer.addDocument(document2); writer.addDocument(document3); writer.addDocument(document4); writer.close(); } @Test public void search() throws Exception { // 索引存储目录 Directory directory = new SimpleFSDirectory(file); // 创建查询器 IndexSearcher searcher = new IndexSearcher(directory, true); // 创建查询解析器 对文档document的content字段进行查询 QueryParser parser = new QueryParser(Version.LUCENE_30, "content", new StandardAnalyzer(Version.LUCENE_30)); //创建查询类 String key = "chengdu"; Query query = parser.parse(key); //获取钱n位索引 TopDocs tDocs = searcher.search(query, 100); System.out.println("关键字"+key+"一共有"+tDocs.totalHits+"条记录索引."); for(int i = 0; i<tDocs.scoreDocs.length; i++){ ScoreDoc sDoc = tDocs.scoreDocs[i]; System.out.println("文档编号的索引:"+sDoc.doc); System.out.println("得分:"+sDoc.score); Document document = searcher.doc(sDoc.doc); System.out.println("内容是:"+document.get("content")); } searcher.close(); } }
相关推荐
java全文搜索lucene-3.0.0-src+lib.zip
lucene3.0.0 入门DEMO 适合新手使用 先要新建号相关的目录
luceneTest 博文链接:https://wolftou.iteye.com/blog/148701
The new version is mostly a cleanup release without any new features. All deprecations targeted to be removed in version 3.0 were removed.
Lucene使用lucene入门[归类].pdf
lucene-3.0.0-src.zip
lucene-3.0.0是lucene目前最新版本
Lucene索引器实例Lucene索引器实例Lucene索引器实例Lucene索引器实例
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....