ElasticSearch6.x日期类型处理

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"
      }
    }
  }
}

大佬们有什么解决办法吗?

阅读 8.3k
4 个回答

实际上ES6.0中是取消了type的这个概念的。具体原因可以看:

Removal of mapping

其中也给出了一些修改的方案。
比如可以给这些文档放在一起,通过特定的字段来区分到底是哪种类型的文档。

PUT m_index
{
  "mappings": {
    "doc": {
      "properties": {
        "type": "keyword",
        "startTime": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_second"
        },
        "endTime": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_second"
        }
      }
    }
  }
}

这里给你的m_type_1和m_type_2处理成一个字段,用于区分,查询的时候增加一个filter就可以。

或者用比较笨的方法,给原来的type升级为index处理。按照官方的说法是有两个优势:

This approach has two benefits:

  • Data is more likely to be dense and so benefit from compression techniques used in Lucene.
  • The term statistics used for scoring in full text search are more likely to be accurate because all documents in the same index represent a single entity.
  • 数据可能会更加密集,此时Lucene引擎的压缩优势就得以展现优势。
  • 由于在相同索引中的文档表示的是同一个实体,这样在全文检索过程中分项计算的得分将会变得更精确。

遇到同样的问题, 关注中....

题主这个问题解决了吗? 新入门 es 还不是很明白 mapping 是什么意思 ,但是有同样的问题 需要使用时间段作为条件查询.

新手上路,请多包涵

问题解决了吗?遇到了同样的问题

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进