es - elasticsearch mapping - parameters - dynamic

110 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :dynamic有什么特点?
答 :
在这里插入图片描述
问 :dynamic如何使用?
答 :

# dynamic

# 动态添加
POST /dynamic_test_1/_doc/1
{
  "name"  : "hello good",
  "user"  : {
    "first_name" : "hello",
    "last_name"  : "good"
  }
}

# 查看mapping
GET /dynamic_test_1/_mapping

# 结果
{
  "dynamic_test_1" : {
    "mappings" : {
      "properties" : {
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "user" : {
          "properties" : {
            "first_name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "last_name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }
      }
    }
  }
}

# 搜索
GET /dynamic_test_1/_search
{
  "query" : {
    "term" : {
      "user.first_name" : {
        "value" : "hello"
      }
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "dynamic_test_1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "hello good",
          "user" : {
            "first_name" : "hello",
            "last_name" : "good"
          }
        }
      }
    ]
  }
}


# 设置dynamic为false
PUT /dynamic_test_2
{
  "mappings" : {
    "dynamic" : false
  }
}

# 索引
POST /dynamic_test_2/_doc/1
{
  "name" : "hello good",
  "user" : {
    "first_name" : "hello",
    "last_name"  : "good"
  }
}

# 查看mapping
GET /dynamic_test_2/_mapping

# 结果
{
  "dynamic_test_2" : {
    "mappings" : {
      "dynamic" : "false"
    }
  }
}

# 搜索
GET /dynamic_test_2/_search
{
  "query" : {
    "term" : {
      "user.first_name" : {
        "value" : "hello"
      }
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

# 搜索 不支持域查询,但是全部查可以查到
GET /dynamic_test_2/_search

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "dynamic_test_2",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "hello good",
          "user" : {
            "first_name" : "hello",
            "last_name" : "good"
          }
        }
      }
    ]
  }
}

# dynamic设置为strict
PUT /dynamic_test_3
{
  "mappings" : {
    "dynamic" : "strict"
  }
}

# 索引 直接报错
POST /dynamic_test_3/_doc/1
{
  "name" : "hello good",
  "user" : {
    "first_name" : "hello",
    "last_name"  : "good"
  }
}

# 结果
{
  "error" : {
    "root_cause" : [
      {
        "type" : "strict_dynamic_mapping_exception",
        "reason" : "mapping set to strict, dynamic introduction of [name] within [_doc] is not allowed"
      }
    ],
    "type" : "strict_dynamic_mapping_exception",
    "reason" : "mapping set to strict, dynamic introduction of [name] within [_doc] is not allowed"
  },
  "status" : 400
}