【问题标题】:Defining mapping within elasticsearch with json file使用 json 文件在 elasticsearch 中定义映射
【发布时间】:2018-02-02 20:07:54
【问题描述】:

我正在尝试使用 json 文件来定义每个索引的默认映射。这就是我想要做的:

/usr/share/elasticsearch/config/default-mapping.json

{
    "item": {
        "properties": {
            "uuid": {"type": "string", "store": "yes", "index": "no"},
            "title": {"type": "string", "store": "yes", "boost": 5,
                      "index": "analyzed", "analyzer": "english"},
            "description": {"type": "string", "store": "yes", "boost": 3,
                            "index": "analyzed", "analyzer": "english"},
}

当我尝试查询我的 index_test elasticsearch 索引时,我得到了这个:

curl -XGET 'http://...:9200/index_test/_mapping'
{"index_test":{"mappings":{}}}

我使用了在此处找到的文档。

https://www.found.no/foundation/elasticsearch-mapping-introduction/

【问题讨论】:

标签: json elasticsearch


【解决方案1】:

您可以使用默认配置(索引设置、映射等)为您的索引创建 index template

  1. 为此,请将 default-mapping.json 文件的内容更改为 喜欢:

    {
        "template_1" : {
            "template" : "*",
            "mappings" : {
                "type" : {
                    "properties" : {
                        "uuid" : {
                            "type" : "string",
                            "store" : "yes",
                            "index" : "no"
                        },
                        "title" : {
                            "type" : "string",
                            "store" : "yes",
                            "boost" : 5,
                            "index" : "analyzed",
                            "analyzer" : "english"
                        },
                        "description" : {
                            "type" : "string",
                            "store" : "yes",
                            "boost" : 3,
                            "index" : "analyzed",
                            "analyzer" : "english"
                        }
                    }
                }
            }
        }
    }
    
  2. 将 default-mapping.json 文件移至 /usr/share/elasticsearch/config/templates 目录
  3. 创建一个新索引

    POST /newindex
    
  4. 新建索引的映射:

    {
        "newindex" : {
            "mappings" : {
                "type" : {
                    "properties" : {
                        "description" : {
                            "type" : "string",
                            "boost" : 3,
                            "store" : true,
                            "analyzer" : "english"
                        },
                        "title" : {
                            "type" : "string",
                            "boost" : 5,
                            "store" : true,
                            "analyzer" : "english"
                        },
                        "uuid" : {
                            "type" : "string",
                            "index" : "no",
                            "store" : true
                        }
                    }
                }
            }
        }
    }
    

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    假设您已经创建了索引index_test,您需要执行以下操作(如您的链接中所示):

    $ curl -XPUT 'http://localhost:9200/index_test/my_type/_mapping' -d '
    {
      "my_type": {
        "properties": {
          "uuid": {
            "type": "string",
            "store": "yes",
            "index": "no"
          },
          "title": {
            "type": "string",
            "store": "yes",
            "boost": 5,
            "index": "analyzed",
            "analyzer": "english"
          },
          "description": {
            "type": "string",
            "store": "yes",
            "boost": 3,
            "index": "analyzed",
            "analyzer": "english"
          }
        }
      }
    }
    
    '
    

    重要的是要注意payload中的字段对应于type-name(一般我取my_type)。

    url:9200/index_test/_mapping/my_type?pretty 上使用GET 进行验证。

    问候 弗里克

    【讨论】:

    • 非常好的答案。我想补充一件事。您可能在 elasticsearch/config 中找不到模板目录。您应该创建一个并将映射文件放入其中。
    【解决方案3】:

    您的 JSON 文档无效。应该是:

    {
        "my_type": {
            "properties": {
                "uuid": {
                    "type": "string",
                    "store": "yes",
                    "index": "no"
                },
                "title": {
                    "type": "string",
                    "store": "yes",
                    "boost": 5,
                    "index": "analyzed",
                    "analyzer": "english"
                },
                "description": {
                    "type": "string",
                    "store": "yes",
                    "boost": 3,
                    "index": "analyzed",
                    "analyzer": "english"
                }
            }
        }
    }
    

    我建议您在修改映射时始终检查您的 JSON 文档。 当您发送无效的 JSON 文档作为映射时,ElasticSearch 将忽略您的映射并且不会提示您任何错误

    JSON 验证器站点:http://jsonlint.com/

    【讨论】:

      猜你喜欢
      • 2017-04-23
      • 2022-01-03
      • 2016-03-30
      • 2020-01-19
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      相关资源
      最近更新 更多