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的相关知识

目录
相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8083 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10374 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1567 5
|
9天前
|
云安全 数据采集 人工智能
|
17天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2716 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2490 0