下面讲一下索引的建立
其实从上面的例子就可以看出建立索引就用到Document,IndexWriter,Field。
最简单的步骤就是:
首先分别new 一个Document,IndexWriter,Field
然后用Doument.add()方法加入Field,
其次用IndexWrtier.addDocument()方法加入Document。
最后调用一下IndexWriter.close()方法关闭输入索引,这一步非常的重要只有调用这个方法索引才会被写入索引的目录里,而这是被很多
初学的人所忽略的。
Document没有什么好介绍的,把它的作用看成数据库中的一行记录就行。
Field是一个比较重要的也是比较复杂的:
看一下它的构造函数有5个:
Field(String name, byte[] value, Field.Store store)
Field(String name, Reader reader)
Field(String name, Reader reader, Field.TermVector termVector)
Field(String name, String value, Field.Store store, Field.Index index)
Field(String name, String value, Field.Store store, Field.Index index, Field.TermVector termVector)
在Field中有三个内部类:Field.Index,Field.Store,Field.termVector,而构造函数也用到了它们。
注意:termVector是Lucene 1.4新增的它提供一种向量机制来进行模糊查询的这个不常用,默认是false不过是什么对于一般查询无影响。
它们的不同的组合,在全文检索中有着不同的作用。看看下面的表吧:
Field.Index
Field.Store
看本文上面的介绍
说明
TOKENIZED(分词)
YES
文章的标题或内容(如果是内容的话不能太长)是可以被搜索的
TOKENIZED
NO
文章的标题或内容(内容可以很长)也是可以被看过的
NO
YES
这是不能被搜索的,它只是被搜索内容的附属物。如URL等
UN_TOKENIZED
YES/NO
不被分词,它作为一个整体被搜索,搜一部分是搜不出来的
NO
NO
没有这种用法
而对于Field(String name, Reader reader)
Field(String name, Reader reader, Field.TermVector termVector)
**注意**:他们是Field.Index.TOKENIZED和Field.Store.NO的。这就是为什么我们在上面的例子中会出现文章的内容为 null了。因为它只是被索引了,
而并没有被存储下来。如果一定要看到文章的内容的话可以通过文章的路径得到毕竟文章的路径是作为搜索的附属物被搜索出来了。而我们在
Web开发的时候一般是将大数据放在数据库中,不会放在文件系统中,更不会放在索引目录里,因为它太大了操作会加大服务器的负担。
下面介绍一下IndexWriter:
它就是一个写入索引的写入器,它的任务比较简单:
1.用addDocument()将已经准备好写入索引的document们加入
2.调用close()将索引写入索引目录
先看一下它的构造函数:
IndexWriter(Directory d, Analyzer a, boolean create)
IndexWriter(File path, Analyzer a, boolean create)
IndexWriter(String path, Analyzer a, boolean create)
可见构造它需要一个索引文件目录,一个分析器(一般用标准的这个),最后一个参数是标识是否清空索引目录
它有一些设置参数的功能如:设置Field的最大长度
看个例子:
public void IndexMaxField() throws IOException {
IndexWriter indexWriter= new IndexWriter("c:\\\\index",new StandardAnalyzer(),true);
Document doc1 = new Document();
doc1.add(new Field("name1","程序员之家",Field.Store.YES,Field.Index.TOKENIZED));
Document doc2 = new Document();
doc2.add(new Field("name2","Welcome to the Home of programers",Field.Store.YES,Field.Index.TOKENIZED));
indexWriter.setMaxFieldLength(5);
indexWriter.addDocument(doc1);
indexWriter.setMaxFieldLength(3);
indexWriter.addDocument(doc1);
indexWriter.setMaxFieldLength(0);
indexWriter.addDocument(doc2);
indexWriter.setMaxFieldLength(3);
indexWriter.addDocument(doc2);
indexWriter.close();
}
public void SearcherMaxField() throws ParseException, IOException {
Query query = null;
Hits hits = null;
IndexSearcher indexSearcher= null;
QueryParser queryParser= null;
queryParser = new QueryParser("name1",new StandardAnalyzer());
query = queryParser.parse("程序员");
indexSearcher= new IndexSearcher("c:\\\\index");
hits = indexSearcher.search(query);
System.out.println("您搜的是:程序员");
System.out.println("找到了"+hits.length()+"个结果");
System.out.println("它们分别是:");
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println(doc.get("name1"));
}
query = queryParser.parse("程序员之家");
indexSearcher= new IndexSearcher("c:\\\\index");
hits = indexSearcher.search(query);
System.out.println("您搜的是:程序员之家");
System.out.println("找到了"+hits.length()+"个结果");
System.out.println("它们分别是:");
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println(doc.get("name1"));
}
queryParser = new QueryParser("name2",new StandardAnalyzer());
query = queryParser.parse("Welcome");
indexSearcher= new IndexSearcher("c:\\\\index");
hits = indexSearcher.search(query);
System.out.println("您搜的是:Welcome");
System.out.println("找到了"+hits.length()+"个结果");
System.out.println("它们分别是:");
for (int i = 0; i < hits.length(); i++){
Document doc = hits.doc(i);
System.out.println(doc.get("name2"));
}
query = queryParser.parse("the");
indexSearcher= new IndexSearcher("c:\\\\index");
hits = indexSearcher.search(query);
System.out.println("您搜的是:the");
System.out.println("找到了"+hits.length()+"个结果");
System.out.println("它们分别是:");
for (int i = 0; i < hits.length(); i++){
Document doc = hits.doc(i);
System.out.println(doc.get("name2"));
}
query = queryParser.parse("home");
indexSearcher= new IndexSearcher("c:\\\\index");
hits = indexSearcher.search(query);
System.out.println("您搜的是:home");
System.out.println("找到了"+hits.length()+"个结果");
System.out.println("它们分别是:");
for (int i = 0; i < hits.length(); i++){
Document doc = hits.doc(i);
System.out.println(doc.get("name2"));
}
}
它的运行结果为:
总结一下:
1.设置Field的长度限制只是限制了搜索。如果用了Field.Store.YES的话还是会全部被保存进索引目录里的。
2.为什么搜the没有搜出来呢是因为lucene分析英文的时候不会搜索the to of 等无用的词(搜这些词是无意义的)。
3.New StandardAnlayzer()对于英文的分词是按空格和一些无用的词,而中文呢是全部的单个的字。
4.设置Field的最大长度是以0开头和数组一样。
程序员之家----------3--------程序员之
0 1 2 3
Welcome to the home of programmers------3------Welcome to the home of programmers
0 1 2
大家还可以试一下别的,以便加深一下印象,到现在我们已经可以用lucene建立索引了
分享到:
相关推荐
本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记本人的Lucene2.9学习笔记本人的Lucene2.9学习笔记 本人的Lucene2.9学习笔记
lucene学习笔记,lucene入门必备材料
很好的Lucene学习入门资料。lucene是纯java开发的,支持索引的建立和搜索
第一章 LUCENE基础 2 1.1 索引部分的核心类 2 1.2 分词部分的核心类 2 1.3 搜索部分的核心类 2 第二章 索引建立 3 2.1 创建Directory 3 2.2 创建Writer 3 2.3 创建文档并且添加索引 4 2.4 查询索引的基本信息 5 2.5 ...
lucene学习笔记 1 .txt lucene学习笔记 2.txt lucene学习笔记 3 .txt lucene入门实战.txt Lucene 的学习 .txt Lucene-2.0学习文档 .txt Lucene入门与使用 .txt lucene性能.txt 大富翁全文索引和查询的例子...
NULL 博文链接:https://kylinsoong.iteye.com/blog/719415
lucene基础学习笔记&源码
NULL 博文链接:https://menglh.iteye.com/blog/347467
介绍lucene3.5的相关技术,包括基本用法、分析器、索引建立与查询,扩展的高亮、分页、以及solr3.5的相关用法
这是本人学习lucene时候做的笔记,内容较详细,通俗易懂,是入门Lucene的好帮手!欢迎下载!
lucene相关学习资料,包括lucene学习笔记,lucene添加中文分词等
NULL 博文链接:https://lpf.iteye.com/blog/1440160
主要包含Lucene.net 学习笔记和 Lucene.net 系列的代码,一直一些简单的程序