<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IT元素 &#187; Lucene</title>
	<atom:link href="http://www.ourys.com/tags/lucene/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ourys.com</link>
	<description>Hector的编程笔记夹，如果喜欢，收藏一个</description>
	<lastBuildDate>Wed, 18 Jan 2012 22:07:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Lucene3.0的主要变化</title>
		<link>http://www.ourys.com/post/lucene3-0_about.html</link>
		<comments>http://www.ourys.com/post/lucene3-0_about.html#comments</comments>
		<pubDate>Sat, 10 Apr 2010 16:06:27 +0000</pubDate>
		<dc:creator>Hector</dc:creator>
				<category><![CDATA[垂直搜索]]></category>
		<category><![CDATA[Lucene]]></category>

		<guid isPermaLink="false">http://www.ourys.com/hector/ourys.com/?p=182</guid>
		<description><![CDATA[<p>一、概述 &#160;&#160;Lucene3.0（以下简称3.0）已于2009-11-25发布，3.0版本是重大的版本，改动很大。在API上做了很多的调整，已经删除了很多之前废弃的方法以及类，并支持了很多Java5 的新特性：包括泛型、可变参数、枚举和autoboxing等。&#160;&#160; 因此，此版本和2.x版本不能兼容，如要使用3.0版本，最好是在新项目中去使用，而不是去升级2.</p>
]]></description>
			<content:encoded><![CDATA[<h2>一、概述</h2>
<p class="bleft"><span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span>3.0（以下简称3.0）已于2009-11-25发布，3.0版本是重大的版本，改动很大。在API上做了很多的调整，已经删除了很多之前废弃的方法以及类，并支持了很多Java5 的新特性：包括泛型、可变参数、枚举和autoboxing等。</p>
<p>因此，此版本和2.x版本不能兼容，如要使用3.0版本，最好是在新项目中去使用，而不是去升级2.x或之前的版本！</p>
<h2>二、2.9版本介绍</h2>
<p class="bleft">
<p>由于新版本变动很大，官方是不推荐从旧版本升级到新版本的。因为改动会很大。<br />
其实在2.9版本时改动就很大，因为2.9版本就是为3.0做准备的，但是为了向下兼容，2.9并没有抛弃之前的旧方法，所以可以直接向下兼容。2.9版本主要是在性能方面的优化，包括在<span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span>对Lucene底层的内部结构改进、索引的管理方式等多个方面。</p>
<h3>1、索引文件改进</h3>
<p class="bleft">Lucene的索引数据是存放在独立的文件中的，这些文件就是存储着索引数据库一些列分离的“片段”。当我们想索引中增加文档时，便会不断的创建一些可以合并的新片段，因为读写文件的开销比较大，因此这些字段信息Lucene并非每次都直接加到索引文件里面去，而是先缓存，等到一定量的时候再一次写到文件中。在2.9以后，Lucene会为每个片段分别管理FieldCache以此避开跨片段加载FieldCatch的需求，这样就解决了Lucene跨片段加载FieldCatch的效率很低下问题，这个改动大为提高了性能。Lucid Imagination的Mark Miller运行了一个简单的性能测试，表明在5,000,000个不同字符串下的情况下，Lucene 相对于2.4版本会获得15倍左右的性能提高： Lucene 2.4: 150.726s Lucene 2.9: 9.695s</p>
<h3>2、重开搜索</h3>
<p class="bleft">新版本引入了IndexWriter.getReader()方法，它可用于搜索目前完整的索引，包括当前IndexWriter会话中还没有提交的改变，这带来了接近于实时搜索的能力。此外，你还可以调用IndexWriter.setMergedSegmentWarmer()方法进行“预热”，这样那些片断便可以立即投入使用了。</p>
<h3>3、数字处理</h3>
<p class="bleft">2.9版本之前的版本，都是基于文本搜索的，因为对于很多数字的处理方式就很头疼，例如在我们项目中遇到的很多问题都是由于把数字当作了文本处理出现的BUG：1、搜索价格的5，把包含.5的也搜索出来了；2、排序（降序）时，把800排到5000前面；……这些都是由于Lucene把所有的都作为文本处理的方式造成的问题。Lucene 2.9以后已经自带对数字的处理方式。Field和Query类会采取合适的精度进行索引和搜索，这样大大降低了需要搜索的关键字数量，使查询的响应能力得以显著提高。</p>
<h3>4、其他优化</h3>
<p class="bleft">引入了新的查询类型和适用性更广的多关键字查询（通配、前缀等等）方式，以及新的针对波斯语，阿拉伯语及中文的分析器。此外，这次更新还包括更好Unicode支持，一个新的查询分析框架，以及对地理位置的查询，它允许根据距离信息对文档进行过滤和排序（如“找出我家5公里范围内的所有超市”）。</p>
<h2>三、2.9版本和3.0比较</h2>
<p class="bleft">虽然2.9是为3.0做准备的一个版本，但是3.0和2.9相比较，变化还是比较大的，这处要体现在：</p>
<p class="bleft">
<ol>
<li>1、3.0抛弃了在2.9声明废弃的方法，因此3.0无法向下兼容；</li>
<li>2、3.0放弃了对Java1.4的支持，改为对高版本Java1.5和ant 1.7.0支持；</li>
<li>3、其他内核的一些变化，如oalLock.isLocked（）现在会抛出IOException异常，对于一些静态变量的改变等。</li>
</ol>
<h2>四、3.0主要方法的改变</h2>
<p class="bleft">这里这种讲下目前新版本后使用建立索引以及搜索的不同。</p>
<h3>1、建立索引</h3>
<p class="bleft">新版本在建立索引时抛弃了很多未用的方法，见下图，所有声明被抛弃的IndexWriter构造函数都在3.0中被删除。<img src="http://www.ourys.com/upload/2010/4/201004101612328067.jpg" alt="" /></p>
<p class="bleft">3.0版本的IndexWriter构造函数：</p>
<p class="bleft"><img src="http://www.ourys.com/upload/2010/4/201004101612417280.jpg" alt="" /></p>
<p class="bleft">在增加索引时，每个field的常量也有改变，具体如下：</p>
<h3>2、查询</h3>
<p class="bleft">删除了Hits类，增加了TopScoreDocCollector去取得“Hits”，实际上在3.0给了个新命名：collector。使用方式和hits类同，同时删除了Search以及QueryParser的几个构造方法，QueryParser删除了 QueryParser(String f, Analyzer a)构造方法。<br />
新查询例子如下（蓝色部分是与以往不同的部分）：[CODE=java]<br />
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field,<br />
new StandardAnalyzer(Version.LUCENE_CURRENT));<br />
Query query = parser.parse(q);<br />
TopScoreDocCollector collector = TopScoreDocCollector.create(100, true);<br />
IndexSearcher is = new IndexSearcher(FSDirectory.open(file), true);<br />
is.search(query, collector);<br />
ScoreDoc[] docs = collector.topDocs().scoreDocs;<br />
for (int i = 0; i &lt; docs.length; i++) {<br />
Document doc = is.doc(docs[i].doc);// new method is.doc()<br />
System.out.println(doc.getField(“name”) + ”   ” + docs[i].toString() + ”  “);<br />
}</p>
<p>[/CODE]</p>
<p class="bleft">3.0版本的Search构造方法：</p>
<p class="bleft"><img src="http://www.ourys.com/upload/2010/4/201004101612491667.jpg" alt="" /></p>
<p class="bleft">3.0前的构造方法：</p>
<p class="bleft"><img src="http://www.ourys.com/upload/2010/4/201004101613183326.jpg" alt="" /></p>
<h2>五、3.0总体图</h2>
<p class="bleft">3.0版本的结构和之前的版本（2.9之前）相比，在程序结构上表现出来就只是多了一个message包，用来专门处理国际化。 <img src="http://www.ourys.com/upload/2010/4/201004101613284237.jpg" alt="" /></p>
<p class="bleft">可以看到，3.0和之前的版本一样还是由对外接口、索引核心以及基础结构封装三大部分共八个模块（也即包package），详细介绍详见附件一。 	我们从上图也可以看到Lucene搜索时的调用关系：当我们要查询一个词时，在查询模块（search）会先调用语法分析器（queryParser）对查询语句进行分析，语法分析模块调用了词法分析器（analysis）进行词法分析，如对搜索关键字分词、过滤等，词法分析器在使用时会根据实际情况调用国际化模块（message）进行一些国际化的处理。当这些前置工作做完之后，才真正进入到搜索核心，首先会调用索引模块（index）,它负责向底层的存储类（store）去读取索引文件里面的数据，然后返回给查询模块。其他模块在整个搜索过程中是作为公共类存在的。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ourys.com/post/lucene3-0_about.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lucene3.0学习笔记3（给数据库建立索引）</title>
		<link>http://www.ourys.com/post/lucene3-0_database_index.html</link>
		<comments>http://www.ourys.com/post/lucene3-0_database_index.html#comments</comments>
		<pubDate>Mon, 22 Mar 2010 05:15:15 +0000</pubDate>
		<dc:creator>Hector</dc:creator>
				<category><![CDATA[垂直搜索]]></category>
		<category><![CDATA[Lucene]]></category>

		<guid isPermaLink="false">http://www.ourys.com/hector/ourys.com/?p=168</guid>
		<description><![CDATA[<p>&#160;给数据库字段建立索引的方法和给文件建立索引的方法类似。（可见这篇文章：） [Lucene3.0学习笔记1（建立索引）]只是需要将待索引的源换为从数据库里面读取的字段值就可以了。代码中用到的数据库操作类在这里：[java通用数据库操作类]请对照&#160;[Lucene3.0学习笔记1（建立索引）]加以理解。代码如下：</p>
]]></description>
			<content:encoded><![CDATA[<p>&nbsp;给数据库字段建立索引的方法和给文件建立索引的方法类似。（可见这篇文章：） [<a target="_blank" href="http://www.ourys.com/post/lucene_create_index.html">Lucene3.0学习笔记1（建立索引）</a>]</p>
<p>只是需要将待索引的源换为从数据库里面读取的字段值就可以了。</p>
<p>代码中用到的数据库操作类在这里：[<a target="_blank" href="http://www.ourys.com/post/java_databass_class.html">java通用数据库操作类</a>]</p>
<p>请对照&nbsp;[<a target="_blank" href="http://www.ourys.com/post/lucene_create_index.html">Lucene3.0学习笔记1（建立索引）</a>]加以理解。</p>
<p>代码如下：</p>
<p class="bleft">[CODE=java]<br/>package com.hector.firstlucene;<br/><br/>import java.io.File;<br/>import java.io.IOException;<br/>import java.sql.ResultSet;<br/>import java.sql.SQLException;<br/>import java.util.Date;<br/><br/>import org.apache.lucene.analysis.standard.StandardAnalyzer;<br/>import org.apache.lucene.document.DateTools;<br/>import org.apache.lucene.document.Document;<br/>import org.apache.lucene.document.Field;<br/>import org.apache.lucene.index.IndexWriter;<br/>import org.apache.lucene.store.Directory;<br/>import org.apache.lucene.store.SimpleFSDirectory;<br/>import org.apache.lucene.util.Version;<br/><br/><br/><br/>/**********************<br/>*<br/>* @author Hector<br/>* 建立数据库索引 lucene3.0+<br/>*/<br/><br/>public class DataBaseIndexer{<br/>	public static void main(String[] args) throws IOException,SQLException{<br/>		String indexDir = “d:\\lucene\\index”;<br/>		DBConn conn = new DBConn();<br/>		conn.OpenConnection();<br/>		ResultSet rs = conn.ExecuteQuery(“select * from Article”);<br/>		// 为表字段建立索引    <br/>		Directory dir = new SimpleFSDirectory(new File(indexDir));    <br/>		IndexWriter indexWriter = new IndexWriter(dir,    <br/>		new StandardAnalyzer(Version.LUCENE_30), true,    <br/>		IndexWriter.MaxFieldLength.UNLIMITED);    <br/>		while (rs.next()) {    <br/>			System.out.println(rs.getString(“Article_Title”));    <br/>			Document doc = new Document();    <br/>			doc.add(new Field(“Article_Title”, rs.getString(“Article_Title”),Field.Store.YES, Field.Index.ANALYZED));    <br/>			doc.add(new Field(“Article_Content”, rs.getString(“Article_Content”),Field.Store.YES, Field.Index.ANALYZED));<br/>			doc.add(new Field(“indexDate”,DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES,Field.Index.NOT_ANALYZED));   <br/>			indexWriter.addDocument(doc); <br/>		}<br/>		System.out.println(“numDocs”+indexWriter.numDocs());   <br/>        indexWriter.close();   <br/>	}<br/>}<br/>[/CODE]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ourys.com/post/lucene3-0_database_index.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lucene3.0学习笔记2（查询索引）</title>
		<link>http://www.ourys.com/post/lucene3-0_learning2.html</link>
		<comments>http://www.ourys.com/post/lucene3-0_learning2.html#comments</comments>
		<pubDate>Sat, 20 Mar 2010 14:14:29 +0000</pubDate>
		<dc:creator>Hector</dc:creator>
				<category><![CDATA[垂直搜索]]></category>
		<category><![CDATA[Lucene]]></category>

		<guid isPermaLink="false">http://www.ourys.com/hector/ourys.com/?p=164</guid>
		<description><![CDATA[<img alt="Lucene3" align="right" width="201" height="53" src="http://www.ourys.com/upload/2010/3/Lucene.jpg" /><font color="Blue">d:\lucene\index</font>是上一篇学习笔记（[Lucene3.0学习笔记1（建立索引）] ）中生成的索引文件的存放地址。具体步骤简介如下：<br/>1、创建Directory对象，索引文件夹<br/>2、创建IndexSearch对象，建立查询（参数是Directory对象）<br/>3、创建QueryParser对象（lucene版本，查询Field
]]></description>
			<content:encoded><![CDATA[<p><font color="Blue">d:\lucene\index</font>是上一篇学习笔记（[<a href="http://www.ourys.com/post/lucene_create_index.html" target="_blank">Lucene3.0学习笔记1（建立索引）</a>] ）中生成的索引文件的存放地址。具体步骤简介如下：</p>
<p class="bbox">1、创建Directory对象，索引文件夹<br /><br/>2、创建IndexSearch对象，建立查询（参数是Directory对象）<br /><br/>3、创建QueryParser对象（lucene版本，查询Field字段，所用分词器）<br /><br/>4、生成Query对象，由QueryParser对象的parse函数生成（参数是所查的关键字）<br /><br/>5、建立TopDocs对象（IndexSearch的search函数，参数是Query查询对象，）<br /><br/>6、TopDocs对象数组里存放查询信息<br /><br/>7、关闭IndexSearch</p>
<p>&nbsp;</p>
<p>源码：</p>
<p>[CODE=java]<br/>package com.hector.firstlucene;<br/><br/>import java.io.File;   <br/>import java.io.IOException;   <br/>  <br/>import org.apache.lucene.analysis.standard.StandardAnalyzer;   <br/>import org.apache.lucene.document.Document;   <br/>import org.apache.lucene.queryParser.ParseException;   <br/>import org.apache.lucene.queryParser.QueryParser;   <br/>import org.apache.lucene.search.IndexSearcher;   <br/>import org.apache.lucene.search.Query;   <br/>import org.apache.lucene.search.ScoreDoc;   <br/>import org.apache.lucene.search.TopDocs;   <br/>import org.apache.lucene.store.Directory;   <br/>import org.apache.lucene.store.SimpleFSDirectory;   <br/>import org.apache.lucene.util.Version;   <br/>/**  <br/> * 搜索索引 <span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span> 3.0+  <br/> * @author Hector <br/> *  <br/> */  <br/>public class Searcher {   <br/>  <br/>    public static void main(String[] args) throws IOException, ParseException {   <br/>        //保存索引文件的地方   <br/>        String indexDir = “d:\\lucene\\index”;   <br/>        Directory dir = new SimpleFSDirectory(new File(indexDir));   <br/>        //创建 IndexSearcher对象，相比IndexWriter对象，这个参数就要提供一个索引的目录就行了   <br/>        IndexSearcher indexSearch = new IndexSearcher(dir);   <br/>        //创建QueryParser对象,第一个参数表示<span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span>的版本,第二个表示搜索Field的字段,第三个表示搜索使用分词器   <br/>        QueryParser queryParser = new QueryParser(Version.LUCENE_30,   <br/>                “contents”, new StandardAnalyzer(Version.LUCENE_30));   <br/>        //生成Query对象   <br/>        Query query = queryParser.parse(“好”);   <br/>        //搜索结果 TopDocs里面有scoreDocs[]数组，里面保存着索引值   <br/>        TopDocs hits = indexSearch.search(query, 10);   <br/>        //hits.totalHits表示一共搜到多少个   <br/>        System.out.println(“找到了”+hits.totalHits+”个”);   <br/>        //循环hits.scoreDocs数据，并使用indexSearch.doc方法把Document还原，再拿出对应的字段的值   <br/>        for (int i = 0; i < hits.scoreDocs.length; i++) {   <br/>            ScoreDoc sdoc = hits.scoreDocs[i];   <br/>            Document doc = indexSearch.doc(sdoc.doc);   <br/>            System.out.println(doc.get(“filename”));               <br/>        }          <br/>        indexSearch.close();   <br/>    }   <br/>}  <br/><br/>[/CODE]</p>
<p>代码中Lucene相关名词：[<a target="_blank" href="http://www.ourys.com/post/Lucene_Name_About.html">Lucene相关名词解释</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ourys.com/post/lucene3-0_learning2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lucene3.0包详细介绍</title>
		<link>http://www.ourys.com/post/Lucene_package_introduction.html</link>
		<comments>http://www.ourys.com/post/Lucene_package_introduction.html#comments</comments>
		<pubDate>Fri, 19 Mar 2010 20:39:17 +0000</pubDate>
		<dc:creator>Hector</dc:creator>
				<category><![CDATA[垂直搜索]]></category>
		<category><![CDATA[Lucene]]></category>

		<guid isPermaLink="false">http://www.ourys.com/hector/ourys.com/?p=163</guid>
		<description><![CDATA[<p><img alt="lucene" align="right" width="200" height="64" src="http://www.ourys.com/upload/2010/3/Lucene.jpg" />1、analysisAnalysis包含一些内建的分析器，例如按空白字符分词的WhitespaceAnalyzer，添加了stopwrod过滤的StopAnalyzer，最常用的是StandardAnalyzer。2、documentDocument包含文档的数据结构，例如Document类定义了存储文档的数据结构，Field类定义了Document的一个域。3、indexIndex包含了索引的读</p>
]]></description>
			<content:encoded><![CDATA[<h3>1、analysis</h3>
<p class="bbox">Analysis包含一些内建的分析器，例如按空白字符分词的WhitespaceAnalyzer，添加了stopwrod过滤的StopAnalyzer，最常用的是StandardAnalyzer。</p>
<h3>2、document</h3>
<p class="bbox">Document包含文档的数据结构，例如Document类定义了存储文档的数据结构，Field类定义了Document的一个域。</p>
<h3>3、index</h3>
<p class="bbox">Index包含了索引的读写类，例如对索引文件的segment进行写、合并、优化的IndexWriter类和对索引进行读取和删除操作的 IndexReader类，这里要注意的是不要被IndexReader这个名字误导，以为它是索引文件的读取类，实际上删除索引也是由它完成， IndexWriter只关心如何将索引写入一个个segment，并将它们合并优化；IndexReader则关注索引文件中各个文档的组织形式。</p>
<h3>4、queryParser</h3>
<p class="bbox">QueryParser包含了解析查询语句的类，lucene的查询语句和sql语句有点类似，有各种保留字，按照一定的语法可以组成各种查询。 <span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span>有很多种Query类，它们都继承自Query，执行各种特殊的查询，QueryParser的作用就是解析查询语句，按顺序调用各种 Query类查找出结果。</p>
<h3>5、search</h3>
<p class="bbox">Search包含了从索引中搜索结果的各种类，例如刚才说的各种Query类，包括TermQuery、BooleanQuery等就在这个包里。</p>
<h3>6、store</h3>
<p class="bbox">Store包含了索引的存储类，例如Directory定义了索引文件的存储结构，FSDirectory为存储在文件中的索引，RAMDirectory为存储在内存中的索引，MmapDirectory为使用内存映射的索引。</p>
<h3>7、util</h3>
<p class="bbox">Util包含一些公共工具类，例如时间和字符串之间的转换工具。</p>
<h3>8、message</h3>
<p class="bbox">处理国际化的类。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ourys.com/post/Lucene_package_introduction.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lucene3.0学习笔记1（建立索引）</title>
		<link>http://www.ourys.com/post/lucene_create_index.html</link>
		<comments>http://www.ourys.com/post/lucene_create_index.html#comments</comments>
		<pubDate>Thu, 18 Mar 2010 22:42:29 +0000</pubDate>
		<dc:creator>Hector</dc:creator>
				<category><![CDATA[垂直搜索]]></category>
		<category><![CDATA[Lucene]]></category>

		<guid isPermaLink="false">http://www.ourys.com/hector/ourys.com/?p=162</guid>
		<description><![CDATA[<p><img alt="Lucene3" align="right" width="201" height="53" src="http://www.ourys.com/upload/2010/3/Lucene.jpg" />我们首先在<font color="Blue">d:\lucene\s</font>下放置了几个txt文件作为索引的源。创建<font color="Blue">d:\lucene\index</font>作为索引文件的存放地址。[CODE=java] package com.hector.firstlucene; /********************** *</p>
]]></description>
			<content:encoded><![CDATA[<p>我们首先在<font color="Blue">d:\lucene\s</font>下放置了几个txt文件作为索引的源。创建<font color="Blue">d:\lucene\index</font>作为索引文件的存放地址。当然还需要引入lucene3.0的包。具体步骤简介如下：
<p class="bbox">1、创建Directory对象（参数是存放索引的File类型，根据File的存放地点选择创建类）<br/><br/>2、创建indexWriter对象，参数（Directory对象，分词器，是否创建，分词的最大值）<br/><br/>3、获取源文件的File数组<br/>4、通过循环将每个文件写入索引。<br/>{<br/>创建Document对象，并创建Field对象（列名称（文件名、内容等）），将Field加入到Dcument中，通过IndexWriter.addDocument(Document)写入索引中。<br/>}<br/><br/>5、关闭indexWriter。</p>
</p>
<p>源码：</p>
<p>[CODE=java] <br/>package com.hector.firstlucene;<br/>/**********************<br/> *<br/> * @author Hector<br/> * 建立索引 lucene3.0<br/> */<br/><br/>import java.io.File;   <br/>import java.io.FileReader;   <br/>import java.io.IOException;   <br/>import java.util.Date;   <br/>  <br/>import org.apache.lucene.analysis.standard.StandardAnalyzer;   <br/>import org.apache.lucene.document.DateTools;   <br/>import org.apache.lucene.document.Document;   <br/>import org.apache.lucene.document.Field;   <br/>import org.apache.lucene.index.IndexWriter;   <br/>import org.apache.lucene.store.Directory;   <br/>import org.apache.lucene.store.SimpleFSDirectory;   <br/>import org.apache.lucene.util.Version;   <br/><br/>public class TextFileIndexer {   <br/>  <br/>    /**  <br/>     * @param args  <br/>     * @throws IOException   <br/>     */  <br/>    public static void main(String[] args) throws IOException {   <br/>        //保存索引文件的地方   <br/>        String indexDir = “d:\\lucene\\index”;   <br/>        //将要搜索TXT文件的地方   <br/>        String dateDir = “d:\\lucene\\s”;   <br/>        IndexWriter indexWriter = null;   <br/>        //创建Directory对象 ，FSDirectory代表待索引的文件存在磁盘上  <br/>        Directory dir = new SimpleFSDirectory(new File(indexDir));   <br/>        //创建IndexWriter对象,第一个参数是Directory,第二个是分词器,第三个表示是否是创建,如果为false为在此基础上面修改,第四表示表示分词的最大值，比如说new MaxFieldLength(2)，就表示两个字一分，一般用IndexWriter.MaxFieldLength.LIMITED    <br/>        indexWriter = new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);   <br/>        File[] files = new File(dateDir).listFiles();   <br/>        for (int i = 0; i < files.length; i++) {   <br/>            Document doc = new Document();   <br/>            //创建Field对象，并放入doc对象中   <br/>            doc.add(new Field(“contents”, new FileReader(files[i])));    <br/>            doc.add(new Field(“filename”, files[i].getName(),    <br/>                                Field.Store.YES, Field.Index.NOT_ANALYZED));   <br/>            doc.add(new Field(“indexDate”,DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES,Field.Index.NOT_ANALYZED));   <br/>            //写入IndexWriter   <br/>            indexWriter.addDocument(doc);   <br/>        }   <br/>        //查看IndexWriter里面有多少个索引   <br/>        System.out.println(“numDocs”+indexWriter.numDocs());   <br/>        indexWriter.close();   <br/>           <br/>    }   <br/>  <br/>}  <br/><br/>[/CODE]</p>
<p>代码中<span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span>相关名词：[<a target="_blank" href="http://www.ourys.com/post/Lucene_Name_About.html">Lucene相关名词解释</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ourys.com/post/lucene_create_index.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lucene相关名词解释</title>
		<link>http://www.ourys.com/post/Lucene_Name_About.html</link>
		<comments>http://www.ourys.com/post/Lucene_Name_About.html#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:18:43 +0000</pubDate>
		<dc:creator>Hector</dc:creator>
				<category><![CDATA[垂直搜索]]></category>
		<category><![CDATA[Lucene]]></category>

		<guid isPermaLink="false">http://www.ourys.com/hector/ourys.com/?p=161</guid>
		<description><![CDATA[<p><img alt="Lucene" align="right" src="http://www.ourys.com/upload/2010/3/Lucene.jpg" />1、IndexWriter lucene中最重要的的类之一，它主要是用来将文档加入索引，同时控制索引过程中的一些参数使用。2、Analyzer分析器,主要用于分析搜索引擎遇到的各种文本。常用的有StandardAnalyzer分析器,StopAnalyzer分析器,WhitespaceAnalyzer分析器等。3、Directory &#62;索引存放的位置;lucene提供了两种索引存放的位置，一种</p>
]]></description>
			<content:encoded><![CDATA[<h3>1、IndexWriter</h3>
<p class="bbox">lucene中最重要的的类之一，它主要是用来将文档加入索引，同时控制索引过程中的一些参数使用。</p>
<h3>2、Analyzer</h3>
<p class="bbox">分析器,主要用于分析搜索引擎遇到的各种文本。常用的有StandardAnalyzer分析器,StopAnalyzer分析器,WhitespaceAnalyzer分析器等。</p>
<h3>3、Directory</h3>
<p class="bbox">&gt;索引存放的位置;lucene提供了两种索引存放的位置，一种是磁盘，一种是内存。一般情况将索引放在磁盘上；相应地lucene提供了FSDirectory和RAMDirectory两个类。</p>
<h3>4、Document</h3>
<p class="bbox">文档;Document相当于一个要进行索引的单元，任何可以想要被索引的文件都必须转化为Document对象才能进行索引。</p>
<h3>5、Field</h3>
<p class="bbox">字段。</p>
<h3>6、IndexSearcher</h3>
<p class="bbox">是lucene中最基本的检索工具，所有的检索都会用到IndexSearcher工具;</p>
<h3>7、Query</h3>
<p class="bbox">查询，lucene中支持模糊查询，语义查询，短语查询，组合查询等等,如有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些类。</p>
<h3>8、QueryParser</h3>
<p class="bbox">是一个解析用户输入的工具，可以通过扫描用户输入的字符串，生成Query对象。</p>
<h3>9、Hits</h3>
<p class="bbox">在搜索完成之后，需要把搜索结果返回并显示给用户，只有这样才算是完成搜索的目的。在<span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span>中，搜索的结果的集合是用Hits类的实例来表示的。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ourys.com/post/Lucene_Name_About.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lucene简介</title>
		<link>http://www.ourys.com/post/Lucene_About.html</link>
		<comments>http://www.ourys.com/post/Lucene_About.html#comments</comments>
		<pubDate>Fri, 12 Mar 2010 11:07:28 +0000</pubDate>
		<dc:creator>Hector</dc:creator>
				<category><![CDATA[垂直搜索]]></category>
		<category><![CDATA[Lucene]]></category>

		<guid isPermaLink="false">http://www.ourys.com/hector/ourys.com/?p=160</guid>
		<description><![CDATA[<p><img alt="" align="right" onload="ResizeImage(this,520)" src="http://www.ourys.com/upload/2010/3/Lucene.jpg" />Lucene官网：&#160;简介：&#160;&#160;&#160; Lucene是apache软件基金会[4] jakarta项目组的一个子项目，是一个开放源代码[5]的全文检索引擎工具包，即它不是一个完整的全文检索引擎，而是一个全文检索引擎的架构，提供了完整的查询引擎和索引引擎，部分文本分析引擎（英文与德文两种西方语言）。Lucene的目的是为软件开发人员提供一个简单易用的工具包，以方便的</p>
]]></description>
			<content:encoded><![CDATA[<p><strong><span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span>官网：</strong></p>
<p>
<dd style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; word-wrap: break-word; color: rgb(102,102,102); word-break: break-all; padding-top: 0px">1.<a style="color: rgb(51,102,204); text-decoration: underline" target="_blank" href="http://lucene.apache.org/">http://lucene.apache.org/</a><span class="Apple-converted-space">&nbsp;</span>官方站点 </dd>
<dd style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; word-wrap: break-word; color: rgb(102,102,102); word-break: break-all; padding-top: 0px">2.<a style="color: rgb(51,102,204); text-decoration: underline" target="_blank" href="http://lucene.com.cn/">http://lucene.com.cn/</a><span class="Apple-converted-space">&nbsp;</span>LUCENE 中国 </dd>
</p>
<p><strong><span style="font-family: 幼圆"><span style="font-size: medium"><span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="text-align: left; line-height: 22px" class="Apple-style-span">简介：</span></span></span></span></strong></p>
<p><span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="text-align: left; line-height: 22px; font-family: Arial; font-size: 14px" class="Apple-style-span">&nbsp;&nbsp;&nbsp; <span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span>是apache软件基金会[4] jakarta项目组的一个子项目，是一个开放源代码[5]的全文检索引擎工具包，即它不是一个完整的全文检索引擎，而是一个全文检索引擎的架构，提供了完整的查询引擎和索引引擎，部分文本分析引擎（英文与德文两种西方语言）。<span class='wp_keywordlink_affiliate'><a href="http://www.ourys.com/tags/lucene" title="查看 Lucene 中的全部文章" target="_blank">Lucene</a></span>的目的是为软件开发人员提供一个简单易用的工具包，以方便的在目标系统中实现全文检索的功能，或者是以此为基础建立起完整的全文检索引擎。<br />　　Lucene的原作者是Doug Cutting，他是一位资深全文索引/检索专家，曾经是V-Twin搜索引擎[6]的主要开发者，后在Excite[7]担任高级系统架构设计师，目前从事于一些Internet底层架构的研究。早先发布在作者自己的http://www.lucene.com/，后来发布在SourceForge[8]，2001年年底成为apache软件基金会jakarta的一个子项目：<a href="http://jakarta.apache.org/lucene/">http://jakarta.apache.org/lucene/</a>。</span></span></p>
<p><span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="text-align: left; line-height: 22px; font-family: Arial; font-size: 14px" class="Apple-style-span"><span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="text-align: left; line-height: 24px; font-family: Arial; font-size: 14px" class="Apple-style-span">
<p>优势：</p>
<p>（1）索引文件格式独立于应用平台。<br />（2）在传统全文检索引擎的倒排索引的基础上，实现了分块索引，能够针对新的文件建立小文件索引，提升索引速度。</p>
<p>（3）优秀的面向对象的系统架构，使得对于Lucene扩展的学习难度降低，方便扩充新功能。<br />（4）设计了独立于语言和文件格式的文本分析接口，索引器通过接受Token流完成索引文件的创立，用户扩展新的语言和文件格式，只需要实现文本分析的接口。</p>
<p>（5）已经默认实现了一套强大的查询引擎，用户无需自己编写代码即使系统可获得强大的查询能力，Lucene的查询实现中默认实现了布尔操作、模糊查询（Fuzzy Search[11]）、分组查询等等。</p>
<p><strong>关于亚洲语言的的切分词问题</strong><br class="Apple-interchange-newline" />&nbsp;</p>
<p></span></span></span></span></p>
<p><span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="text-align: left; line-height: 21px; font-family: 'trebuchet ms', verdana, helvetica, arial, sans-serif; color: rgb(51,51,51); font-size: 14px" class="Apple-style-span"><br />
<table style="width: 618px; height: 230px" border="1" width="618">
<tbody>
<tr>
<td style="width: 77px; height: 18px" align="center">            &nbsp;</td>
<td style="width: 280px; height: 18px" align="center">自动切分</td>
<td style="width: 361px; height: 18px" align="center">词表切分</td>
</tr>
<tr>
<td style="width: 77px; height: 16px">实现</td>
<td style="width: 280px; height: 16px">实现非常简单</td>
<td style="width: 361px; height: 16px">实现复杂</td>
</tr>
<tr>
<td style="width: 77px; height: 16px">查询</td>
<td style="width: 280px; height: 16px">增加了查询分析的复杂程度，</td>
<td style="width: 361px; height: 16px">适于实现比较复杂的查询语法规则</td>
</tr>
<tr>
<td style="width: 77px; height: 16px">存储效率</td>
<td style="width: 280px; height: 16px">索引冗余大，索引几乎和原文一样大</td>
<td style="width: 361px; height: 16px">索引效率高，为原文大小的30％左右</td>
</tr>
<tr>
<td style="width: 77px; height: 16px">维护成本</td>
<td style="width: 280px; height: 16px">无词表维护成本</td>
<td style="width: 361px; height: 16px">词表维护成本非常高：中日韩等语言需要分别维护。<br />            还需要包括词频统计等内容</td>
</tr>
<tr>
<td style="width: 77px; height: 16px">适用领域</td>
<td style="width: 280px; height: 16px">嵌入式系统：运行环境资源有限<br />            分布式系统：无词表同步问题<br />            多语言环境：无词表维护成本</td>
<td style="width: 361px; height: 16px">对查询和存储效率要求高的专业搜索引擎<br />            &nbsp;</td>
</tr>
</tbody>
</table>
<p></span></span>&nbsp;</p>
<p style="margin-top: 10px; margin-bottom: 10px"><strong>安装和使用</strong></p>
<p style="margin-top: 10px; margin-bottom: 10px">下载：<a style="color: rgb(49,101,206); text-decoration: underline" href="http://jakarta.apache.org/lucene/">http://jakarta.apache.org/lucene/</a></p>
<p style="margin-top: 10px; margin-bottom: 10px">注意：Lucene中的一些比较复杂的词法分析是用JavaCC生成的（JavaCC：JavaCompilerCompiler，纯Java的词法分析生成器），所以如果从源代码编译或需要修改其中的QueryParser<br />
、定制自己的词法分析器，还需要从<a style="color: rgb(49,101,206); text-decoration: underline" href="https://javacc.dev.java.net/">https://javacc.dev.java.net/</a>下载javacc。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ourys.com/post/Lucene_About.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

