??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲免费黄色网址,亚洲VA综合VA国产产VA中,亚洲AV中文无码乱人伦http://www.tkk7.com/maguangjun/category/35638.htmlzh-cnWed, 03 Dec 2008 05:06:35 GMTWed, 03 Dec 2008 05:06:35 GMT60利用Lucene 实现高搜烦http://www.tkk7.com/maguangjun/articles/238109.html马光?/dc:creator>马光?/author>Sat, 01 Nov 2008 10:08:00 GMThttp://www.tkk7.com/maguangjun/articles/238109.htmlhttp://www.tkk7.com/maguangjun/comments/238109.htmlhttp://www.tkk7.com/maguangjun/articles/238109.html#Feedback0http://www.tkk7.com/maguangjun/comments/commentRss/238109.htmlhttp://www.tkk7.com/maguangjun/services/trackbacks/238109.html

Lucene 支持多种形式的高U搜索,我们在这一部分中会(x)q行探讨Q然后我?x)?Lucene ?API 来演C如何实现这些高U搜索功能?br />

布尔操作W?/strong>

大多数的搜烦引擎都会(x)提供布尔操作W让用户可以l合查询Q典型的布尔操作W有 AND, OR, NOT。Lucene 支持 5 U布?yu)(dng)操作符Q分别是 AND, OR, NOT, ?+), ?-)。接下来我会(x)讲述每个操作W的用法?

  • OR: 如果你要搜烦含有字符 A 或?B 的文,那么需要?OR 操作W。需要记住的是,如果你只是简单的用空格将两个关键词分割开Q其实在搜烦的时候搜索引擎会(x)自动在两个关键词之间加上 OR 操作W。例如,“Java OR Lucene” ?“Java Lucene” 都是搜烦含有 Java 或者含?Lucene 的文?
  • AND: 如果你需要搜索包含一个以上关键词的文档,那么需要?AND 操作W。例如,“Java AND Lucene” q回所有既包含 Java 又包?Lucene 的文?
  • NOT: Not 操作W得包含紧跟在 NOT 后面的关键词的文不?x)被q回。例如,如果你想搜烦所有含?Java 但不含有 Lucene 的文档,你可以用查询语?“Java NOT Lucene”。但是你不能只对一个搜索词使用q个操作W,比如Q查询语?“NOT Java” 不会(x)q回Ml果?
  • 加号Q?Q?/strong>: q个操作W的作用?AND 差不多,但它只对紧跟着它的一个搜索词起作用。例如,如果你想搜烦一定包?JavaQ但不一定包?Lucene 的文,可以用查询语?#8220;+Java Lucene”?
  • 减号Q?Q?/strong>: q个操作W的功能?NOT 一P查询语句 “Java -Lucene” q回所有包?Java 但不包含 Lucene 的文?

域搜?Field Search)

Lucene 支持域搜索,你可以指定一ơ查询是在哪些域(Field)上进行。例如,如果索引的文包含两个域Q?code>Title ?ContentQ你可以用查?“Title: Lucene AND Content: Java” 来返回所有在 Title 域上包含 Lucene q且?Content 域上包含 Java 的文档?br />

通配W搜?Wildcard Search)

Lucene 支持两种通配W:(x)问号Q?Q和星号Q?Q。你可以使用问号Q?Q来q行单字W的通配W查询,或者利用星P*Q进行多字符的通配W查询。例如,如果你想搜烦 tiny 或?tonyQ你可以用查询语?“t?ny”Q如果你x?Teach, Teacher ?TeachingQ你可以用查询语?“Teach*”?br />

模糊查询

Lucene 提供的模p查询基于编辑距ȝ?Edit distance algorithm)。你可以在搜索词的尾部加上字W?~ 来进行模p查询。例如,查询语句 “think~” q回所有包含和 think cM的关键词的文?br />

范围搜烦(Range Search)

