不在映射中的字段包含在 ElasticSearch 返回的搜索结果中

2024-03-15

我想使用 Tire gem 作为 ElasticSearch 的客户端来索引 pdf 附件。在我的映射中,我从 _source 中排除附件字段,以便附件不会存储在索引中,并且未在搜索结果中返回:

mapping :_source => { :excludes => ['attachment_original'] } do
  indexes :id, :type => 'integer'
  indexes :folder_id, :type => 'integer'
  indexes :attachment_file_name
  indexes :attachment_updated_at, :type => 'date'
  indexes :attachment_original, :type => 'attachment'
end 

当我运行以下curl命令时,我仍然可以看到搜索结果中包含的附件​​内容:

curl -X POST "http://localhost:9200/user_files/user_file/_search?pretty=true" -d '{
  "query": {
    "query_string": {
      "query": "rspec"
    }
  }
}'

我已在此发布我的问题thread https://stackoverflow.com/questions/11873248/how-to-prevent-attachments-from-being-stored-in-source-with-elasticsearch-and-t/11991205#comment16005481_11991205:

但我刚刚注意到,搜索结果中不仅包含附件,还包含所有其他字段,包括未映射的字段,如下所示:

{
  "took": 20,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.025427073,
    "hits": [
      {
        "_index": "user_files",
        "_type": "user_file",
        "_id": "5",
        "_score": 0.025427073,
        "_source": {
          "user_file": {
            "id": 5,
            "folder_id": 1,
            "updated_at": "2012-08-16T11:32:41Z",
            "attachment_file_size": 179895,
            "attachment_updated_at": "2012-08-16T11:32:41Z",
            "attachment_file_name": "hw4.pdf",
            "attachment_content_type": "application/pdf",
            "created_at": "2012-08-16T11:32:41Z",
            "attachment_original": "JVBERi0xLjQKJeLjz9MKNyA"
          }
        }
      }
    ]
  }
}

attachment_file_size and attachment_content_type未在映射中定义,但在搜索结果中返回:

{
  "id": 5,
  "folder_id": 1,
  "updated_at": "2012-08-16T11:32:41Z",
  "attachment_file_size": 179895, <---------------------
  "attachment_updated_at": "2012-08-16T11:32:41Z",
  "attachment_file_name": "hw4.pdf", <------------------
  "attachment_content_type": "application/pdf",
  "created_at": "2012-08-16T11:32:41Z",
  "attachment_original": "JVBERi0xLjQKJeLjz9MKNyA"
}

这是我的完整实现:

  include Tire::Model::Search
  include Tire::Model::Callbacks

  def self.search(folder, params)
    tire.search() do
      query { string params[:query], default_operator: "AND"} if params[:query].present?
      #filter :term, folder_id: folder.id
      #highlight :attachment_original, :options => {:tag => "<em>"}
      raise to_curl
    end
  end

  mapping :_source => { :excludes => ['attachment_original'] } do
    indexes :id, :type => 'integer'
    indexes :folder_id, :type => 'integer'
    indexes :attachment_file_name
    indexes :attachment_updated_at, :type => 'date'
    indexes :attachment_original, :type => 'attachment'
  end

  def to_indexed_json
     to_json(:methods => [:attachment_original])
   end

  def attachment_original
    if attachment_file_name.present?
      path_to_original = attachment.path
      Base64.encode64(open(path_to_original) { |f| f.read })
    end    
  end

有人可以帮我弄清楚为什么所有字段都包含在_source?

Edit:这是运行的输出localhost:9200/user_files/_mapping

