elasticsearch - 如何删除与foreach处理器中的条件匹配的特定字段?

标签 elasticsearch

这是我的映射定义。

{
    "mappings": {
        "properties": {
            "title": {
                "properties": {
                    "cell_type": {
                        "type": "text"
                    },
                    "content": {
                        "type": "text"
                    }
                }
            },
            "question": {
                "properties": {
                    "cell_type": {
                        "type": "text"
                    },
                    "format": {
                        "type": "text"
                    },
                    "content": {
                        "type": "text"
                    }
                }
            },
            "answer": {
                "properties": {
                    "cell_type": {
                        "type": "text"
                    },
                    "format": {
                        "type": "text"
                    },
                    "content": {
                        "type": "text"
                    }
                }
            },
            "tags": {
                "type": "keyword"
            }
        }
    }
}
我认为在大多数情况下,问题和答案字段都是数组。但是,当格式字段具有多行时,我想避免索引内容字段。
因此,我定义了以下管道来删除内容字段。
{
    "description": "remove content field",
    "processors": [
        {
            "foreach": {
                "field": "question",
                "processor": {
                    "remove": {
                        "field": "_ingest._value.content",
                        "if": "ctx.format == 'multiline'"
                    }
                }
            }
        },
        {
            "foreach": {
                "field": "answer",
                "processor": {
                    "remove": {
                        "field": "_ingest._value.content",
                        "field": "ctx.content"
                    }
                }
            }
        }
    ]
}
即使格式字段设置为“多行”,该文档仍然具有内容字段。这种情况似乎不符合我的预期。
是否可以使用foreach处理器中的任何变量访问内容字段,或者删除问题或答案数组中的内容字段?
我正在使用Elasticsearch 7.8。

最佳答案

我将使用script处理器代替一个更简单的方法:

{
    "description": "remove content field",
    "processors": [
        {
          "script": {
            "source": """
            ctx.question.stream()
              .filter(x -> x.format == 'multiline')
              .forEach(x -> x.remove('content'));
            ctx.answer.stream()
              .filter(x -> x.format == 'multiline')
              .forEach(x -> x.remove('content'));
            """
          }
        }
    ]
}

关于elasticsearch - 如何删除与foreach处理器中的条件匹配的特定字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62714394/

相关文章:

elasticsearch - 文件计数不一致

php - Elasticsearch Php日期映射,过滤和搜索

java - 找不到任何可执行的 java 二进制文件

sorting - 类别值的elasticsearch排序聚合

ElasticSearch:如何编写字符串字段为 null 或空的查询?

java - 从 Java 应用程序将数据插入到 elastic-search

couchdb - Elasticsearch Couchdb河监控器

solr - 如果WildcardQuery不影响文档的评分,为什么它会不断返回0.5?

spring-boot - 使用 Spring Boot 连接到 Elasticsearch 6.2.3

ruby - 如何使用 elasticsearch-model 创建索引,批量索引不起作用