ElasticSearch Spring - 使用 @Mapping 注释仅对一组字段禁用 date_detection,而不是对整个索引

2024-01-09

我正在尝试禁用索引中一组字段的 date_detection 。下面是映射

{
  "my-index" : {
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "customFields" : {
          "properties" : {
            "firstName" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "lastName" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "address" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "dateOfBirth" : {
              "type" : "date"
            }
          }
        },
        "key" : {
          "type" : "long"
        },
        "updatedDate" : {
          "type" : "date",
          "format" : "basic_date_time"
        }
      }
    }
  }
}

我想要dateOfBirth字段类型为text, not date.

所以我做了以下事情:

我创建了一个mappings.json文件(见下文)并使用 @Mapping(mappingPath = "mappings.json") 注释

{
  "date_detection": false
}

现在,这确实禁用了date_detection但它也迫使updatedDate属于类型text这会导致一些错误。

这些是updatedDate and customFields我的索引类中的变量:

@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
Instant updatedDate;

Map<String, Object> customFields;

有没有办法禁用date_detection对于里面的字段customFields这样只有dateOfBirth字段的类型更改为text并不是updatedDate?


阅读文档位于https://www.elastic.co/guide/en/elasticsearch/reference/8.2/dynamic-field-mapping.html#_disabling_date_detection https://www.elastic.co/guide/en/elasticsearch/reference/8.2/dynamic-field-mapping.html#_disabling_date_detection 日期检测是针对每个索引的,不可针对各个字段进行配置。

如果您使用映射文件,Spring Data Elasticsearch 不会从实体创建映射,而仅使用该文件,因此您的@Field注释被忽略。

保持@Field上的注释updatedDate and add

@Mapping(dateDetection = Mapping.Detection.FALSE)

到您的实体类(带有注释的实体类)@Document)。删除对mapping.json 的引用。

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

ElasticSearch Spring - 使用 @Mapping 注释仅对一组字段禁用 date_detection,而不是对整个索引 的相关文章

随机推荐