{
  "user_files": {
    "user_file": {
      "_source": {
        "excludes": [
          "attachment_original"
        ]
      },
      "properties": {
        "attachment_content_type": {
          "type": "string"
        },
        "attachment_file_name": {
          "type": "string"
        },
        "attachment_file_size": {
          "type": "long"
        },
        "attachment_original": {
          "type": "attachment",
          "path": "full",
          "fields": {
            "attachment_original": {
              "type": "string"
            },
            "author": {
              "type": "string"
            },
            "title": {
              "type": "string"
            },
            "name": {
              "type": "string"
            },
            "date": {
              "type": "date",
              "format": "dateOptionalTime"
            },
            "keywords": {
              "type": "string"
            },
            "content_type": {
              "type": "string"
            }
          }
        },
        "attachment_updated_at": {
          "type": "date",
          "format": "dateOptionalTime"
        },
        "created_at": {
          "type": "date",
          "format": "dateOptionalTime"
        },
        "folder_id": {
          "type": "integer"
        },
        "id": {
          "type": "integer"
        },
        "updated_at": {
          "type": "date",
          "format": "dateOptionalTime"
        }
      }
    }
  }
}

正如您所看到的,由于某种原因,所有字段都包含在映射中!


In your to_indexed_json,您包括attachment_original方法,因此被发送到elasticsearch。这也是所有其他属性都包含在映射中并因此包含在源中的原因。

See the ElasticSearch 和 Tire:使用映射和 to_indexed_json https://stackoverflow.com/questions/11672072/elasticsearch-tire-using-mapping-and-to-indexed-json问题以获取有关该主题的更多信息。

看来 Tire 确实将正确的映射 JSON 发送到 elasticsearch ——我的建议是使用Tire.configure { logger STDERR, level: "debug" }检查正在发生的情况并在原始级别上查明问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不在映射中的字段包含在 ElasticSearch 返回的搜索结果中 的相关文章

