Lucene创建索引的概念

版权声明:write by DGHxj https://blog.csdn.net/DGHxj_/article/details/84110545

查询(Query):对于全文搜索,最终都是使用词项指向一批document文档对象集合,利用对

                           词项的逻辑计算可以实现不同的查询功能;查询时构建的对象就是Query;

文档(document):是索引文件中的一个最小的数据单位,例如非结构化数据中的网页将会封装成

                                 一个document存储在索引文件中,而封装过程中写在对象里的所有数据都会根据逻辑

                                 进行分词计算,不同的结构源数据会对应创建具有不同属性的document对象

文档的域(Field):每个文档对象根据不同的数据来源封装Field的名称,个数和数据,导致document

                                的结构可能各不相同

词条化(tokenization):计算分词的过程

词项(Term):计算分词的结果每一个词语都是一个语项

创建一个索引文件(空)

      指向一个索引文件的位置(指向操作系统的一个文件夹)

      生成输出对象,进行输出

	@Test
	public void emptyIndex() throws Exception{
		//指向一个文件夹位置
		Path path=Paths.get("./index01");
		Directory dir=FSDirectory.open(path);
		//生成一个数出对象writer需要分词计算器,配置对象
		Analyzer analyzer=new IKAnalyzer6x();//中文分词器
		IndexWriterConfig config=new IndexWriterConfig(analyzer);
		IndexWriter writer=new IndexWriter(dir, config);
		//写出到磁盘,如果没有携带document,生成一个空的index文件
		writer.commit();
	}

在索引中创建数据

      将源数据读取封装成document对象,根据源数据结构定义document的给中field;

@Test
public void createDate() throws Exception {
	/**
	 * 指向一个索引文件 生成输出对象 封装document对象(手动填写数据) 将document添加到输出对象索引文件的输出
	 */
	// 指向一个文件夹位置
	Path path = Paths.get("./index02");
	Directory dir = FSDirectory.open(path);
	// 生成一个输出对象writer需要分词计算器,配置对象
	Analyzer analyzer = new IKAnalyzer6x();// 中文分词器
	IndexWriterConfig config = new IndexWriterConfig(analyzer);
	IndexWriter writer = new IndexWriter(dir, config);
	// 构造document对象
	Document document1 = new Document();// 新闻、作者、内容、网站链接地址
	Document document2 = new Document();// 商品页面、title、price、详情、图片等
	document1.add(new TextField("auther", "韩寒", Store.YES));
	document1.add(new TextField("content", "我是上海大金子", Store.NO));
	document1.add(new StringField("address", "http://www.baidu.com", Store.YES));
	document2.add(new TextField("title", "三星(SAMSUNG) 1TB Type-c USB3.1 移动固态硬盘", Store.YES));
	document2.add(new TextField("price", "1699", Store.YES));
	document2.add(new TextField("desc", "不怕爆炸你就买", Store.YES));
	document2.add(new StringField("image", "image.jt.com/1/1.jpg", Store.YES));
	//将2个document对象添加到writer中写出到索引文件;
	writer.addDocument(document1);
	writer.addDocument(document2);
	//写出到磁盘,如果没有携带document,生成一个空的index文件
	writer.commit();
}

利用软件观察索引内容

      问题1:Store.YES和NO的区别是什么?

      解:Store的yes和no的区别在于,创建索引数据时,field数据是否输出到索引时存储在索引文件,按照

             域的类型进行计算分词。

             在索引中,一些过大的数据,查询不需要的数据可以不存储在索引文件中;

             例如网页内容;计算不计算分词,和存储在索引没关系

      问题2:StringField和TextField的区别是什么

      解:域的数据需要进行分词计算,如果是字符串有两种对应的域类型

             其中StringField表示不对数据进行分词计算,以整体形势计算索引

             TextField表示对数据进行分词计算,以词项形势计算索引;

 

      问题3:显然document中的不同域field应该保存不同的数据类型

               IntPoint

               LongPoint

               DoublePoint

               FloatPoint

      解: 数据中的类型不同,存储的数据计算逻辑也不同;

             int long double的数字数据如果使用字符串类型保存域

             只能做到一件事--存储在索引文件上

             以上几个Point类型的域会对数据进行二进制数字的计算;

             范围查找,只要利用intPoint,longPoint对应域存储到document对象后

             这种类型的数据在分词计算中就具有了数字的特性 > <

 

             intPoint只能存储数值,不存储数据

             如果既想记性数字特性的使用,又要存储数据;需要使用StringField类型

             构造一个同名的,同值域

猜你喜欢

转载自blog.csdn.net/DGHxj_/article/details/84110545
今日推荐