范围搜烦匚w某个域上的值在一定范围的文。例如,查询 “age:[18 TO 35]” q回所?age 域上的值在 18 ?35 之间的文档?/p> 转蝲地址Qhttp://www.cnblogs.com/weekzero/articles/1224064.html

]]>
Lucene软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个包提供了一些ؓ(f)装要烦引的文所需要的c,比如 Document, Field。这P每一个文最l被装成了一?Document 对象?/p> <p>Package: org.apache.lucene.analysis</p> <p>q个包主要功能是Ҏ(gu)进行分词,因ؓ(f)文在徏立烦引之前必要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="f5h3tfn" class="atitle">建立索引</span></a></p> <p>ZҎ(gu)进行烦引,Lucene 提供了五个基的类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?/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ؓ(f)每一个文本文档创Z一?Document 对象?br /> 然后把文本文的两个属性:(x)路径和内容加入到了两?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="5v5pnnn" 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对象由如下语句完成:(x) 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的构造函数接受一个类型ؓ(f)Directory的对象,Directory是一个抽象类Q它目前有两个子c:(x)FSDirctory和RAMDirectory. 我们的程序中传入了一个FSDirctory对象作ؓ(f)其参敎ͼ代表了一个存储在盘上的索引的位|。构造函数执行完成后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://wenfaka.com" target="_blank">þþþAV</a>| <a href="http://pecbj.com" target="_blank">þ޹ŷ޾Ʒһ</a>| <a href="http://shiliuvip.com" target="_blank">޹˾Ʒþ</a>| <a href="http://3x79.com" target="_blank">Ļ߹ۿձ</a>| <a href="http://z88d.com" target="_blank">AVһϵ</a>| <a href="http://wwwdd312.com" target="_blank">3pˬִƵ</a>| <a href="http://91vk.com" target="_blank">ԭƵ</a>| <a href="http://cqtchtwq.com" target="_blank">þþþþྫƷֱ</a>| <a href="http://zzyqr.com" target="_blank">޹һ</a>| <a href="http://av-fantasy.com" target="_blank">avһ</a>| <a href="http://91se01.com" target="_blank">ŷ޹պƷ</a>| <a href="http://51wdn.com" target="_blank">91Ƶѿ</a>| <a href="http://qimiaodh.com" target="_blank">츾һ</a>| <a href="http://xuexilo.com" target="_blank">˻18Ƶ</a>| <a href="http://airou08.com" target="_blank">AëƬֻ</a>| <a href="http://6363388.com" target="_blank">avպavŷv</a>| <a href="http://yongfu-sh.com" target="_blank">91avƵ</a>| <a href="http://dbyssw.com" target="_blank">޾ɫ߲</a>| <a href="http://tlyihong.com" target="_blank">131ŮëƬ</a>| <a href="http://jinlaifubuxiugang.com" target="_blank">߲</a>| <a href="http://beijinzhongliuyiyuan.com" target="_blank">ƷŮҹˬˬˬ</a>| <a href="http://jjesqc.com" target="_blank">ȫѵһëƬ</a>| <a href="http://dingdingduo.com" target="_blank">츾avһ </a>| <a href="http://sijep.com" target="_blank">Ƶ</a>| <a href="http://joob4s.com" target="_blank">޹㽶ˬAVƬþ</a>| <a href="http://www-533999.com" target="_blank">þþþþþùѿ</a>| <a href="http://iciap.com" target="_blank">þ޾ƷƷ</a>| <a href="http://zfzz008.com" target="_blank">ҳƵ߹ۿ </a>| <a href="http://www-01313.com" target="_blank">һȫëƬ</a>| <a href="http://dwj28.com" target="_blank">ĻƵ߹ۿ </a>| <a href="http://tv886.com" target="_blank">ɫ¶ۺ</a>| <a href="http://guhey.com" target="_blank">ùƷӰ˾þ</a>| <a href="http://caicpa.com" target="_blank">a߹ۿƵ</a>| <a href="http://6711wan.com" target="_blank">޹˾Ʒһ</a>| <a href="http://szwangzhongwang.com" target="_blank">Ƶ97</a>| <a href="http://szyxfhm.com" target="_blank">㶮ѹۿ</a>| <a href="http://lh726.com" target="_blank">ɫϰƵ߹</a>| <a href="http://by23336.com" target="_blank">޹һɾþþƷۺ</a>| <a href="http://fennenll.com" target="_blank">þþþþһ</a>| <a href="http://173ba.com" target="_blank">ĻۺСۺ</a>| <a href="http://116com.com" 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>