Lucene 简介
最近几年Lucene的更新速度很快.目前的最新版本是4.6.Lucene它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构.目前有很多应用程序是基于Lucene的,比如我们常用的Eclipse的帮助信息就是其中之一.Lucene能够为文本类型的数据建立索引.所以我们也可以将HTML,PDF,Word格式数据转换成文本后进行索引.然后将其保存到磁盘或者内存中.用户可以根据条件在索引文件中进行查询.
Lucene常用的几个对象:
Document:用来描述文档.一个 Document 对象由多个Field 对象组成的。可以将其看成是一个Document 就是一条记录,Field 相当于一条记录中的一个属性
Field:描述一个文档的属性.比如一个文件可以由文件名和内容两个Field描述.
Analyzer:需要索引就可能需要分词.Analyzer就是来负责这个工作的.它是一个抽象类.
IndexWriter:把一个个的Document添加到索引中.
IndexReader:主要是对文档的检索.
Directory:Lucene 的索引的存储的位置.它是一个抽象类.FSDirectory,表示检索文件磁在盘中的位置.RAMDirectory表示内存中的索引位置.
接下来看一下如何建立索引:
public static void index(boolean hasIndex) { int[] ids = {0,1,2,3,4,5}; String[] emails = {"lfd@foxmail.com","lfd@qq.com","lfx@qq.com","lfx@foxmail.com","hll@gcp.edu","zzp@gcp.edu"}; String[] contents = { "incididunt ut labore et dolore magna aliqua. Ut enim ad lorem. ", "Lorem ipsum dolor sit amet lorem consectetur adipisicing", "dolor in reprehenderit in voluptate velit esse cillum nostrud exercitation ullamco laboris. ", "dolor in reprehenderit in voluptate velit esse cillum nostrud exercitation ullamco laboris. ", "Lorem ipsum dolor sit amet, consectetur adipisicing elit", "Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna " }; String[] names = {"zzp","lfd","lfx","tom","huanglili","tzp"}; IndexWriter writer = null ; Directory directory = null ; try { directory = FSDirectory.open(new File("D:/Lucene")) ; //directory = new RAMDirectory() ; //索引文件在内存 writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_46, new StandardAnalyzer(Version.LUCENE_46))) ; //是否重新构建索引 if(hasIndex) { writer.deleteAll() ; } int count = names.length ; for(int i=0; i<count; i++) { Document doc = new Document() ; /* 首先是一个不变的属性值,这类字段还有一个主要用途, * 就是可以用于对搜索的返回结果集排序或是按范 * 围查询FloatField DoubleField IntField LongField BinaryDocValuesField NumericDocValuesField SortedDocValuesField SortedSetDocValuesField StoredField 整个域要存储的 StringField 是一个不需要分词,而直接用于索引的字符串 TextField 是一大块需要经过分词的文本 FieldType fieldType = new FieldType(); fieldType.setIndexed(true);//set 是否索引 fieldType.setStored(true);//set 是否存储 fieldType.setTokenized(false);//set 是否分词*/ doc.add(new IntField("id", ids[i], Store.YES)) ; doc.add(new StringField("email", emails[i], Store.YES)); doc.add(new TextField("content", contents[i], Store.YES)) ; FieldType type = new FieldType() ; type.setIndexed(true) ; type.setStored(true) ; doc.add(new Field("name", names[i], type)) ; writer.addDocument(doc) ; } writer.commit() ; } catch (IOException e) { e.printStackTrace(); } finally { try { if(writer != null) { writer.close() ; writer = null ; } } catch (IOException e) { e.printStackTrace(); } } }
简单的查询方式:
public static void searcher01() { try { Directory directory = FSDirectory.open(new File("D:/Lucene")) ; DirectoryReader reader = DirectoryReader.open(directory) ; IndexSearcher searcher = new IndexSearcher(reader) ; //获取Query,查询Field名为content,内容中包含consectetur. Query query = new TermQuery(new Term("content", "consectetur")) ; TopDocs topDocs = searcher.search(query, 10) ; ScoreDoc[] scores = topDocs.scoreDocs ; int length = scores.length ; for(int i=0; i<length; i++) { //scores[i].doc:根据Document的id获取Document //doc.get("xxx"):获取储存索引时的Field名获取相应Document的内容. Document doc = searcher.doc(scores[i].doc) ; System.out.println("id:" + doc.get("id") + " email:" + doc.get("email") + " content:" + doc.get("content") + " name:" + doc.get("name")); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(reader != null) { reader.close() ; reader = null ; } } catch (IOException e) { e.printStackTrace(); } } }
删除索引:
public static void delIndex() { IndexWriter writer = null ; try { writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_46, new StandardAnalyzer(Version.LUCENE_46))) ; writer.deleteDocuments(new Term("content", "welcome")) ; } catch (IOException e) { e.printStackTrace(); } finally { try { if(writer != null) { writer.close() ; writer = null ; } } catch (IOException e) { e.printStackTrace(); } } }
更新索引:
public static void updIndex() { IndexWriter writer = null ; try { writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_46, new StandardAnalyzer(Version.LUCENE_46))) ; Document doc = new Document(); doc.add(new StringField("id","11", Field.Store.YES)); doc.add(new StringField("content", "incididunt ut labore et dolore magna aliqua. Ut enim ad lorem. ", Field.Store.YES)); writer.updateDocument(new Term("id","0"), doc) ; } catch (IOException e) { e.printStackTrace(); } finally { try { if(writer != null) { writer.close() ; writer = null ; } } catch (IOException e) { e.printStackTrace(); } } }
相关推荐
全局搜索lucene4.6版本开发需要的jar包
创建索引,查询,过滤,同义词,近及时搜索,里面有4.6与5.0的jar文件,请自行引入
18.lucene4.6索引的相关操作 19.lucene4.6的各种Query(1) 20.lucene4.6的各种Query(2) 21.lucene4.6的各种Query(3) 22.solr4.6的快速搭建 23.solr4.6索引的相关操作 24.solr4.6搜索的相关参数功能(1) 25.solr4.6搜索...
lucene4.6所有jar包 lucene 搜索引擎 可以网上找教程,但是貌似网上的都太旧了, 4.6改动挺大的,构造函数都有很大差异~~无语啊!
lucene4.6实例,其中用到的jar包到官网下载.
lucene
Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻。 从之前发布其他chm文件下载用户的反映看,有不少朋友反映下载后打开无法显示,这一般不是chm文件的问题,这里统一说明一下解决办法: 如果文件...
整个课程,按照一个从无到有的过程来展开。所有的数据,来自于互联网,用heritrix去抓取。对于抓取的数据,进行去重,去标签,然后利用lucene 和 solr 进行索引和搜索。 课程的最大特点是内容新颖全面而又通俗易懂...
NULL 博文链接:https://love-66521.iteye.com/blog/2039912
NULL 博文链接:https://sharp-fcc.iteye.com/blog/2038339
lucene,solr的使用lucene,solr的使用lucene,solr的使用lucene,solr的使用lucene,solr的使用lucene,solr的使用lucene,solr的使用lucene,solr的使用lucene,solr的使用lucene,solr的使用lucene,solr的使用
Lucene4.6版本,适用于Lucene的所有研究,以及中文分词功能
lucene3.0.3搜索的使用示例lucene3.0.3搜索的使用示例lucene3.0.3搜索的使用示例
Lucene索引的基本操作,包括添加和更新操作及测试代码;
LUCENE搜索引擎基本工作原理 详细介绍了搜索引擎的工作原理
lucene、lucene.NET详细使用与优化详解lucene、lucene.NET详细使用与优化详解
Lucene创建索引,查询索引的简单使用。
Lucene.Net基本用法.pdf
lucene
lucene.net基本应用(doc) lucene.net基本应用(doc) lucene.net基本应用(doc)