es - elasticsearch mapping - parameters - ignore_malformed

311 阅读1分钟

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

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

# ignore_malformed
PUT /ignore_malformed_test
{
  "settings" : {
    "index.mapping.ignore_malformed" : true
  }, 
  "mappings" : {
    "properties" : {
      "num_1" : {
        "type" : "integer"
      },
      "num_2" : {
        "type"             : "integer",
        "ignore_malformed" : false
      },
      "name" : {
        "type" : "text"
      }
    }
  }
}

# 索引
POST /ignore_malformed_test/_doc/1
{
  "num_1" : "hello",
  "name"  : "hello"
}

# 索引 - 格式不正确,抛出异常
POST /ignore_malformed_test/_doc/2
{
  "num_2" : "good",
  "name"  : "good"
}

# 结果
{
  "error" : {
    "root_cause" : [
      {
        "type" : "mapper_parsing_exception",
        "reason" : "failed to parse field [num_2] of type [integer] in document with id '2'. Preview of field's value: 'good'"
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse field [num_2] of type [integer] in document with id '2'. Preview of field's value: 'good'",
    "caused_by" : {
      "type" : "number_format_exception",
      "reason" : "For input string: \"good\""
    }
  },
  "status" : 400
}

# 搜索
GET /ignore_malformed_test/_search
{
  "query" : {
    "term" : {
      "name" : {
        "value" : "hello"
      }
    }
  }
}

# 结果
{
  "took" : 645,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "ignore_malformed_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_ignored" : [
          "num_1"
        ],
        "_source" : {
          "num_1" : "hello",
          "name" : "hello"
        }
      }
    ]
  }
}


# 搜索 - num_1格式不正确,所以不能搜索
GET /ignore_malformed_test/_search
{
  "query" : {
    "term" : {
      "num_1" : {
        "value" : "hello"
      }
    }
  }
}

# 结果
{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: For input string: \"hello\"",
        "index_uuid" : "0FVsnnkmS4ukolX2M3izPg",
        "index" : "ignore_malformed_test"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "ignore_malformed_test",
        "node" : "VK0GF1KyQGKmM_0KvgHHnw",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: For input string: \"hello\"",
          "index_uuid" : "0FVsnnkmS4ukolX2M3izPg",
          "index" : "ignore_malformed_test",
          "caused_by" : {
            "type" : "number_format_exception",
            "reason" : "For input string: \"hello\""
          }
        }
      }
    ]
  },
  "status" : 400
}