??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品美女一区二区三区 ,久久亚洲精品视频,欧美理论在线观看http://www.aygfsteel.com/maguangjun/category/35629.htmlzh-cnWed, 03 Dec 2008 09:00:48 GMTWed, 03 Dec 2008 09:00:48 GMT60Lucene软g包分?/title><link>http://www.aygfsteel.com/maguangjun/articles/238108.html</link><dc:creator>马光?/dc:creator><author>马光?/author><pubDate>Sat, 01 Nov 2008 09:57:00 GMT</pubDate><guid>http://www.aygfsteel.com/maguangjun/articles/238108.html</guid><wfw:comment>http://www.aygfsteel.com/maguangjun/comments/238108.html</wfw:comment><comments>http://www.aygfsteel.com/maguangjun/articles/238108.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/maguangjun/comments/commentRss/238108.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/maguangjun/services/trackbacks/238108.html</trackback:ping><description><![CDATA[<table style="width: 762px; height: 461px" cellspacing="2" cellpadding="2" width="762" border="0"> <tbody> <tr> <td> <p>Lucene 软g包的发布形式是一?JAR 文gQ下面我们分析一下这?JAR 文g里面的主要的 JAVA 包,使读者对之有个初步的?jin)解?/p> <p>Package: org.apache.lucene.document</p> <p>q个包提供了(jin)一些ؓ(f)装要烦(ch)引的文档所需要的c,比如 Document, Field。这P每一个文档最l被装成了(jin)一?Document 对象?/p> <p>Package: org.apache.lucene.analysis</p> <p>q个包主要功能是Ҏ(gu)档进行分词,因ؓ(f)文档在徏立烦(ch)引之前必要q行分词Q所以这个包的作用可以看成是为徏立烦(ch)引做准备工作?/p> <p>Package: org.apache.lucene.index</p> <p>q个包提供了(jin)一些类来协助创建烦(ch)引以?qing)对创徏好的索引q行更新。这里面有两个基的类QIndexWriter ?IndexReaderQ其?IndexWriter 是用来创建烦(ch)引ƈd文档到烦(ch)引中的,IndexReader 是用来删除烦(ch)引中的文档的?/p> <p>Package: org.apache.lucene.search</p> <p>q个包提供了(jin)对在建立好的索引上进行搜索所需要的cR比?IndexSearcher ?Hits, IndexSearcher 定义?jin)在指定的?ch)引上q行搜烦(ch)的方法,Hits 用来保存搜烦(ch)得到的结果?/p> </td> </tr> </tbody> </table> <table style="width: 760px; height: 33px" cellspacing="2" cellpadding="2" width="760" border="0"> <tbody> <tr> <td> <p><a name="N10094"><span id="wmqeeuq" class="atitle">建立索引</span></a></p> <p>Z(jin)Ҏ(gu)档进行烦(ch)引,Lucene 提供?jin)五个基的类Q他们分别是 Document, Field, IndexWriter, Analyzer, Directory。下面我们分别介l一下这五个cȝ用途:(x)</p> <p><strong>Document</strong></p> <p>Document 是用来描q文档的Q这里的文档可以指一?HTML 面Q一电(sh)子邮Ӟ或者是一个文本文件。一?Document 对象由多?Field 对象l成的。可以把一?Document 对象惌成数据库中的一个记录,而每?Field 对象是记录的一个字Dc(din)?/p> <p><strong>Field</strong></p> <p>Field 对象是用来描qC个文档的某个属性的Q比如一电(sh)子邮件的标题和内容可以用两个 Field 对象分别描述?/p> <p><strong>Analyzer</strong></p> <p>在一个文档被索引之前Q首先需要对文档内容q行分词处理Q这部分工作是?Analyzer 来做的。Analyzer cL一个抽象类Q它有多个实现。针对不同的语言和应用需要选择适合?Analyzer。Analyzer 把分词后的内容交l?IndexWriter 来徏立烦(ch)引?/p> <p><strong>IndexWriter</strong></p> <p>IndexWriter ?Lucene 用来创徏索引的一个核?j)的c,他的作用是把一个个?Document 对象加到索引中来?/p> <p><strong>Directory</strong></p> <p>q个cM表了(jin) Lucene 的烦(ch)引的存储的位|,q是一个抽象类Q它目前有两个实玎ͼW一个是 FSDirectoryQ它表示一个存储在文gpȝ中的索引的位|。第二个?RAMDirectoryQ它表示一个存储在内存当中的烦(ch)引的位置?/p> <p>熟?zhn)了(jin)徏立?ch)引所需要的q些cdQ我们就开始对某个目录下面的文本文件徏立烦(ch)引了(jin)Q清?l出?jin)对某个目录下的文本文g建立索引的源代码?/p> </td> </tr> </tbody> </table> <a name="N100C3"><strong>清单 1. Ҏ(gu)本文件徏立烦(ch)?/strong></a><br /> <table style="width: 1111px; height: 964px" cellspacing="2" cellpadding="2" width="1111" border="0"> <tbody> <tr> <td> <pre class="displaycode">package TestLucene; import java.io.File; import java.io.FileReader; import java.io.Reader; 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; /** * This class demonstrate the process of creating index with Lucene * for text files */ public class TxtFileIndexer { public static void main(String[] args) throws Exception{ //indexDir is the directory that hosts Lucene's index files File indexDir = new File("D:\\luceneIndex"); //dataDir is the directory that hosts the text files that to be indexed File dataDir = new File("D:\\luceneData"); Analyzer luceneAnalyzer = new StandardAnalyzer(); File[] dataFiles = dataDir.listFiles(); IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true); long startTime = new Date().getTime(); for(int i = 0; i < dataFiles.length; i++){ if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt")){ System.out.println("Indexing file " + dataFiles[i].getCanonicalPath()); <!-- code sample is too wide --> Document document = new Document(); Reader txtReader = new FileReader(dataFiles[i]); document.add(Field.Text("path",dataFiles[i].getCanonicalPath())); document.add(Field.Text("contents",txtReader)); indexWriter.addDocument(document); } } indexWriter.optimize(); indexWriter.close(); long endTime = new Date().getTime(); System.out.println("It takes " + (endTime - startTime) + " milliseconds to create index for the files in directory " + dataDir.getPath()); } }</pre> <pre class="displaycode"> <p>在清?中,我们注意到类 IndexWriter 的构造函数需要三个参敎ͼW一个参数指定了(jin)所创徏的烦(ch)引要存放的位|,他可以是一?File 对象Q?br /> 也可以是一?FSDirectory 对象或?RAMDirectory 对象?br /> W二个参数指定了(jin) Analyzer cȝ一个实玎ͼ也就是指定这个烦(ch)引是用哪个分词器Ҏ(gu)挡内容进行分词?br /> W三个参数是一个布?yu)(dng)型的变量,如果?true 的话׃表创Z个新的烦(ch)引,?false 的话׃表在原来索引的基上进行操作?br /> 接着E序遍历?jin)目录下面的所有文本文档,qؓ(f)每一个文本文档创Z(jin)一?Document 对象?br /> 然后把文本文档的两个属性:(x)路径和内容加入到?jin)两?Field 对象中,接着在把q两?Field 对象加入?Document 对象中,<br /> 最后把q个文档?IndexWriter cȝ add Ҏ(gu)加入到烦(ch)引中厅R这h们便完成?jin)?ch)引的创徏。接下来我们q入在徏立好的烦(ch)引上q行搜烦(ch)的部分?/p> </pre> </td> </tr> </tbody> </table> <table style="width: 767px; height: 1616px" cellspacing="2" cellpadding="2" width="767" border="0"> <tbody> <tr> <td> <p><a name="N100CF"><span id="wmqeeuq" class="atitle">搜烦(ch)文档</span></a></p> <p>利用Luceneq行搜烦(ch)像建立索引一样也是非常方便的。在上面一部分中,我们已经Z个目录下的文本文档徏立好?jin)?ch)引,现在我们p在这个烦(ch)引上q行搜烦(ch)以找到包含某个关键词或短语的文档。Lucene提供?jin)几个基的类来完成这个过E,它们分别是呢IndexSearcher, Term, Query, TermQuery, Hits. 下面我们分别介绍q几个类的功能?/p> <p><strong>Query</strong></p> <p>q是一个抽象类Q他有多个实玎ͼ比如TermQuery, BooleanQuery, PrefixQuery. q个cȝ目的是把用户输入的查询字W串装成Lucene能够识别的Query?/p> <p><strong>Term</strong></p> <p>Term是搜索的基本单位Q一个Term对象有两个Stringcd的域l成。生成一个Term对象可以有如下一条语句来完成QTerm term = new Term(“fieldName”,”queryWord”); 其中W一个参C表了(jin)要在文档的哪一个Field上进行查找,W二个参C表了(jin)要查询的关键词?/p> <p><strong>TermQuery</strong></p> <p>TermQuery是抽象类Query的一个子c,它同时也是Lucene支持的最为基本的一个查询类。生成一个TermQuery对象由如下语句完成:(x) TermQuery termQuery = new TermQuery(new Term(“fieldName”,”queryWord”)); 它的构造函数只接受一个参敎ͼ那就是一个Term对象?/p> <p><strong>IndexSearcher</strong></p> <p>IndexSearcher是用来在建立好的索引上进行搜索的。它只能以只ȝ方式打开一个烦(ch)引,所以可以有多个IndexSearcher的实例在一个烦(ch)引上q行操作?/p> <p><strong>Hits</strong></p> <p>Hits是用来保存搜索的l果的?/p> <p>介绍完这些搜索所必须的类之后Q我们就开始在之前所建立的烦(ch)引上q行搜烦(ch)?jin),清?l出?jin)完成搜索功能所需要的代码?/p> <br /> <a name="N100FE"><strong>清单2 Q在建立好的索引上进行搜?/strong></a><br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode">package TestLucene; import java.io.File; import org.apache.lucene.document.Document; import org.apache.lucene.index.Term; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.FSDirectory; /** * This class is used to demonstrate the * process of searching on an existing * Lucene index * */ public class TxtFileSearcher { public static void main(String[] args) throws Exception{ String queryStr = "lucene"; //This is the directory that hosts the Lucene index File indexDir = new File("D:\\luceneIndex"); FSDirectory directory = FSDirectory.getDirectory(indexDir,false); IndexSearcher searcher = new IndexSearcher(directory); if(!indexDir.exists()){ System.out.println("The Lucene index is not exist"); return; } Term term = new Term("contents",queryStr.toLowerCase()); TermQuery luceneQuery = new TermQuery(term); Hits hits = searcher.search(luceneQuery); for(int i = 0; i < hits.length(); i++){ Document document = hits.doc(i); System.out.println("File: " + document.get("path")); } } } </pre> </td> </tr> </tbody> </table> <br /> <p>在清?中,cIndexSearcher的构造函数接受一个类型ؓ(f)Directory的对象,Directory是一个抽象类Q它目前有两个子c:(x)FSDirctory和RAMDirectory. 我们的程序中传入?jin)一个FSDirctory对象作ؓ(f)其参敎ͼ代表?jin)一个存储在盘?sh)的索引的位|。构造函数执行完成后Q代表了(jin)q个IndexSearcher以只ȝ方式打开?jin)一个烦(ch)引。然后我们程序构造了(jin)一个Term对象Q通过q个Term对象Q我们指定了(jin)要在文档的内容中搜烦(ch)包含关键?#8221;lucene”的文档。接着利用q个Term对象构造出TermQuery对象q把q个TermQuery对象传入到IndexSearcher的searchҎ(gu)中进行查询,q回的结果保存在Hits对象中。最后我们用?jin)一个@环语句把搜烦(ch)到的文档的\径都打印?jin)出来。好?jin),我们的搜索应用程序已l开发完毕,怎么P利用Lucene开发搜索应用程序是不是很简单?/p> </td> </tr> </tbody> </table> <br /> 转蝲地址Qhttp://www-128.ibm.com/developerworks/cn/java/j-lo-lucene1/ <img src ="http://www.aygfsteel.com/maguangjun/aggbug/238108.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/maguangjun/" target="_blank">马光?/a> 2008-11-01 17:57 <a href="http://www.aygfsteel.com/maguangjun/articles/238108.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ϊ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">̨</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ն</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ӻ</a>| <a href="http://" target="_blank">ڶ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ȷ</a>| <a href="http://" target="_blank">ҵ</a>| <a href="http://" target="_blank">ֵ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">˳</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ԫ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ʳ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">˷</a>| <a href="http://" target="_blank">ζ</a>| <a href="http://" target="_blank">׳</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ˮ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ϸ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>