随机推荐

  • 找不到 Pyspark 模块

    我正在尝试在 Yarn 中执行一个简单的 Pyspark 作业 这是代码 from pyspark import SparkConf SparkContext conf SparkConf setMaster yarn client set
  • Bootstrap 弹出框在 AngularJs ng-repeat 中不起作用

    我有一个国家 地区列表 我正在使用 ng repeat 填充这些国家 地区列表 一切正常 另外 我试图通过使用引导弹出窗口来显示每个国家 地区内的一些其他详细信息 但它似乎不起作用 所以有人可以帮助我吗 网页代码 div class spa
  • PHP 和 mySQL:什么时候使用 htmlentities?

    平台 PHP 和 MySQL 出于实验目的 我自己在自己的网站上尝试了一些 XSS 注入 考虑这种情况 我有表单文本区域输入 由于这是一个文本区域 我可以输入文本和各种 英语 字符 以下是我的观察 A 如果我在将数据插入数据库之前仅应用 s
  • 如何检查 uint8_t 是否作为类型存在,而不是 unsigned char?

    我有两个编译器 一种可以识别 uint8 t GCC ARM EABI 另一种则不能 Renesas M16 标准工具链 Renesas 工具链不兼容 ANSI C 因此您可以丢弃 因此 uint8 t uint16 t 未定义为现有类型
  • 如何搜索 git 存储库历史记录以查找合并错误?

    在我们过去的某个时刻 git 的开发分支被合并了 然而 做出了错误的合并决定 因此一些代码没有进入我们预期的主分支 在最终合并到主分支之前 不同分支进行了多次合并 因此分支和合并历史相当复杂 有没有一种简单的方法来搜索 git 存储库以确定
  • 在 R 中创建默认评论标题模板?

    是否可以在 R 中为所有新脚本创建默认注释标题模板 我通常在所有脚本的顶部包含一些标准信息 并希望自动执行创建评论标题的过程 例如 Project Script purpose Date Author 按照上面 lmo 的建议 我通过编辑位
  • Pandas 滚动窗口 - datetime64[ns] 未实现

    我正在尝试使用 Python Pandas 构建一些图表 我有每秒采样的数据 这是一个示例 Index Time Value 31362 1975 05 07 07 59 18 36 151612 31363 1975 05 07 07 5
  • WEBHDFS REST API 将文件从 Windows 服务器/本地文件夹/桌面复制/移动到 HDFS

    使用 WEBHDFS REST API 调用 我可以将文件从 Windows 计算机 即 Windows 服务器或 Windows 本地文件夹或桌面 传输或复制到 Hadoop HDFS 文件系统吗 如果是 有任何示例命令信息吗 我已经尝试
  • 分解量子态

    我正在寻找采用由位组成的加权经典状态之和组成的任意量子态的算法 如下所示 0000 gt 2 0011 gt 2 0100 gt 2 0111 gt 2 并使用张量积将其分解为更紧凑的形式 如下所示 0 gt x 0 gt 1 gt x 0
  • 循环或重复一组任务直到成功

    我目前有一个包含任务文件的剧本 在该任务文件中 我想检查一个条件 如果该条件的退出代码不等于 0 则应重复任务文件中的所有步骤 我已经尝试了块和循环的一些变体 但我还没有找到一种方法来使它执行我上面描述的操作 目前我有这样的事情 tasks
  • eax如何存储大小大于4字节的返回值?

    EAX在32位平台上用于存储函数的返回值 我想知道如果函数的返回值的大小大于4个字节 eax如何处理 在这种情况下 操作系统可以将返回值保存在堆栈上 并将堆栈地址存储在EAX中 但是操作系统如何判断EAX中存储的值是返回值的地址还是实际上是
  • 单括号和双括号 Numpy 数组的区别?

    这两个 numpy 对象有什么区别 import numpy as np np array 0 0 0 0 np array 0 0 0 0 In 71 np array 0 0 0 0 shape Out 71 1 4 In 72 np
  • 在 [FINE UPLOADER] 中显示以前上传的图像

    我正在使用精细的上传器插件来上传图像 图片上传工作正常 我想做的是 当图像上传后刷新页面时 精细上传器应该显示以前上传的图像 这是我的代码 accordion on shown bs collapse function activeShop
  • 如何在 Laravel Eloquent 中使用带有子查询的内连接

    注意 这是 laravel 5 3 基本上 当用户选择阿拉伯语翻译时 我正在运行查询 完整的 sql 如下所示 select s ref t text as ref ar FROM stores AS s INNER JOIN SELECT
  • 在 SharePoint 2013 中以编程方式创建文件夹

    目前我有在中创建文件夹的代码Documents运行时目录 using var context new Microsoft SharePoint Client ClientContext sharePointSite context Cred
  • 方案中的河内塔(递归)

    今天在scheme中写了如下代码 但是求值错误 请不要告诉我我编程很糟糕 我知道这是一个经典的递归问题 但我遇到了麻烦 define towers of hanoi n source temp dest if n 1 begin displ
  • 解析 ISO 8601 值 24:00:00 的日期失败

    我正在尝试解析来自数据源的传入日期 无法更改 它给了我 ISO 8601 格式示例的时间 2007 04 05T24 00 然而在 Net 中它无法将其解析为有效时间 维基百科指出它应该是有效的格式 维基百科 ISO 8601 http e
  • 更少的 mixin 和变量

    我有以下混合 iconFont color green font size 18px color color font size font size 如果我只想更改第二个变量值 我需要编写第一个变量默认值吗 h1 iconFont gree
  • Drupal 7在自定义主题中覆盖jquery js文件

    是否可以重写 覆盖自定义模板脚本变量中使用的默认 Drupal 7 26 jquery 我的意思是js文件之一 通过自定义主题来的一个 我试过这个sites all MYTPL template php但它不起作用 scripts misc
  • 不在映射中的字段包含在 ElasticSearch 返回的搜索结果中

    我想使用 Tire gem 作为 ElasticSearch 的客户端来索引 pdf 附件 在我的映射中 我从 source 中排除附件字段 以便附件不会存储在索引中 并且未在搜索结果中返回 mapping source gt exclud