`
sbl2255
  • 浏览: 221836 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

初试 Lucene3.3.0

 
阅读更多
从网上下载了个3.3.0版本的.看着demo做了个例子:

public class IndexClasss {

	String path = "c:\\lucene\\";
	String path2 = "e:\\api\\"; 
		
	
	public void  createIndex() throws IOException{
		Directory dir = FSDirectory.open(new File(path));
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_33);
		IndexWriterConfig con = new IndexWriterConfig(Version.LUCENE_33,analyzer);
		IndexWriter writer = new IndexWriter(dir,con);
		Document doc = new Document();
		Date start = new Date();
		if(addField2Document(listFiles(path2),doc)){
			Date end = new Date();
			System.out.println("total time: " + (end.getTime()-start.getTime()) + " ms");
			System.out.println("---------------------------------------");
		}else{
			System.out.println("false to create Index by path :" +path);
		}
		writer.addDocument(doc);
		writer.optimize();
		writer.close();
	}
	
	public boolean addField2Document(List<Fieldable> list,Document doc){
		if(list == null || list.isEmpty()){
			return false;
		}else{
			for(int a=0;a<list.size();a++){
				doc.add(list.get(a));
			}
		}
		return true;
	}
	
	public List listFiles(String path){
		if(path==null || "".equals(path)){
			return null;
		}
		List<Fieldable> fields = new ArrayList();
		File files = new File(path);
		if(files.isDirectory()){
			String[] fs = files.list();
			for(int a=0;a<fs.length;a++){
				System.out.println("found " + fs[a]);
				fields.add(getFieldable(new File(path,fs[a])));
			}
		}else{
			fields.add(getFieldable(new File(path)));
		}
		System.out.println("found files : \t"+fields.size());
		System.out.println("create index files finished....");
		return fields;
	}
	
	
	public Fieldable getFieldable(File file){
		Field field = new Field("path",file.getPath(),Field.Store.YES,Field.Index.ANALYZED);
		return field;
	}
	
	public void QuerySearcher(String querystr) throws ParseException, IOException{
		Directory dir = FSDirectory.open(new File(path));
		IndexSearcher search = new IndexSearcher(dir);
		System.out.println("found index : " +dir.listAll().length +" files");
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_33);
		QueryParser parse = new QueryParser(Version.LUCENE_33,"path",analyzer);
		Query query = parse.parse(querystr);
		TopDocs it = search.search(query,100);
		System.out.println("search : " + query.toString());
		ScoreDoc[] docs = it.scoreDocs;
		for(int a=0;a<docs.length;a++){
			System.out.println(a+"\t"+docs.toString());
		}
	}
	
	public Fieldable getField(String name,String value){
		return new Field(name,value,Field.Store.YES,Field.Index.ANALYZED);
	}
	
	public static void main(String[] arg) throws IOException, ParseException{
		IndexClasss index  =new IndexClasss();
		index.createIndex();
		index.QuerySearcher("Struts1.3.chm");
	}
}

分享到:
评论

相关推荐

    Lucene3.3.0学习Demo

    这个"Lucene3.3.0学习Demo"是针对这一版本进行的实践教学资源,旨在帮助开发者更好地理解和使用Lucene。 首先,我们来看看Lucene的基本工作流程: 1. **分词**:Lucene通过Analyzer处理输入的文本,将其分解为一...

    Lucene 3.3.0 API CHM 格式

    Lucene 3.3.0 API chm格式带全文索引方便携带和查询。 从之前发布其他chm文件下载用户的反映看,有不少朋友反映下载后打开无法显示,这一般不是chm文件的问题,这里统一说明一下解决办法: 如果文件打开看不到右边的...

    Java多级多类型全文检索 - 基于Lucene3.3.0

    在这个“Java多级多类型全文检索 - 基于Lucene3.3.0”的主题中,我们将深入探讨如何利用Lucene 3.3.0版本来实现复杂且高效的检索机制,支持多种文件类型和多层次的索引构建。 首先,Lucene是一个开源的全文检索框架...

    lucene-3.3.0-src.zip

    《Apache Lucene 3.3.0 源码解析》 Apache Lucene 是一个开源全文搜索引擎库,它为开发者提供了强大的文本搜索功能。这里我们关注的是它的3.3.0版本的源代码,文件名为“lucene-3.3.0-src.zip”。这个版本在2011年...

    lucene3.3的全部jar包

    lucene-analyzers-3.3.0.jar lucene-benchmark-3.3.0.jar lucene-core-3.3.0.jar lucene-demo-3.3.0.jar lucene-grouping-3.3.0.jar lucene-highlighter-3.3.0.jar lucene-icu-3.3.0.jar lucene-instantiated-3.3.0....

    lucene-core-3.3.0.jar

    lucene-core-3.3.0.jarlucene-core-3.3.0.jar

    lucene-3.3.0.zip

    lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎。

    lucene-memory-3.3.0.jar

    lucene-memory-3.3.0.jar 包下载

    lucene-analyzers-3.3.0 jar包和源码

    《深入理解Lucene-Analyzers-3.3.0:jar包与源码解析》 在信息技术领域,搜索引擎是至关重要的工具,而Lucene作为开源全文检索库,为开发者提供了强大的搜索功能。本文将深入探讨Lucene-Analyzers-3.3.0版本,解析...

    lucene-highlighter-3.3.0.jar

    lucene-highlighter-3.3.0.jar 包下载

    lucene-analyzers-3.3.0.jar

    lucene-analyzers-3.3.0.jar包下载

    lucene,lucene教程,lucene讲解

    lucene,lucene教程,lucene讲解。 为了对文档进行索引,Lucene 提供了五个基础的类 public class IndexWriter org.apache.lucene.index.IndexWriter public abstract class Directory org.apache.lucene.store....

    chinese_lucene.rar_Lucene C#_Lucene for .net_PanGu4Lucene_lucene

    《深入理解Lucene C#与.NET环境下的应用实践》 Lucene是一款强大的全文搜索引擎库,最初由Apache软件基金会开发,现已成为Apache Lucene项目的一部分。它提供了高效的索引和搜索功能,广泛应用于各类数据检索系统。...

    lucene3.3全部jar包

    - `lucene-core-3.3.0.jar`:Lucene 的核心库,包含了索引和搜索的基本功能。 - `lucene-analyzers-3.3.0.jar`:提供了多种语言的文本分析器,用于处理不同语言的文本。 - `lucene-backward-codecs-3.3.0.jar`:为了...

    lucene3.0 lucene3.0

    lucene3.0 lucene3.0 lucene3.0 lucene3.0 lucene3.0

    lucene-4.7.0全套jar包

    【Lucene 4.7.0 全套JAR包详解】 Lucene是一个开源全文搜索引擎库,由Apache软件基金会开发并维护。它提供了一个高级、灵活的文本搜索API,允许开发者轻松地在应用程序中实现复杂的搜索功能。这次提供的“lucene-...

    Lucene3.5源码jar包

    本压缩包包含的是Lucene 3.5.0版本的全部源码,对于想要深入理解Lucene工作原理、进行二次开发或者进行搜索引擎相关研究的开发者来说,是一份非常宝贵的学习资源。 Lucene 3.5.0是Lucene的一个重要版本,它在3.x...

    Lucene时间区间搜索

    Lucene是一款强大的全文搜索引擎库,广泛应用于各种数据检索场景。在C#环境下,利用Lucene进行时间区间搜索是提高数据检索效率和精确度的重要手段。本篇将深入探讨如何在C#中实现Lucene的时间区间查询匹配,以及涉及...

    Lucene示例 BM25相似度计算

    在IT领域,搜索引擎技术是至关重要的,而Lucene作为一个开源全文搜索引擎库,广泛应用于各种文本检索系统中。本文将深入探讨Lucene示例中的BM25相似度计算,旨在帮助初学者理解如何利用Lucene 4.7.1版本构建索引、...

    lucene in action英文版 lucene 3.30包

    《Lucene in Action》是关于Apache Lucene的权威指南,这本书深入浅出地介绍了全文搜索引擎的构建和优化。Lucene是一个高性能、全文本搜索库,它允许开发人员在应用程序中轻松实现复杂的搜索功能。这本书主要面向...

Global site tag (gtag.js) - Google Analytics