ElasticSearch6.x只允许在一个index里对一个type进行mapping,如果对一个以上的type进行mapping就会失败。比如:
创建mapping
PUT m_index
{
"mappings": {
"m_type_1": {
"properties": {
"startTime": {
"type": "date",
"format": "strict_date_optional_time||epoch_second"
},
"endTime": {
"type": "date",
"format": "strict_date_optional_time||epoch_second"
}
}
},
"m_type_2": {
"properties": {
"startTime": {
"type": "date",
"format": "strict_date_optional_time||epoch_second"
},
"endTime": {
"type": "date",
"format": "strict_date_optional_time||epoch_second"
}
}
}
}
}
响应:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Rejecting mapping update to [m_index] as the final mapping would have more than 1 type: [m_type_2, m_type_1]"
}
],
"type": "illegal_argument_exception",
"reason": "Rejecting mapping update to [m_index] as the final mapping would have more than 1 type: [m_type_2, m_type_1]"
},
"status": 400
}
我如果不对每个type进行mapping的话,就不能用日期作为条件进行查询了。
比如这样的查询:
POST m_index/_search
{
"query": {
"range": {
"startTime": {
"gte": "2017-08-01",
"lte": "2017-12-31"
}
}
}
}
大佬们有什么解决办法吗?
实际上ES6.0中是取消了type的这个概念的。具体原因可以看:
Removal of mapping
其中也给出了一些修改的方案。
比如可以给这些文档放在一起,通过特定的字段来区分到底是哪种类型的文档。
这里给你的m_type_1和m_type_2处理成一个字段,用于区分,查询的时候增加一个filter就可以。
或者用比较笨的方法,给原来的type升级为index处理。按照官方的说法是有两个优势: