【问题标题】:Reject numeric values to be indexed for fields with text/keyword data type ( ElaticSearch 6.2.3 )拒绝为文本/关键字数据类型(ElasticSearch 6.2.3)的字段编制索引的数值
【发布时间】:2020-04-03 13:41:35
【问题描述】:

文本或关键字数据类型允许插入数值。

索引模板如下图:

{
    "index_patterns" : [ "temp-index" ],
    "mappings": {
        "docs" : {
            "properties": {
                "username" : {
                    "type": "keyword"
                }
            }
        }
    }
}

文档插入:

{
    "username" : 10
}

由于上述文档插入成功,是否可以拒绝文本/关键字数据类型的此类数值。

【问题讨论】:

    标签: elasticsearch elasticsearch-6 elasticsearch-mapping


    【解决方案1】:

    您可以使用coerce 设置,但我认为它只能反之亦然——即不允许将字符串索引为数字:

    PUT roddo
    {
      "mappings": {
        "properties": {
          "username": {
            "type": "integer",
            "coerce": false
          }
        }
      }
    }
    

    这样,尝试

    POST roddo/_doc
    {
      "username": "10"
    }
    

    会抛出Integer value passed as String 错误。


    或者,您可以扩展用户名功能:

    PUT roddo2
    {
      "mappings": {
        "properties": {
          "username": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              },
              "integer": {
                "type": "integer",
                "coerce": true,
                "ignore_malformed": true
              }
            }
          }
        }
      }
    }
    

    它会尝试强制,但在失败时不会抛出异常。

    同步您的文档后,您将能够找到那些没有具有整数用户名的文档:

    GET roddo2/_search
    {
      "query": {
        "bool": {
          "must_not": {
            "exists": {
              "field": "username.integer"
            }
          }
        }
      }
    }
    

    【讨论】:

    • 嗨,乔,我想要文本/关键字完全相同的东西。强制不适用于文本/关键字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多