孔浩Lucene创建和域选项
来源:互联网 发布:2016年青少年犯罪数据 编辑:程序博客网 时间:2024/05/18 02:50
一、域索引选项:
//Field.Index(索引域选项) //Index.ANALYZED:进行分词和索引,适用于标题、内容等 //Index.NOT_ANALYZED:进行索引,但是不进行分词,如果身份证号,姓名,ID等,适用于精确搜索 //Index.ANALYZED_NOT_NORMS:进行分词但是不存储norms信息,这个norms中包括了创建索引的时间和权值等信息 //Index.NOT_ANALYZED_NOT_NORMS:即不进行分词也不存储norms信息 //Index.NO:不进行索引
二、域存储选项:
//Field.Store.YES或者NO(存储域选项) //设置为YES表示或把这个域中的内容完全存储到文件中,方便进行文本的还原 //设置为NO表示把这个域的内容不存储到文件中,但是可以被索引,此时内容无法完全还原(doc.get)
三、最佳实践:
package org.it.index;import java.io.File;import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;@SuppressWarnings("all")public class IndexUtil {private String[] ids={"1","2","3","4","5","6"};private String[] emails={"aa@itat.org","bb@itat.org","cc@cc.org","dd@sina.org","dd@sohu.org","ee@qq.org","ff@xiao.org"};private String[] contents={"welcome to visited the space","hello boy","my name is cc","I like movie and swim","jijoij","wwwww","jjjjjjjjjjjj"};private int[] attachs ={2,3,4,3,5,6};private String[] names={"shanghai","zhangsan","john","mike","jake","jakedd"}; //1.创建Directoryprivate Directory directory=null;public IndexUtil(){try {directory=FSDirectory.open(new File("d:/lucene/index02"));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} public void query(){try {IndexReader reader = IndexReader.open(directory);//存储的文档数<span style="color:#FF0000;">//小知识点:maxDoc 和 numDocs()方法的区别:maxDoc()//返回索引中删除和未被删除的文档总数,后者返回索引中未被删除的文档总数</span>System.out.println("numDocs "+reader.numDocs());//存储的文档总数System.out.println("maxDocs "+reader.maxDoc());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void index(){//创建IndexWriterIndexWriter writer=null;try {writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_4_9,new StandardAnalyzer(Version.LUCENE_4_9)));// 3.创建Document对象Document doc=null;for(int i=0;i<ids.length;i++){doc=new Document();// 4.为Document添加Fielddoc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); doc.add(new Field("emails",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); doc.add(new Field("contents",contents[i],Field.Store.NO,Field.Index.NOT_ANALYZED)); doc.add(new Field("names",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));// 4通过IndexWriter 添加文档到索引中 writer.addDocument(doc);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if (writer != null) { try {// 关闭流writer.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }
以上内容主要是回顾创建索引---》怎么设置域--》查询索引
还有域存储 域索引的概念
查询的时候用IndexReader:
IndexReader reader = IndexReader.open(directory);
举一反三:在上一节中的HelloLucene中,
doc.add(new Field("content", new FileReader(file)));
对于这行代码,内容默认的是没有域存储选项,那么我怎么实现将内容存储到域存储中去呢?
传统方法Reader----->BufferReader,现在我们这样用:
加载jar包: commons-io-2.4.jar
利用工具类,先转换为字符串就OK,其它步骤和id,path一样存储实现的。
String content=FileUtils.readFileToString(file);System.out.println(content);
0 0
- 孔浩Lucene创建和域选项
- lucene 索引创建域选项
- Lucene之索引的创建和域选项-yellowcong
- lucene索引_创建_域选项和lucene索引_的删除、更新.
- Lucene--003索引创建、域选项
- Lucene索引创建之域选项介绍,Field.Store和Fiele.Index
- lucene学习笔记3-索引-创建域选项
- Lucene创建和搜索
- Lucene的创建和查询
- Lucene教程(三) 索引域选项
- Lucene教程(三) 索引域选项
- Lucene学习笔记之域选项
- Lucene 6教程(三) 索引域选项
- Lucene(三)索引域选项
- Lucene.Net 创建索引和检索
- lucene一般创建index和indexsearch例子
- Lucene 入门之创建索引和搜索
- lucene创建索引初步和搜索初步
- 算法题集——001
- Eclipse部署项目到tomcat,获取项目路径
- VMware下安装MS-DOS7.10
- java简单实现webservice接口
- 算法导论1:第一章算法的作用
- 孔浩Lucene创建和域选项
- HDOJ 题目2717 Catch That Cow(BFS)
- Git的使用
- [LeetCode] Recover Binary Search Tree
- 链表(链接)
- PCB电路设计问题总结
- mongoDB
- 掌握数据整体状态--小结
- Android代码混淆