??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲自偷自偷在线制服,亚洲砖码砖专无区2023,2017亚洲男人天堂一http://www.tkk7.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.tkk7.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.tkk7.com/maguangjun/articles/238108.html</guid><wfw:comment>http://www.tkk7.com/maguangjun/comments/238108.html</wfw:comment><comments>http://www.tkk7.com/maguangjun/articles/238108.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/maguangjun/comments/commentRss/238108.html</wfw:commentRss><trackback:ping>http://www.tkk7.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 包,使读者对之有个初步的了解?/p> <p>Package: org.apache.lucene.document</p> <p>q个包提供了一些ؓ装要烦引的文档所需要的c,比如 Document, Field。这P每一个文最l被装成了一?Document 对象?/p> <p>Package: org.apache.lucene.analysis</p> <p>q个包主要功能是Ҏ(gu)进行分词,因ؓ文在徏立烦引之前必要q行分词Q所以这个包的作用可以看成是为徏立烦引做准备工作?/p> <p>Package: org.apache.lucene.index</p> <p>q个包提供了一些类来协助创建烦引以及对创徏好的索引q行更新。这里面有两个基的类QIndexWriter ?IndexReaderQ其?IndexWriter 是用来创建烦引ƈd文档到烦引中的,IndexReader 是用来删除烦引中的文的?/p> <p>Package: org.apache.lucene.search</p> <p>q个包提供了对在建立好的索引上进行搜索所需要的cR比?IndexSearcher ?Hits, IndexSearcher 定义了在指定的烦引上q行搜烦的方法,Hits 用来保存搜烦得到的结果?/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="91zr31d" class="atitle">建立索引</span></a></p> <p>ZҎ(gu)进行烦引,Lucene 提供了五个基的类Q他们分别是 Document, Field, IndexWriter, Analyzer, Directory。下面我们分别介l一下这五个cȝ用途:</p> <p><strong>Document</strong></p> <p>Document 是用来描q文档的Q这里的文可以指一?HTML 面Q一电(sh)子邮Ӟ或者是一个文本文件。一?Document 对象由多?Field 对象l成的。可以把一?Document 对象惌成数据库中的一个记录,而每?Field 对象是记录的一个字Dc?/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 来徏立烦引?/p> <p><strong>IndexWriter</strong></p> <p>IndexWriter ?Lucene 用来创徏索引的一个核心的c,他的作用是把一个个?Document 对象加到索引中来?/p> <p><strong>Directory</strong></p> <p>q个cM表了 Lucene 的烦引的存储的位|,q是一个抽象类Q它目前有两个实玎ͼW一个是 FSDirectoryQ它表示一个存储在文gpȝ中的索引的位|。第二个?RAMDirectoryQ它表示一个存储在内存当中的烦引的位置?/p> <p>熟?zhn)了徏立烦引所需要的q些cdQ我们就开始对某个目录下面的文本文件徏立烦引了Q清?l出了对某个目录下的文本文g建立索引的源代码?/p> </td> </tr> </tbody> </table> <a name="N100C3"><strong>清单 1. Ҏ(gu)本文件徏立烦?/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一个参数指定了所创徏的烦引要存放的位|,他可以是一?File 对象Q?br /> 也可以是一?FSDirectory 对象或?RAMDirectory 对象?br /> W二个参数指定了 Analyzer cȝ一个实玎ͼ也就是指定这个烦引是用哪个分词器Ҏ(gu)挡内容进行分词?br /> W三个参数是一个布?yu)(dng)型的变量,如果?true 的话׃表创Z个新的烦引,?false 的话׃表在原来索引的基上进行操作?br /> 接着E序遍历了目录下面的所有文本文,qؓ每一个文本文创Z一?Document 对象?br /> 然后把文本文档的两个属性:路径和内容加入到了两?Field 对象中,接着在把q两?Field 对象加入?Document 对象中,<br /> 最后把q个文?IndexWriter cȝ add Ҏ(gu)加入到烦引中厅R这h们便完成了烦引的创徏。接下来我们q入在徏立好的烦引上q行搜烦的部分?/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="vphnr99" class="atitle">搜烦文</span></a></p> <p>利用Luceneq行搜烦像建立索引一样也是非常方便的。在上面一部分中,我们已经Z个目录下的文本文徏立好了烦引,现在我们p在这个烦引上q行搜烦以找到包含某个关键词或短语的文。Lucene提供了几个基的类来完成这个过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表了要在文档的哪一个Field上进行查找,W二个参C表了要查询的关键词?/p> <p><strong>TermQuery</strong></p> <p>TermQuery是抽象类Query的一个子c,它同时也是Lucene支持的最为基本的一个查询类。生成一个TermQuery对象由如下语句完成: TermQuery termQuery = new TermQuery(new Term(“fieldName”,”queryWord”)); 它的构造函数只接受一个参敎ͼ那就是一个Term对象?/p> <p><strong>IndexSearcher</strong></p> <p>IndexSearcher是用来在建立好的索引上进行搜索的。它只能以只ȝ方式打开一个烦引,所以可以有多个IndexSearcher的实例在一个烦引上q行操作?/p> <p><strong>Hits</strong></p> <p>Hits是用来保存搜索的l果的?/p> <p>介绍完这些搜索所必须的类之后Q我们就开始在之前所建立的烦引上q行搜烦了,清单2l出了完成搜索功能所需要的代码?/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的构造函数接受一个类型ؓDirectory的对象,Directory是一个抽象类Q它目前有两个子c:FSDirctory和RAMDirectory. 我们的程序中传入了一个FSDirctory对象作ؓ其参敎ͼ代表了一个存储在盘上的索引的位|。构造函数执行完成后Q代表了q个IndexSearcher以只ȝ方式打开了一个烦引。然后我们程序构造了一个Term对象Q通过q个Term对象Q我们指定了要在文的内容中搜烦包含关键?#8221;lucene”的文。接着利用q个Term对象构造出TermQuery对象q把q个TermQuery对象传入到IndexSearcher的searchҎ(gu)中进行查询,q回的结果保存在Hits对象中。最后我们用了一个@环语句把搜烦到的文的\径都打印了出来。好了,我们的搜索应用程序已l开发完毕,怎么P利用Lucene开发搜索应用程序是不是很简单?/p> </td> </tr> </tbody> </table> <br /> 转蝲地址Qhttp://www-128.ibm.com/developerworks/cn/java/j-lo-lucene1/ <img src ="http://www.tkk7.com/maguangjun/aggbug/238108.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/maguangjun/" target="_blank">马光?/a> 2008-11-01 17:57 <a href="http://www.tkk7.com/maguangjun/articles/238108.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> վ֩ģ壺 <a href="http://tbw77.com" target="_blank">޼ɫС˵</a>| <a href="http://9xcb.com" target="_blank">AVþþƷ</a>| <a href="http://rp71.com" target="_blank">츾AVһլ</a>| <a href="http://lyjhjx.com" target="_blank">91Ƶ</a>| <a href="http://222941.com" target="_blank">޾Ʒþþþþþ </a>| <a href="http://jiayila.com" target="_blank">ȫëƬ</a>| <a href="http://wenbye.com" target="_blank">͵޾Ʒ</a>| <a href="http://qulu999.com" target="_blank">޾Ʒ˳ëƬ</a>| <a href="http://hhgcnet.com" target="_blank">avӰԺһ</a>| <a href="http://www12kvkv.com" target="_blank">ëƬƵ</a>| <a href="http://142121.com" target="_blank">¾þþƷ</a>| <a href="http://khushkhush.com" target="_blank">޹Ʒˬ </a>| <a href="http://sxjttxkywl.com" target="_blank">޸Ƶ</a>| <a href="http://billtsssrvp.com" target="_blank">2019Ļֱ</a>| <a href="http://14743592.com" target="_blank">޹ֻߵӰbd </a>| <a href="http://youyou8tv.com" target="_blank">AV߹ۿ </a>| <a href="http://wilbysec.com" target="_blank">ۺϼƵ</a>| <a href="http://yw323.com" target="_blank">ëƬۿ</a>| <a href="http://fanqi5.com" target="_blank">ɫƷƵ</a>| <a href="http://0730jxpm.com" target="_blank">ݺۺϾþۺ88</a>| <a href="http://528676.com" target="_blank">Ůcaoվѿ߿</a>| <a href="http://17soco.com" target="_blank">Ӱվ</a>| <a href="http://uiui6.com" target="_blank">վɫѿ</a>| <a href="http://16lds.com" target="_blank">ѾƷӰվ</a>| <a href="http://jiezinet.com" target="_blank">AV12þ</a>| <a href="http://xdxsw.com" target="_blank">㽶97ѹ</a>| <a href="http://sdshfengji.com" target="_blank">þˮAV뾫Ʒ</a>| <a href="http://66cws.com" target="_blank">AV߲պŷ</a>| <a href="http://88ww99.com" target="_blank">Ļ</a>| <a href="http://zjhmpaper.com" target="_blank">޳aƬ߲һ</a>| <a href="http://manghuo365.com" target="_blank">jjzzŮ</a>| <a href="http://cnkaiertong.com" target="_blank">99ƷƵѹۿ</a>| <a href="http://avqq222.com" target="_blank">޹ۺϾƷĵһ</a>| <a href="http://shlamore.com" target="_blank">ѾþþƷþþ</a>| <a href="http://mangaou.com" target="_blank">AVרһ</a>| <a href="http://pjwys.com" target="_blank">Թ</a>| <a href="http://ttvv77.com" target="_blank">JIZZձ</a>| <a href="http://ssdtsz.com" target="_blank">ҹƵ</a>| <a href="http://zanyoo.com" target="_blank">va߹ۿ</a>| <a href="http://gedebai.com" target="_blank">޾Ʒm߹ۿ</a>| <a href="http://sdyzzs.com" target="_blank">޾Ʒa vС˵</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>