将include_type_name放在config.exs中的位置

时间:2019-11-12 18:18:34

标签: elasticsearch elixir phoenix

我想从我的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"
            }
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

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

  

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

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

  

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

From here

更新

在弹性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"
}