【问题标题】:How to prevent certain fields from get indexed in Elasticsearch如何防止某些字段在 Elasticsearch 中被索引
【发布时间】:2021-02-19 07:28:51
【问题描述】:

我需要防止某些具有“null”(null 作为字符串)和“”(空字符串)等值的字段在 Elasticsearch 中被编入索引,即我应该能够获取文档中的其余字段,但具有此类的字段除外_source 中的值。 我正在使用如下规范化器

{
"analysis": {
    "normalizer": {
        "my_normalizer": {
            "filter": [
                "uppercase"
            ],
            "type": "custom"
        }
    }
}

}

上面或字段映射中是否需要任何设置?

P.S:- 我使用的是 elasticsearch 7.6.1

【问题讨论】:

标签: elasticsearch elasticsearch-mapping


【解决方案1】:

您可以查看Elasticsearch Pipelines。它们在索引(以及您的案例分析)发生之前应用。

具体来说,您可以添加一个 Elasticsearch Pipeline,如果它们满足您列出的条件,则删除必填字段。比如:

PUT _ingest/pipeline/remove_invalid_value
{
   "description": "my pipeline that removes empty string and null strings",
   "processors": [
       { 
          "remove": {
              "field": "field1",
              "ignore_missing": true,
              "if": "ctx.field1 == \"null\" || ctx.field1 == \"\""
          }
       },
        { 
          "remove": {
              "field": "field2",
              "ignore_missing": true,
              "if": "ctx.field2 == \"null\" || ctx.field2 == \"\""
          }
       },
       
        { 
          "remove": {
              "field": "field3",
              "ignore_missing": true,
              "if": "ctx.field3 == \"null\" || ctx.field3 == \"\""
          }
       }
   ]
}

然后,您可以在index request 中指定管道,或者将其作为default_pipeline 或final_pipeline 放在index settings 中。您也可以在索引模板中指定此设置。

(脚本)循环方法

如果您不想编写一长串删除操作,可以尝试使用脚本处理器,如下所示:

PUT _ingest/pipeline/remove_invalid_fields
{
  "description": "remove fields",
  "processors": [
    {
      "script": {
        "source": """
          for (x in params.to_delete_on_condition) {
                if (ctx[x] == "null" || ctx[x] == "") {
                    ctx.remove(x);
                }
          }
          """,
        "params": {
          "to_delete_on_condition": [
            "field1",
            "field2",
            "field3"
          ]
        }
      }
    }
  ]
}

它遍历列表并在条件匹配时删除该字段。

访问脚本中的嵌套字段并不像许多答案中所报告的那样微不足道,但它应该是可行的。这个想法是nested.field 应该作为ctx['nested']['field'] 访问。

【讨论】:

  • 感谢@Briomkez 的回复。我在索引中有多个字段。那么有没有办法将此处理器应用于文档中的所有字段。
  • 查看我的编辑。我试图解决您的评论问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
相关资源
最近更新 更多