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

基于 lucene3.1 开发demo.

阅读更多
package com.search.crawler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

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.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
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.util.Version;

public class IndexProcess {

	private static String indexPath = "src/indexFiles"; //生成索引存放目录

	public static void createIndex(String docsPath) {

		File docDir = new File(docsPath);
		if (!docDir.exists() || !docDir.canRead()) {
			System.out	.println("Document directory '"
							+ docDir.getAbsolutePath()
							+ "' does not exist or is not readable, please check the path");
			System.exit(1);
		}

		System.out.println("Indexing to directory '" + indexPath + "'...");
		Directory dir = null;
		try {
			dir = FSDirectory.open(new File(indexPath));

			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
			IndexWriter writer = new IndexWriter(dir, analyzer, true, new IndexWriter.MaxFieldLength(25000));
			
			indexDocs(writer, docDir);
			writer.close();
			dir.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	static void indexDocs(IndexWriter writer, File file) throws IOException {
		// do not try to index files that cannot be read
		if (file.canRead()) {
			if (file.isDirectory()) {
				File[] files = file.listFiles();
				if (files != null) {
					for (int i = 0; i < files.length; i++) {
						indexDocs(writer, files[i]);
					}
				}
			} else {

				FileInputStream fis = null;
				try {
					fis = new FileInputStream(file);
				} catch (FileNotFoundException fnfe) {
					fnfe.printStackTrace();
				}
				try {
					// make a new, empty document
					Document doc = new Document();
					
					//create index of path
					doc.add( new Field("path", file.getPath(),	Field.Store.YES, Field.Index.ANALYZED));
					
					//create index of content
					doc.add(new Field("content", new FileReader(file))); 
					
					writer.addDocument(doc);
					
				} finally {
					fis.close();
				}
			}
		}
	}

	static void search(String key ,String value) {
		
		Date startTime = new Date(); //
		Directory dir = null;
		try {
			dir = FSDirectory.open(new File(indexPath));
			IndexSearcher searcher = new IndexSearcher(dir,true);
			QueryParser par = new QueryParser(Version.LUCENE_31,key,new StandardAnalyzer(Version.LUCENE_31));
			Query query = null;
			try {
				query = par.parse(value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			System.out.println(query.toString());
			TopDocs topDocs = searcher.search(query, null, 1000);
			ScoreDoc[] scores = topDocs.scoreDocs;
			for (ScoreDoc soc : scores) {
				System.out.println(soc+"\t"+searcher.doc(soc.doc).get("path") );
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date endTime = new Date();
		System.out.println("总共花了" + (endTime.getTime() - startTime.getTime())+ "毫秒时间");

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 		IndexProcess.createIndex("src/index");
		System.out.println("search starting :");
		IndexProcess.search("content","revision");
	}

}

 

分享到:
评论

相关推荐

    lucene3.3的全部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.jar lucene-memory-3.3.0.jar lucene-misc-3.3.0.jar lucene-queries-3.3.0.jar ...

    java全文搜索lucene-3.0.0-src+lib.zip

    Lucene 3.0.0源码包含了多个模块,如core、analysis、queryparser、demo等,分别对应不同的功能模块。开发者可以通过阅读源码了解其内部实现原理,以便更好地进行二次开发和优化。 3.2 关键类解析 - Document:表示...

    Lucene入门与使用

    尽管 Lucene.NET 曾经面临开发停滞的问题,但随着时间的推移,该项目已经稳定下来,并继续为.NET 开发者提供了一个强大的全文搜索解决方案。 **1.2 Lucene 工作原理** Lucene 的核心机制基于索引检索技术,通过...

    深入 Lucene 索引机制深入 Lucene 索引机制

    Lucene的索引机制基于倒排索引,这种技术通过牺牲存储空间来换取快速的检索速度。在索引过程中,Lucene会对输入的文本进行分词,创建一个词项到文档位置的映射。当进行查询时,系统会快速定位到包含查询词的文档,而...

    Lucene快速入门

    - **添加路径**: 将demo.jar的路径添加到系统环境变量CLASSPATH中,例如:“D:\java\lucene-1.4-final\lucene-demos-1.4-final.jar”。 #### 4. Lucene的核心概念 - **索引(Index)**: 索引是Lucene中最核心的概念...

    ZendFramework中文文档

    3.1.3.1. 在PHP Session 中的缺省持久(Persistence) 3.1.3.2. 实现订制存储 3.1.4. 使用Zend_Auth 3.2. 数据库表认证 3.2.1. 简介 3.2.2. 高级使用:持久一个 DbTable 结果对象 3.2.3. 高级用法示例 3.3. ...

Global site tag (gtag.js) - Google Analytics