3

我们使用动态模板来指定索引中某些字段的映射。模板中没有提到大多数文本字段,并获取到分析字段和关键字子字段的默认映射:

                      "someField" : {
                        "type" : "text",
                        "fields" : {
                          "keyword" : {
                            "type" : "keyword"
                          }
                        }
                      }

这很好。

索引有一些我们不想分析的字段,只是做了关键字。但是,为了保持一致性,我们希望所有关键字字段都是名为“关键字”的子字段,如上所示。所以这:

"dynamic_templates" : [

  {
    "someField" : {
      "match" : "someField",
      "mapping" : {
        "type" : "keyword",
        "norms" : false
      }
    }
  },

不合适,因为它使“someField”本身成为关键字,而我们希望“someField.keyword”成为关键字字段。但是如果我使用这个模板:

"dynamic_templates" : [

  {
    "someField" : {
      "match" : "someField",
      "mapping" : {
        "fields": {
          "keyword": {
            "type": "keyword",
            "norms" : false
          }
        }
      }
    }
  },

创建分析的“顶级”字段以及关键字子字段:

                          "someField" : {
                            "type" : "text",
                            "fields" : {
                              "keyword" : {
                                "type" : "keyword"
                              }
                            }
                          },

有没有办法不映射“someField”字段,只有“someField.keyword”?我可以为“someField”级别设置 index : false ,但用户可能仍会尝试使用它,并想知道它为什么不起作用。

编辑:我试图将 someField 定义为对象类型,正如haltabush 所建议的那样,但得到一个错误。使用此模板:

{
  "template": "test*",
  "order":    1,

    "mappings" : {
      "_default_" : {

        "dynamic_templates" : [
          {
            "someField" : {
              "match" : "someField",
              "mapping" : {
                "type" : "object",
                "fields": {
                  "keyword": {
                    "type": "keyword"
                  }
                }
              }
            }
          }
        ]
      }
    }
}

当我发布此文件时:

curl -XPUT localhost:9200/test-1/doc/1?pretty -d '
{
  "someField" : "some value"
}
'

我收到此错误:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "mapper_parsing_exception",
        "reason" : "object mapping for [someField] tried to parse field [someField] as object, but found a concrete value"
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "object mapping for [someField] tried to parse field [someField] as object, but found a concrete value"
  },
  "status" : 400
}
4

1 回答 1

0

您可以创建一个对象:

                     "someField" : {
                        "type" : "object",
                        "fields" : {
                          "keyword" : {
                            "type" : "keyword"
                          }
                        }
                      },
于 2017-09-18T23:35:20.983 回答