簡體   English   中英

如何防止某些字段在 Elasticsearch 中被索引

[英]How to prevent certain fields from get indexed in Elasticsearch

我需要防止某些具有“null”(null 作為字符串)和“”(空字符串)等值的字段在 Elasticsearch 中編入索引,即我應該能夠獲取文檔中的其余字段,但在 _source 中具有此類值的字段除外. 我正在使用規范化器如下

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

}

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

PS:-我使用的是elasticsearch 7.6.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 == \"\""
          }
       }
   ]
}

然后,您可以在索引請求中指定管道,也可以將其作為default_pipelinefinal_pipeline在您的索引設置中 您還可以在索引模板中指定此設置。

(腳本)循環方法

如果您不想編寫一長串刪除操作,可以嘗試使用腳本處理器,如下所示:

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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM