首页 > 解决方案 > 复杂文档 - 仅提供少数字段的映射,但保持其余部分不变

问题描述

我有一些非常复杂的文档,我想将它们存储在弹性中以检索它们并使它们可搜索。我不知道数据的整个结构,所以我希望弹性“吞下”我放入的所有内容,但定义一些索引字段以使搜索成为可能。

一旦我为弹性提供映射,我就会得到错误,如果我不定义映射,我担心索引会变得太大,因为弹性会索引太多。

我使用 php 创建索引,但归结为:

PUT localhost:9200/name-of-index

{
    "mappings": {
        "_doc": {
            "properties": {
                "title": {
                    "type": "text"
                }
            }
        }
    }
}

然后 - 当我添加一个对象来测试所有内容时,我会收到以下错误:

状态 400

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Rejecting mapping update to [name-of-index] as the final mapping would have more than 1 type: [_doc, 2187]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "Rejecting mapping update to [name-of-index] as the final mapping would have more than 1 type: [_doc, 2187]"
    },
    "status": 400
}

我用来发布文档的命令是关于:

POST localhost:9200/name-of-index/2187

{
    "title": "some title",
    "otherField": "other value",
    "obj": {
        "nestedProp": "nestedValue",
        "deepObj": {
            "someStorage": [
                ...
                {
                    "someVeryDeepProp": 1
                }
                ...                
            ]            
        }
    },
    "obj2": [
        "str1",
        "str2"
    ]
}

节点名称当然不是真实的,结构比这复杂得多。但我怀疑这是我的问题的原因。

那么我如何定义部分索引并保持其他所有内容不变呢?

更新:我忘记了一些弹性信息..

{
  "name" : "KNJ_3Eg",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "4M9p8XiaQHKPz7N2AAuVlw",
  "version" : {
    "number" : "6.6.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "a9861f4",
    "build_date" : "2019-01-24T11:27:09.439740Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

标签: elasticsearchlucene

解决方案


好吧,那只是一个愚蠢的错误,我忘了添加类型。

所以添加文档的正确请求应该是:

POST localhost:9200/name-of-index/_doc/2187

{
    "title": "some title",
    "otherField": "other value",
    "obj": {
        "nestedProp": "nestedValue",
        "deepObj": {
            "someStorage": [
                ...
                {
                    "someVeryDeepProp": 1
                }
                ...                
            ]            
        }
    },
    "obj2": [
        "str1",
        "str2"
    ]
}

我猜从版本 7 开始,它会没有“_doc”,因为通常不推荐使用类型。


推荐阅读