- 浏览: 834375 次
文章分类
- 全部博客 (365)
- java (124)
- spring mvc (21)
- spring (22)
- struts2 (6)
- jquery (27)
- javascript (24)
- mybatis/ibatis (8)
- hibernate (7)
- compass (11)
- lucene (26)
- flex (0)
- actionscript (0)
- webservice (8)
- rabbitMQ/Socket (15)
- jsp/freemaker (5)
- 数据库 (27)
- 应用服务器 (21)
- Hadoop (1)
- PowerDesigner (3)
- EJB (0)
- JPA (0)
- PHP (2)
- C# (0)
- .NET (0)
- html (2)
- xml (5)
- android (7)
- flume (1)
- zookeeper (0)
- 证书加密 (2)
- maven (1)
- redis (2)
- cas (11)
最新评论
-
zuxianghuang:
通过pom上传报错 Artifact upload faile ...
nexus上传了jar包.通过maven引用当前jar,不能取得jar的依赖 -
流年末年:
百度网盘的挂了吧???
SSO单点登录系列3:cas-server端配置认证方式实践(数据源+自定义java类认证) -
953434367:
UfgovDBUtil 是什么类
Java发HTTP POST请求(内容为xml格式) -
smilease:
帮大忙了,非常感谢
freemaker自动生成源代码 -
syd505:
十分感谢作者无私的分享,仔细阅读后很多地方得以解惑。
Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
1。IndexWriter的学习
IndexWriter类的构造函数共四个参数:
(1).Directory dir:FSDirectory:表示对文件系统目录的操作;RAMDirectory:内存中的目录操作,一般FSDirectory用的较多。
(2).Analyzer a: 用来对文档进行词法分析和语言处理(StandardAnalyzer对中文分词的支持不是很好)
(3).boolean b :如果没有该索引就创建,否则覆盖原有索引
(4).
2.document的学习
Document是lucene自己定义的一种文件格式,lucene使用docement来代替对应的物理文件或者保存在数据库中的数据。因此Document只能作为数据源在Lucene中的数据存贮的一种文件形式。
Document只是负责收集数据源,因为不同的文件可以构建同一个Document。只要用户将不同的文件创建成Document类型的文件,Lucene就能快速找到查找并且使用他们。
对于一个Document文件,可以同时增加多个Field。Lucene中对于每个数据源是使用Field类来表示的。多个Field组成一个Document,多个Document组成一个索引文件。
3。
4。搜索
搜索过程如下:
创建IndexSearcher准备进行搜索
创建Analyer用来对查询语句进行词法分析和语言处理
创建QueryParser用来对查询语句进行语法分析
QueryParser调用parser进行语法分析,形成查询语法树,放到Query中
IndexSearcher调用search对查询语法树Query进行搜索,得到结果TopScoreDocCollector
附上例子:
(1)创建索引
2。搜索
- IndexWriter writer = new IndexWriter(FSDirectory.open("E:\\test\\index"),
- new StandardAnalyzer(Version.LUCENE_CURRENT), true,
- IndexWriter.MaxFieldLength.LIMITED);
IndexWriter writer = new IndexWriter(FSDirectory.open("E:\\test\\index"), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
IndexWriter类的构造函数共四个参数:
(1).Directory dir:FSDirectory:表示对文件系统目录的操作;RAMDirectory:内存中的目录操作,一般FSDirectory用的较多。
(2).Analyzer a: 用来对文档进行词法分析和语言处理(StandardAnalyzer对中文分词的支持不是很好)
(3).boolean b :如果没有该索引就创建,否则覆盖原有索引
(4).
2.document的学习
Document是lucene自己定义的一种文件格式,lucene使用docement来代替对应的物理文件或者保存在数据库中的数据。因此Document只能作为数据源在Lucene中的数据存贮的一种文件形式。
Document只是负责收集数据源,因为不同的文件可以构建同一个Document。只要用户将不同的文件创建成Document类型的文件,Lucene就能快速找到查找并且使用他们。
对于一个Document文件,可以同时增加多个Field。Lucene中对于每个数据源是使用Field类来表示的。多个Field组成一个Document,多个Document组成一个索引文件。
- Document doc = new Document();
- doc.add(new Field("contents", new FileReader(f)));
- doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES,
- Field.Index.ANALYZED));
- writer.addDocument(doc);
Document doc = new Document(); doc.add(new Field("contents", new FileReader(f))); doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);
3。
int numIndexed = writer.numDocs();//当前索引中文档的个数 writer.optimize(); writer.close();
4。搜索
搜索过程如下:
创建IndexSearcher准备进行搜索
创建Analyer用来对查询语句进行词法分析和语言处理
创建QueryParser用来对查询语句进行语法分析
QueryParser调用parser进行语法分析,形成查询语法树,放到Query中
IndexSearcher调用search对查询语法树Query进行搜索,得到结果TopScoreDocCollector
- IndexSearcher is = new IndexSearcher(FSDirectory.open(indexDir), true);// read-only
- String field = "contents";
- QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field,
- new StandardAnalyzer(Version.LUCENE_CURRENT));
- Query query = parser.parse(q);
- TopScoreDocCollector collector = TopScoreDocCollector.create(TOP_NUM,
- false);
- long start = new Date().getTime();// start time
- is.search(query, collector);
IndexSearcher is = new IndexSearcher(FSDirectory.open(indexDir), true);// read-only String field = "contents"; QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field, new StandardAnalyzer(Version.LUCENE_CURRENT)); Query query = parser.parse(q); TopScoreDocCollector collector = TopScoreDocCollector.create(TOP_NUM, false); long start = new Date().getTime();// start time is.search(query, collector);
附上例子:
(1)创建索引
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Date;
- 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.store.FSDirectory;
- import org.apache.lucene.util.Version;
- public class Indexer {
- private static String INDEX_DIR = "E:\\test\\index";// 索引存放目录
- private static String DATA_DIR = "E:\\test\\file\\";// 文件存放的目录
- public static void main(String[] args) throws Exception {
- long start = new Date().getTime();
- int numIndexed = index(new File(INDEX_DIR), new File(DATA_DIR));// 调用index方法
- long end = new Date().getTime();
- System.out.println("Indexing " + numIndexed + " files took "
- + (end - start) + " milliseconds");
- }
- /**
- * 索引dataDir下的.txt文件,并储存在indexDir下,返回索引的文件数量
- * @param indexDir
- * @param dataDir
- * @return int
- * @throws IOException
- */
- public static int index(File indexDir, File dataDir) throws IOException {
- if (!dataDir.exists() || !dataDir.isDirectory()) {
- throw new IOException(dataDir
- + " does not exist or is not a directory");
- }
- IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir),
- new StandardAnalyzer(Version.LUCENE_CURRENT), true,
- IndexWriter.MaxFieldLength.LIMITED);
- indexDirectory(writer, dataDir);// 调用indexDirectory方法
- int numIndexed = writer.numDocs();//当前索引中文档的个数
- writer.optimize();
- writer.close();
- return numIndexed;
- }
- /**
- * 循环遍历目录下的所有.txt文件并进行索引
- * @param writer
- * @param dir
- * @throws IOException
- */
- private static void indexDirectory(IndexWriter writer, File dir)
- throws IOException {
- File[] files = dir.listFiles();
- for (int i = 0; i < files.length; i++) {
- File f = files[i];
- if (f.isDirectory()) {
- indexDirectory(writer, f); // recurse
- } else if (f.getName().endsWith(".txt")) {
- indexFile(writer, f);
- }
- }
- }
- /**
- * 对单个txt文件进行索引
- * @param writer
- * @param f
- * @throws IOException
- */
- private static void indexFile(IndexWriter writer, File f)
- throws IOException {
- if (f.isHidden() || !f.exists() || !f.canRead()) {
- return;
- }
- System.out.println("Indexing " + f.getCanonicalPath());
- Document doc = new Document();//针对参数文件建立索引文档
- doc.add(new Field("contents", new FileReader(f)));
- doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES,
- Field.Index.ANALYZED));
- writer.addDocument(doc);//在writer中加入此文档
- }
- }
import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Date; 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.store.FSDirectory; import org.apache.lucene.util.Version; public class Indexer { private static String INDEX_DIR = "E:\\test\\index";// 索引存放目录 private static String DATA_DIR = "E:\\test\\file\\";// 文件存放的目录 public static void main(String[] args) throws Exception { long start = new Date().getTime(); int numIndexed = index(new File(INDEX_DIR), new File(DATA_DIR));// 调用index方法 long end = new Date().getTime(); System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds"); } /** * 索引dataDir下的.txt文件,并储存在indexDir下,返回索引的文件数量 * @param indexDir * @param dataDir * @return int * @throws IOException */ public static int index(File indexDir, File dataDir) throws IOException { if (!dataDir.exists() || !dataDir.isDirectory()) { throw new IOException(dataDir + " does not exist or is not a directory"); } IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); indexDirectory(writer, dataDir);// 调用indexDirectory方法 int numIndexed = writer.numDocs();//当前索引中文档的个数 writer.optimize(); writer.close(); return numIndexed; } /** * 循环遍历目录下的所有.txt文件并进行索引 * @param writer * @param dir * @throws IOException */ private static void indexDirectory(IndexWriter writer, File dir) throws IOException { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { File f = files[i]; if (f.isDirectory()) { indexDirectory(writer, f); // recurse } else if (f.getName().endsWith(".txt")) { indexFile(writer, f); } } } /** * 对单个txt文件进行索引 * @param writer * @param f * @throws IOException */ private static void indexFile(IndexWriter writer, File f) throws IOException { if (f.isHidden() || !f.exists() || !f.canRead()) { return; } System.out.println("Indexing " + f.getCanonicalPath()); Document doc = new Document();//针对参数文件建立索引文档 doc.add(new Field("contents", new FileReader(f))); doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);//在writer中加入此文档 } }
2。搜索
- import java.io.File;
- import java.util.Date;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- 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.TopScoreDocCollector;
- import org.apache.lucene.store.FSDirectory;
- import org.apache.lucene.util.Version;
- public class Searcher {
- private static String INDEX_DIR = "E:\\test\\index\\";// 索引所在的路径
- private static String KEYWORD = "接受";// 关键词
- private static int TOP_NUM = 100;// 显示前100条结果
- public static void main(String[] args) throws Exception {
- File indexDir = new File(INDEX_DIR);
- if (!indexDir.exists() || !indexDir.isDirectory()) {
- throw new Exception(indexDir
- + " does not exist or is not a directory.");
- }
- search(indexDir, KEYWORD);// 调用search方法进行查询
- }
- /**
- * 查询
- *
- * @param indexDir
- * @param q
- * @throws Exception
- */
- public static void search(File indexDir, String q) throws Exception {
- IndexSearcher is = new IndexSearcher(FSDirectory.open(indexDir), true);// read-only
- String field = "contents";
- QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field,
- new StandardAnalyzer(Version.LUCENE_CURRENT));
- Query query = parser.parse(q);
- TopScoreDocCollector collector = TopScoreDocCollector.create(TOP_NUM,
- false);
- long start = new Date().getTime();// start time
- is.search(query, collector);
- ScoreDoc[] hits = collector.topDocs().scoreDocs;
- System.out.println(hits.length);
- for (int i = 0; i < hits.length; i++) {
- Document doc = is.doc(hits[i].doc);// new method is.doc()
- System.out.println(doc.getField("filename") + " "
- + hits[i].toString() + " ");
- }
- long end = new Date().getTime();// end time
- System.out.println("Found " + collector.getTotalHits()
- + " document(s) (in " + (end - start)
- + " milliseconds) that matched query '" + q + "':");
- }
- }
发表评论
-
Solr4.0+IKAnalyzer中文分词安装
2012-11-29 19:14 1540有近2年没接触Solr跟Lucene ... -
solr搜索打分规制排序
2012-09-26 21:58 2361solr使用了Lucene的内核,也继承了Luce ... -
solr DataimportHanler
2012-09-22 17:01 1136大多数的应用程序将数据存储在关系数据库、xml文件 ... -
solr第一弹 autocomplete(自动补全)
2012-09-22 16:38 1422百度和google中都有 ... -
全文搜索服务器solr之客户端 - solrj二次开发
2012-09-21 09:07 4783Solrj已经是很强大的solr客户端了。它本身就包装了h ... -
Solr Data Import 快速入门
2012-09-20 14:32 808原文出处:http://blog.chenl ... -
JAVA环境下利用solrj二次开发SOlR搜索的环境部署常见错误
2012-09-20 11:36 1752问题一:出现控制台坏的响应错误一Bad reque ... -
Solr学习总结
2012-09-20 10:06 6404一、 SOLR搭建企业搜索平台 运行环境: 运行容器:Tomc ... -
olr 的客户端调用solrj 建索引+分页查询
2012-09-20 08:54 1884在 solr 3.5 配置及应用(一) 讲过一了 sol ... -
Solr笔记
2012-09-19 23:07 1248... -
Apache Solr 初级教程(介绍、安装部署、Java接口、中文分词)
2012-09-19 22:56 1725Apache Solr 介绍 Solr 是 ... -
lucene3.0 分页显示与高亮显示(转)
2012-09-19 11:44 1642分页类 Java代码 pac ... -
lucene3 中文IKAnalyzer分词例子
2012-09-10 13:37 1150import java.io.IOException; im ... -
Lucene3.0.1 学习笔记
2012-09-08 08:57 923不管怎么说,搜索都是非 ... -
Compass2.0.2自带例子解析
2012-09-05 08:47 1436Compass2.0.2自带例子解析: 下面的代码来自com ... -
compass站内搜索
2012-09-05 08:49 960compass站内搜索: 1.去官方网站下载compass的 ... -
Spring + Compass + paoding配置
2012-09-05 08:50 1021Spring + Compass + paoding配置: ... -
配置compass的索引位置为相对路径
2012-09-01 10:49 1323配置compass的索引位置为相对路径: Compass是对 ... -
lucene创建索引
2012-09-01 10:48 1059lucene创建索引: import java.io.Fi ... -
Lucene demo调试运行:
2012-09-01 10:47 1988Lucene demo调试运行: 运行环境: JDK ...
相关推荐
lucene3.0 lucene3.0 lucene3.0 lucene3.0 lucene3.0
Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 ...
lucene 3.0 API中文帮助,学习的人懂得的
Lucene3.0特性Lucene3.0特性
Lucene3.0之查询处理(1):原理和查询类型 各种Query对象详解
NULL 博文链接:https://langxiashahai.iteye.com/blog/703285
lucene3.0 中文分词器, 庖丁解牛
Lucene 3.0 原理与代码分析完整版
lucene3.0 实例,在jdk1.5,lucene3.0下调式通过,可以直接运行。先运行生成索引文件的class,在运行搜索的class。
lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件。
at org.apache.lucene.index.DocInverterPerField.processFields(DocInverterPerField.java:137) at org.apache.lucene.index.DocFieldProcessorPerThread.processDocument(DocFieldProcessorPerThread.java:246) ...
传智播客Lucene3.0课程,Lucene3.0的入门教程.
lucene3.0-highlighter.jar lucene3.0的高亮jar包,从lucene3.0源码中导出来的
Lucene 3.0 原理 Lucene 3.0 原理 Lucene 3.0 原理 Lucene 3.0 原理
全面好用的lucene 2.0 api以及lucene 3.0 api帮助文档
Lucene3.0浅析Lucene3.0浅析Lucene3.0浅析Lucene3.0浅析
基于lucene3.0 书籍查询系统 基于lucene3.0 书籍查询系统
Lucene3.0分词系统.doc
lucene3.0 例子lucene3.0 例子 lucene3.0 例子 ,很好的学习,只有原代原,jar 包自己加上去就OK了
全文检索 lucene 3.0 叶涛 全文检索 lucene 3.0 叶涛 非常好用.上手极快!