首页 > 解决方案 > 在哪里放置 include_type_name 在 config.exs

问题描述

我想从我的 elixir config.exs 文件创建一个 Elasticsearch 7.x 索引:

config :app_core, App.Tools.ElasticsearchCluster,
  url: System.get_env("ELASTIC_HOST"),
  # username: "username",
  # password: "password",
  api: Elasticsearch.API.HTTP,
  json_library: Poison,
  indexes: %{
    indie: %{
      settings: "priv/elasticsearch/indies.json",
      store: App.Tools.ElasticSearch.Indie.Store,
      sources: [App.Data.Schema.Indie],
      bulk_page_size: 5000,
      bulk_wait_interval: 15_000
    }
  }

priv/elasticsearch/indies.json开始于

{
  "mappings": {
     "_doc":    {
      "properties": {
        "category" : {
          "type": "nested",
          "properties" : {

但是,当我尝试创建索引时,出现错误

"The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true."

有谁知道如何在我引用的上下文中解决这个问题(把它放在特定查询前面是行不通的)?

应 Assael Azran 的要求,这里是完整的 indies.json:

{
  "mappings": {
     "_doc": {
      "properties": {
        "category" : {
          "type": "nested",
          "properties" : {
            "all_parents" : {
              "type" : "keyword"
            },
            "direct_parent" : {
              "type" : "keyword"
            },
            "paths" : {
              "type" : "keyword"
            }
          }
        },
        "slug": {
          "type": "keyword"
        },
        "parent_id": {
          "type": "integer",
          "index": false
        },
        "images": {
          "type": "nested",
          "properties": {
            "indie_url": {
              "type": "text",
              "index": false
            }
          }
        },
        "tenant": {
          "type": "keyword"
        },
        "suggest_keywords": {
          "type": "completion",
          "contexts": [
            {
                "name": "tenant",
                "type": "category"
            }
          ]
        },
        "name": {
          "type": "text"
        },
        "description": {
          "type": "text",
          "index": false
        },
        "updated_at": {
          "type": "date"
        },
        "string_facet": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword"
            },
            "value": {
              "type": "keyword"
            }
          }
        },
        "number_facet": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword"
            },
            "value": {
              "type": "double"
            }
          }
        }
      }
    }
  }
}

标签: elasticsearchelixir

解决方案


7.x 版不再支持映射类型。

Elasticsearch 7.x 不推荐在请求中指定类型。例如,索引文档不再需要文档类型。新的索引 API 是 PUT {index}/_doc/{id} 用于显式 ID 和 POST {index}/_doc 用于自动生成的 ID。请注意,在 7.0 中,_doc 是路径的永久部分,表示端点名称而不是文档类型。索引创建、索引模板和映射 API 中的 include_type_name 参数将默认为 false。完全设置参数将导致弃用警告。默认映射类型已删除。

我的建议是_doc从您的映射中删除类型。

{“映射”:{“属性”:{“类别”:{“类型”:“嵌套”,“属性”:{

从这里

更新

在弹性 7.2.0 上测试

我尝试创建以下映射:

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "category": {
          "type": "nested",
          "properties": {
            "all_parents": {
              "type": "keyword"
            },
            "direct_parent": {
              "type": "keyword"
            },
            "paths": {
              "type": "keyword"
            }
          }
        },
        "slug": {
          "type": "keyword"
        },
        "parent_id": {
          "type": "integer",
          "index": false
        },
        "images": {
          "type": "nested",
          "properties": {
            "indie_url": {
              "type": "text",
              "index": false
            }
          }
        },
        "tenant": {
          "type": "keyword"
        },
        "suggest_keywords": {
          "type": "completion",
          "contexts": [
            {
              "name": "tenant",
              "type": "category"
            }
          ]
        },
        "name": {
          "type": "text"
        },
        "description": {
          "type": "text",
          "index": false
        },
        "updated_at": {
          "type": "date"
        },
        "string_facet": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword"
            },
            "value": {
              "type": "keyword"
            }
          }
        },
        "number_facet": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword"
            },
            "value": {
              "type": "double"
            }
          }
        }
      }
    }
  }
}

正如预期的那样,我得到了这个错误:

{
"error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true."
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true."
  },
  "status": 400
}

当我_doc从映射中删除时:

PUT my_index
{
  "mappings": {
      "properties": {
        "category": {
          "type": "nested",
          "properties": {
            "all_parents": {
              "type": "keyword"
            },
            "direct_parent": {
              "type": "keyword"
            },
            "paths": {
              "type": "keyword"
            }
          }
        },
        "slug": {
          "type": "keyword"
        },
        "parent_id": {
          "type": "integer",
          "index": false
        },
        "images": {
          "type": "nested",
          "properties": {
            "indie_url": {
              "type": "text",
              "index": false
            }
          }
        },
        "tenant": {
          "type": "keyword"
        },
        "suggest_keywords": {
          "type": "completion",
          "contexts": [
            {
              "name": "tenant",
              "type": "category"
            }
          ]
        },
        "name": {
          "type": "text"
        },
        "description": {
          "type": "text",
          "index": false
        },
        "updated_at": {
          "type": "date"
        },
        "string_facet": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword"
            },
            "value": {
              "type": "keyword"
            }
          }
        },
        "number_facet": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword"
            },
            "value": {
              "type": "double"
            }
          }
        }
      }
    }
}

我明白了

{

"acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "my_index"
}

推荐阅读