首页 lucene简单示例[1]

lucene简单示例[1]

举报
开通vip

lucene简单示例[1]简单实例(一) 简单实例(一) 说明一下,这一篇文章的用到的lucene,是用2.0版本的,主要在查询的时候2.0版本的lucene与以前的版本有了一些区别.  其实这一些代码都是早几个月写的,自己很懒,所以到今天才写到自己的博客上,高深的文章自己写不了,只能记录下一些简单的记录与点滴,其中的代码算是自娱自乐的,希望高手不要把重构之类的砸下来...  1、在windows系统下的的C盘,建一个名叫s的文件夹,在该文件夹里面随便建三个txt文件,随便起名啦,就叫"1.txt","2.txt"和"3.txt"啦  其中...

lucene简单示例[1]
简单实例(一) 简单实例(一) 说明一下,这一篇文章的用到的lucene,是用2.0版本的,主要在查询的时候2.0版本的lucene与以前的版本有了一些区别.  其实这一些代码都是早几个月写的,自己很懒,所以到今天才写到自己的博客上,高深的文章自己写不了,只能记录下一些简单的记录与点滴,其中的代码算是自娱自乐的,希望高手不要把重构之类的砸下来...  1、在windows系统下的的C盘,建一个名叫s的文件夹,在该文件夹里面随便建三个txt文件,随便起名啦,就叫"1.txt","2.txt"和"3.txt"啦  其中1.txt的内容如下:  Java代码  1.​ 中华人民共和国   2.​ 全国人民   3.​ 2006年   而"2.txt"和"3.txt"的内容也可以随便写几写,这里懒写,就复制一个和1.txt文件的内容一样吧  2、下载lucene包,放在classpath路径中  建立索引:  Java代码  1.​ package lighter.javaeye.com;   2.​    3.​ import java.io.BufferedReader;   4.​ import java.io.File;   5.​ import java.io.FileInputStream;   6.​ import java.io.IOException;   7.​ import java.io.InputStreamReader;   8.​ import java.util.Date;   9.​    10.​ import org.apache.lucene.analysis.Analyzer;   11.​ import org.apache.lucene.analysis.standard.StandardAnalyzer;   12.​ import org.apache.lucene.document.Document;   13.​ import org.apache.lucene.document.Field;   14.​ import org.apache.lucene.index.IndexWriter;   15.​    16.​ /**  17.​  * author lighter date 2006-8-7  18.​  */   19.​ public class TextFileIndexer {   20.​     public static void main(String[] args) throws Exception {   21.​         /* 指明要索引文件夹的位置,这里是C盘的S文件夹下 */   22.​         File fileDir = new File("c:\\s");   23.​    24.​         /* 这里放索引文件的位置 */   25.​         File indexDir = new File("c:\\index");   26.​         Analyzer luceneAnalyzer = new StandardAnalyzer();   27.​         IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,   28.​                 true);   29.​         File[] textFiles = fileDir.listFiles();   30.​         long startTime = new Date().getTime();   31.​            32.​         //增加document到索引去   33.​         for (int i = 0; i < textFiles.length; i++) {   34.​             if (textFiles[i].isFile()   35.​                     && textFiles[i].getName().endsWith(".txt")) {   36.​                 System.out.println("File " + textFiles[i].getCanonicalPath()   37.​                         + "正在被索引....");   38.​                 String temp = FileReaderAll(textFiles[i].getCanonicalPath(),   39.​                         "GBK");   40.​                 System.out.println(temp);   41.​                 Document document = new Document();   42.​                 Field FieldPath = new Field("path", textFiles[i].getPath(),   43.​                         Field.Store.YES, Field.Index.NO);   44.​                 Field FieldBody = new Field("body", temp, Field.Store.YES,   45.​                         Field.Index.TOKENIZED,   46.​                         Field.TermVector.WITH_POSITIONS_OFFSETS);   47.​                 document.add(FieldPath);   48.​                 document.add(FieldBody);   49.​                 indexWriter.addDocument(document);   50.​             }   51.​         }   52.​         //optimize()方法是对索引进行优化   53.​         indexWriter.optimize();   54.​         indexWriter.close();   55.​            56.​         //测试一下索引的时间   57.​         long endTime = new Date().getTime();   58.​         System.out   59.​                 .println("这花费了"   60.​                         + (endTime - startTime)   61.​                         + " 毫秒来把文档增加到索引里面去!"   62.​                         + fileDir.getPath());   63.​     }   64.​    65.​     public static String FileReaderAll(String FileName, String charset)   66.​             throws IOException {   67.​         BufferedReader reader = new BufferedReader(new InputStreamReader(   68.​                 new FileInputStream(FileName), charset));   69.​         String line = new String();   70.​         String temp = new String();   71.​            72.​         while ((line = reader.readLine()) != null) {   73.​             temp += line;   74.​         }   75.​         reader.close();   76.​         return temp;   77.​     }   78.​ }   索引的结果:  Java代码  1.​ File C:\s\1.txt正在被索引....   2.​ 中华人民共和国全国人民2006年   3.​ File C:\s\2.txt正在被索引....   4.​ 中华人民共和国全国人民2006年   5.​ File C:\s\3.txt正在被索引....   6.​ 中华人民共和国全国人民2006年   7.​ 这花费了297 毫秒来把文档增加到索引里面去!c:\s   3、建立了索引之后,查询啦....  Java代码  1.​ package lighter.javaeye.com;   2.​    3.​ import java.io.IOException;   4.​    5.​ import org.apache.lucene.analysis.Analyzer;   6.​ import org.apache.lucene.analysis.standard.StandardAnalyzer;   7.​ import org.apache.lucene.queryParser.ParseException;   8.​ import org.apache.lucene.queryParser.QueryParser;   9.​ import org.apache.lucene.search.Hits;   10.​ import org.apache.lucene.search.IndexSearcher;   11.​ import org.apache.lucene.search.Query;   12.​    13.​ public class TestQuery {   14.​     public static void main(String[] args) throws IOException, ParseException {   15.​         Hits hits = null;   16.​         String queryString = "中华";   17.​         Query query = null;   18.​         IndexSearcher searcher = new IndexSearcher("c:\\index");   19.​    20.​         Analyzer analyzer = new StandardAnalyzer();   21.​         try {   22.​             QueryParser qp = new QueryParser("body", analyzer);   23.​             query = qp.parse(queryString);   24.​         } catch (ParseException e) {   25.​         }   26.​         if (searcher != null) {   27.​             hits = searcher.search(query);   28.​             if (hits.length() > 0) {   29.​                 System.out.println("找到:" + hits.length() + " 个结果!");   30.​             }   31.​         }   32.​     }   33.​    34.​ }   其运行结果:  引用 找到:3 个结果! 简单实例(二) 写文章的时候,感觉比较难写的就是标 快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题 ,有时候不知道起什么名字好,反正这里写的都是关于lucene的一些简单的实例,就随便起啦.  Lucene 其实很简单的,它最主要就是做两件事:建立索引和进行搜索  来看一些在lucene中使用的术语,这里并不打算作详细的介绍,只是点一下而已----因为这一个世界有一种好东西,叫搜索。  IndexWriter:lucene中最重要的的类之一,它主要是用来将文档加入索引,同时控制索引过程中的一些参数使用。  Analyzer:分析器,主要用于分析搜索引擎遇到的各种文本。常用的有StandardAnalyzer分析器,StopAnalyzer分析器,WhitespaceAnalyzer分析器等。  Directory:索引存放的位置;lucene提供了两种索引存放的位置,一种是磁盘,一种是内存。一般情况将索引放在磁盘上;相应地lucene提供了FSDirectory和RAMDirectory两个类。  Document:文档;Document相当于一个要进行索引的单元,任何可以想要被索引的文件都必须转化为Document对象才能进行索引。  Field:字段。  IndexSearcher:是lucene中最基本的检索工具,所有的检索都会用到IndexSearcher工具;  Query:查询,lucene中支持模糊查询,语义查询,短语查询,组合查询等等,如有TermQuery,BooleanQuery,RangeQuery,WildcardQuery等一些类。  QueryParser: 是一个解析用户输入的工具,可以通过扫描用户输入的字符串,生成Query对象。  Hits:在搜索完成之后,需要把搜索结果返回并显示给用户,只有这样才算是完成搜索的目的。在lucene中,搜索的结果的集合是用Hits类的实例来表示的。  上面作了一大堆名词解释,下面就看几个简单的实例吧:  1、简单的的StandardAnalyzer测试例子  Java代码  1.​ package lighter.javaeye.com;   2.​    3.​ import java.io.IOException;   4.​ import java.io.StringReader;   5.​    6.​ import org.apache.lucene.analysis.Analyzer;   7.​ import org.apache.lucene.analysis.Token;   8.​ import org.apache.lucene.analysis.TokenStream;   9.​ import org.apache.lucene.analysis.standard.StandardAnalyzer;   10.​    11.​ public class StandardAnalyzerTest    12.​ {   13.​     //构造函数,   14.​     public StandardAnalyzerTest()   15.​     {   16.​     }   17.​     public static void main(String[] args)    18.​     {   19.​         //生成一个StandardAnalyzer对象   20.​         Analyzer aAnalyzer = new StandardAnalyzer();   21.​         //测试字符串   22.​         StringReader sr = new StringReader("lighter javaeye com is the are on");   23.​         //生成TokenStream对象   24.​         TokenStream ts = aAnalyzer.tokenStream("name", sr);    25.​         try {   26.​             int i=0;   27.​             Token t = ts.next();   28.​             while(t!=null)   29.​             {   30.​                 //辅助输出时显示行号   31.​                 i++;   32.​                 //输出处理后的字符   33.​                 System.out.println("第"+i+"行:"+t.termText());   34.​                 //取得下一个字符   35.​                 t=ts.next();   36.​             }   37.​         } catch (IOException e) {   38.​             e.printStackTrace();   39.​         }   40.​     }   41.​ }   显示结果:  引用 第1行:lighter  第2行:javaeye  第3行:com 提示一下:  StandardAnalyzer是lucene中内置的" 标准 excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载 分析器",可以做如下功能:  1、对原有句子按照空格进行了分词  2、所有的大写字母都可以能转换为小写的字母  3、可以去掉一些没有用处的单词,例如"is","the","are"等单词,也删除了所有的标点  查看一下结果与"new StringReader("lighter javaeye com is the are on")"作一个比较就清楚明了。  这里不对其API进行解释了,具体见lucene的官方文档。需要注意一点,这里的代码使用的是lucene2的API,与1.43版有一些明显的差别。  2、看另一个实例,简单地建立索引,进行搜索  Java代码  1.​ package lighter.javaeye.com;   2.​ import org.apache.lucene.analysis.standard.StandardAnalyzer;   3.​ import org.apache.lucene.document.Document;   4.​ import org.apache.lucene.document.Field;   5.​ import org.apache.lucene.index.IndexWriter;   6.​ import org.apache.lucene.queryParser.QueryParser;   7.​ import org.apache.lucene.search.Hits;   8.​ import org.apache.lucene.search.IndexSearcher;   9.​ import org.apache.lucene.search.Query;   10.​ import org.apache.lucene.store.FSDirectory;   11.​    12.​ public class FSDirectoryTest {   13.​    14.​     //建立索引的路径   15.​     public static final String path = "c:\\index2";   16.​    17.​     public static void main(String[] args) throws Exception {   18.​         Document doc1 = new Document();   19.​         doc1.add( new Field("name", "lighter javaeye com",Field.Store.YES,Field.Index.TOKENIZED));   20.​    21.​         Document doc2 = new Document();   22.​         doc2.add(new Field("name", "lighter blog",Field.Store.YES,Field.Index.TOKENIZED));   23.​    24.​         IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true), new StandardAnalyzer(), true);   25.​         writer.setMaxFieldLength(3);   26.​         writer.addDocument(doc1);   27.​         writer.setMaxFieldLength(3);   28.​         writer.addDocument(doc2);   29.​         writer.close();   30.​    31.​         IndexSearcher searcher = new IndexSearcher(path);   32.​         Hits hits = null;   33.​         Query query = null;   34.​         QueryParser qp = new QueryParser("name",new StandardAnalyzer());   35.​            36.​         query = qp.parse("lighter");   37.​         hits = searcher.search(query);   38.​         System.out.println("查找\"lighter\" 共" + hits.length() + "个结果");   39.​    40.​         query = qp.parse("javaeye");   41.​         hits = searcher.search(query);   42.​         System.out.println("查找\"javaeye\" 共" + hits.length() + "个结果");   43.​    44.​     }   45.​    46.​ }   运行结果:  Java代码  1.​ 查找"lighter" 共2个结果   2.​ 查找"javaeye" 共1个结果  
本文档为【lucene简单示例[1]】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_603339
暂无简介~
格式:doc
大小:74KB
软件:Word
页数:7
分类:互联网
上传时间:2010-09-29
浏览量:18