搜索引擎luence之目录索引
2014-06-09 15:01
204 查看
/* * Created on 2004-11-20 * * index a dir file */ package demo; /**使用lucence生成目录索引 * */ import org.apache.lucene.index.IndexWriter; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.analysis.standard.StandardAnalyzer; import java.io.BufferedReader; import java.io.FileReader; import java.io.File; public class DirSpider { private String indexDir; private String sSourceDir; //控制显示配置参数 private boolean verbose; //控制是否增量索引 private boolean incremental; private IndexWriter index; //从主函数的参数中得到所需配置信息 public static void main(String[] args) { try { DirSpider s = new DirSpider(args); s.go(); } catch (Exception e) { e.printStackTrace(); } } public DirSpider(String argv[]) throws Exception { verbose = false; incremental = false; for (int i = 0; i < argv.length; i++) { if (argv[i].equals("-s")) sSourceDir = argv[++i]; if(argv[i].equals("-d")) indexDir = argv[++i]; else if(argv[i].equals("-v")) verbose = true; else if(argv[i].equals("-a")) incremental = true; } if (sSourceDir == null) throw new IllegalArgumentException("Missing required argument: -s [SourceDir dir]"); if (indexDir == null) throw new IllegalArgumentException("Missing required argument: -d [index dir]"); } public void go() throws Exception { long start = System.currentTimeMillis(); // create the index directory -- or append to existing if (verbose) { System.out.println("Creating index in: " + indexDir); if (incremental) System.out.println(" - using incremental mode"); } index = new IndexWriter(new File(indexDir), new StandardAnalyzer(), !incremental); File dir = new File(sSourceDir); indexDir(dir); index.optimize(); index.close(); if(verbose) System.out.println("index complete in :"+(System.currentTimeMillis() - start)/1000); } private void indexDir(File dir) { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { File f = files[i]; if (f.isDirectory()) { indexDir(f); // recurse 递归调用 } else if (f.getName().endsWith(".txt")) {//现在只对txt文本文件索引 indexFile(f); } } } private void indexFile(File item) { if (verbose) System.out.println("Adding FILE: " + item); News news = loadFile(item); if ( news!= null && news.body != null) { Document doc = new Document(); Field f = new Field("url", news.URL , Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.NO); doc.add(f); f = new Field("title", news.title , Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS); doc.add(f); f = new Field("content", news.body.toString() , Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS); doc.add(f); System.out.println(news); //要清晰异常是什么,该怎样去处理。 try{ index.addDocument(doc); } catch(Exception e) { e.printStackTrace(); //System.exit(0)和System.exit(1)分别表示正常退出和异常退出 System.exit(-1); } }else{ System.out.println("索引数据为空!"); } } private static News loadFile(File sSourceFile){ News news = new News(); try { //注意附加URL的方式 //news.URL = "http://www.lietu.com/segtest/"+sSourceFile; news.URL="http://localhost:8080/Chapter2WebPart/"+sSourceFile; BufferedReader br = new BufferedReader(new FileReader(sSourceFile)); String s; if ( (s = br.readLine()) != null ) { news.title = s; System.out.println(s); while( (s = br.readLine()) != null ) { news.body.append(s); news.body.append('\n'); } } br.close(); } catch (Exception e) { e.printStackTrace(); } return news; } } class News { public String URL; public String title; public StringBuffer body; public News(){ this.URL = ""; this.title = ""; this.body = new StringBuffer(); } public String toString(){ return "URL :"+URL+" title :" + title +" body :"+ body.toString() ; } }
package com.lietu.web.test; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import com.lietu.web.bean.example.Article; /** * 根据luence索引搜索数据 * */ public class MySearche { public MySearche() { super(); } public static void main(String[] args) { new MySearche().indexSeaches("中"); } // 索引查询 public Collection indexSeaches(String str) { System.out.println("str:" + str); Collection collections = new ArrayList(); try { // 创建搜索对象 IndexSearcher indexSearcher = new IndexSearcher("C:/index"); // 创建索引读取对象 IndexReader reader = IndexReader.open("C:/index"); Query titleQuery = null; // 创建查询分析器,对File属性title进行查询,采用的分析器是StandardAnalyzer QueryParser queryparser = new QueryParser("title", new StandardAnalyzer()); try { // 分析用户输入的字符串 Query query = queryparser.parse(str); // 根据用户的输入开始搜索 Hits hit = indexSearcher.search(query); System.out.println("hit.length():" + hit.length()); for (int i = 0; i < hit.length(); i++) { Article article = new Article(); // 得到title article.setTitle(hit.doc(i).get("title")); // 得到content article.setContent(hit.doc(i).get("content")); // 把document添加到集合中,并且返回 collections.add(article); System.out.println("标题:" + hit.doc(i).get("title")); System.out.println("内容:" + hit.doc(i).get("content")); } } catch (ParseException e) { e.printStackTrace(); } } catch (CorruptIndexException e) { } catch (IOException e) { e.printStackTrace(); } return collections; } }
相关文章推荐
- 搜索引擎的蜘蛛程序、目录索引工作原理
- 对文件目录进行索引(版本1.5)
- 点石互动--highdiy之:搜索引擎如何索引收录网页
- 禁止Apache显示目录索引的常见方法
- XNA教程-2D游戏-目录索引
- 搜索引擎之内存映射快速索引文件
- 数学之美系列五 -- 简单之美:布尔代数和搜索引擎的索引
- PhpStorm 合理标注目录让索引和扫描更加地高效
- SQL Server 2008中SQL应用系列及BI学习笔记系列--目录索引 [转载]
- 各种开发工具索引/目录
- dentry的中文名称是目录项,是Linux文件系统中某个索引节点(inode)的链接。这个索引节点可以是文件,也可以是目录
- Kotlin入门教程——目录索引
- Knockout应用开发指南(完整版) 目录索引
- 数学之美系列之五:简单之美--布尔代数和搜索引擎的索引
- 最新res索引讲解(drawable、layout、values等目录的分辨率和layout的横竖屏,以及values的多语言)
- 搜索引擎索引之索引基础
- 搜索引擎及目录免费登录入口
- luence全文检索本地磁盘,可构建磁盘搜索引擎,有代码
- solr学习第七课----solr之数据库数据导入生成索引(DataImportHandler)-基于solr搜索引擎
- 禁止apache显示目录索引的常见方法