Lucene&&Solr中的域(Filed)总结

简介: Lucene&&Solr中的域(Filed)总结

引言

 

Field类是文档索引期间很重要的类,控制着被索引的域值,下面先来看几种常用的域类型:



20170802202603453.png


下面对上面几个属相进行介绍

 

是否分词:


  分词的作用是为了索引

  需要分词: 文件名称, 文件内容

  不需要分词: 不需要索引的域不需要分词,还有就是分词后无意义的域不需要分词 比如: id, 身份证号

 

是否索引:


  索引的的目的是为了搜索.

  需要搜索的域就一定要创建索引,只有创建了索引才能被搜索出来

  不需要搜索的域可以不创建索引

  需要索引: 文件名称, 文件内容, id, 身份证号等

  不需要索引: 比如图片地址不需要创建索引, e:\\xxx.jpg,因为根据图片地址搜索无意义

是否存储:


存储的目的是为了显示.


是否存储看个人需要,存储就是将内容放入Document文档对象中保存出来,会额外占用磁盘空间, 如果搜索的时候需要马上显示出来可以放入document中也就是要存储,这样查询显示速度快, 如果不是马上立刻需要显示出来,则不需要存储,因为额外占用磁盘空间不划算.

 

在lucene中使用域


               File[] listFiles = f.listFiles();
    for (File file : listFiles) {
      // 第三步创建document对象
      Document document = new Document();
      String file_name = file.getName();
      // 创建域
      Field fileNameField = new TextField("fileName", file_name, Store.YES);
      long file_size = FileUtils.sizeOf(file);
      Field fileSizeField = new LongField("fileSize", file_size, Store.YES);
      // 文件路径
      String file_path = file.getPath();
      Field filePathField = new StoredField("filePath", file_path);
      // 文件内容
      String file_content = FileUtils.readFileToString(file);
      Field fileContentField = new TextField("fileContent", file_content, Store.NO);
      document.add(fileNameField);
      document.add(fileSizeField);
      document.add(filePathField);
      document.add(fileContentField);
      // 第四步 使用 indexwriter对象将docum对象写人索引库,此过程进行索引创建。并将索引和document对象写入索引库
      indexWriter.addDocument(document);
    }

 Solr中域的介绍

 

域的使用

 

我们在添加索引的时候,使用域必须是在配置文件中配置的,如果我们使用的索引在配置文件中不存在,将会报错,索引添加失败,此时如果我们的需求要求我们必须使用这个域名,则我们需要自己在配置文件添加这个域的定义,在对应的collection下面的schema.xml文件中添加:


        <field name="item_title" type="text_ik" indexed="true" stored="true"/>
  <field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>
  <field name="item_price" type="float" indexed="true" stored="true"/>
  <field name="item_num" type="int" indexed="true" stored="true"/>
  <field name="item_image" type="string" indexed="false" stored="true"/>

动态域

 

为了更好的满足我们在项目中的需求,在原有的配置文件中,还有一些域采取了通配符的模式定义,这些域被称为动态域,只要满足这些域的模式,即可使用

 <dynamicField name="*_i"  type="int"    indexed="true"  stored="true"/>
   <dynamicField name="*_is" type="int"    indexed="true"  stored="true"  multiValued="true"/>
   <dynamicField name="*_s"  type="string"  indexed="true"  stored="true" />
   <dynamicField name="*_ss" type="string"  indexed="true"  stored="true" multiValued="true"/>
   <dynamicField name="*_l"  type="long"   indexed="true"  stored="true"/>
   <dynamicField name="*_ls" type="long"   indexed="true"  stored="true"  multiValued="true"/>
   <dynamicField name="*_t"  type="text_general"    indexed="true"  stored="true"/>
   <dynamicField name="*_txt" type="text_general"   indexed="true"  stored="true" multiValued="true"/>
   <dynamicField name="*_en"  type="text_en"    indexed="true"  stored="true" multiValued="true"/>
   <dynamicField name="*_b"  type="boolean" indexed="true" stored="true"/>
   <dynamicField name="*_bs" type="boolean" indexed="true" stored="true"  multiValued="true"/>

复制域

 

复制域的目的是将多个域合并为一个域,这样我们按照多个条件查询的时候,可以值发送一个get请求就可以完成,如果我们定义了复制域,在我们保存数据的时候,我们只负责保存两个域,但是实际上在索引库中存储了三个域,复制域就是solr内部自动合并的,定格式如下:

        <field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
  <copyField source="item_title" dest="item_keywords"/>
  <copyField source="item_sell_point" dest="item_keywords"/>

自定义域类型

  <!-- IKAnalyzer -->
  <fieldType name="text_ik" class="solr.TextField">
    <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
  </fieldType>


上面代码自定义了一个基于IK中文分词器的域类型。


小结

 

域是全文搜索的一个基础,所以我们需要好好掌握这个概念,因为lucene是solr的基础,所以在这小编将他们两个放在一起介绍了,后面博客中会继续讲解solr的相关知识

目录
相关文章
leetcode-72:编辑距离
leetcode-72:编辑距离
93 0
|
12月前
|
前端开发 开发者
css两种盒子模型
在CSS中,有两种盒子模型:标准盒子模型和IE盒子模型(怪异盒子模型)。标准盒子模型中,`width`和`height`仅指内容区的尺寸,总宽度和高度还包括内边距、边框和外边距。IE盒子模型中,`width`和`height`包括内容区、内边距和边框,总宽度和高度仅加外边距。通过`box-sizing`属性可以切换这两种模型,`box-sizing: content-box;`表示标准盒子模型,`box-sizing: border-box;`表示IE盒子模型。
|
Linux KVM 虚拟化
10-25|我只想可以修改容器内的时间而不影响外部时间怎么办啊
10-25|我只想可以修改容器内的时间而不影响外部时间怎么办啊
|
网络协议 程序员
TCP报文格式全解析:网络小白变高手的必读指南
**TCP报文格式详解摘要** 探索TCP,传输层的关键协议,提供可靠数据传输。报文含源/目的端口(标识应用),32位序号(跟踪字节顺序),确认序号(确认接收),4位首部长度,6位标志(URG, ACK, PSH, RST, SYN, FIN),窗口大小(流量控制),检验和(数据完整性),紧急指针(优先数据)及可变长选项(如MSS, 时间戳)。了解这些字段,能更好地理解TCP连接的建立、管理和数据交换。
921 3
|
机器学习/深度学习 算法 关系型数据库
【博士每天一篇文献-实验】Is a Modular Architecture Enough?
本文探讨了模块化架构在机器学习中的有效性,通过实验展示了模块化系统在特定任务中通过专业化显著提升性能的潜力,并提出了评估模块化架构性能的新指标,同时指出了当前系统在泛化和性能上的局限性。
72 2
【博士每天一篇文献-实验】Is a Modular Architecture Enough?
|
人工智能 Go
Go 等待协程完成
Go 等待协程完成
71 0
|
负载均衡 算法 Java
SDK并发调用优化方案
SDK并发调用优化方案
|
存储 搜索推荐 Java
排序之计数排序
排序之计数排序
126 0
|
网络协议 Devops 大数据
【分布式】大型互联网项目特点
【1月更文挑战第25天】【分布式】大型互联网项目特点
|
存储 自然语言处理 搜索推荐
Elasticsearch 学习笔记(一)-----Lucene的简介以及索引原理
今天,正式开始学习Elasticsearch,因为Elasticsearch是用Lucene来实现索引的查询功能的,所以,理解Lucene的原理显的尤为重要。
834 0
Elasticsearch 学习笔记(一)-----Lucene的简介以及索引原理