在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  網(wǎng)絡(luò)安全/ ElasticSearch6.x日期類型處理

ElasticSearch6.x日期類型處理

ElasticSearch6.x只允許在一個(gè)index里對(duì)一個(gè)type進(jìn)行mapping,如果對(duì)一個(gè)以上的type進(jìn)行mapping就會(huì)失敗。比如:

創(chuàng)建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"
        }
      }
    }
  }
}

響應(yīng):

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

我如果不對(duì)每個(gè)type進(jìn)行mapping的話,就不能用日期作為條件進(jìn)行查詢了。
比如這樣的查詢:

POST m_index/_search
{
  "query": {
    "range": {
      "startTime": {
        "gte": "2017-08-01",
        "lte": "2017-12-31"
      }
    }
  }
}

大佬們有什么解決辦法嗎?

回答
編輯回答
撿肥皂

實(shí)際上ES6.0中是取消了type的這個(gè)概念的。具體原因可以看:

Removal of mapping

其中也給出了一些修改的方案。
比如可以給這些文檔放在一起,通過(guò)特定的字段來(lái)區(qū)分到底是哪種類型的文檔。

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處理成一個(gè)字段,用于區(qū)分,查詢的時(shí)候增加一個(gè)filter就可以。

或者用比較笨的方法,給原來(lái)的type升級(jí)為index處理。按照官方的說(shuō)法是有兩個(gè)優(yōu)勢(shì):

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.
  • 數(shù)據(jù)可能會(huì)更加密集,此時(shí)Lucene引擎的壓縮優(yōu)勢(shì)就得以展現(xiàn)優(yōu)勢(shì)。
  • 由于在相同索引中的文檔表示的是同一個(gè)實(shí)體,這樣在全文檢索過(guò)程中分項(xiàng)計(jì)算的得分將會(huì)變得更精確。
2017年3月12日 18:57
編輯回答
祈歡

遇到同樣的問(wèn)題, 關(guān)注中....

2018年3月30日 06:45
編輯回答
悶騷型

問(wèn)題解決了嗎?遇到了同樣的問(wèn)題

2017年10月7日 15:11
編輯回答
我以為

題主這個(gè)問(wèn)題解決了嗎? 新入門(mén) es 還不是很明白 mapping 是什么意思 ,但是有同樣的問(wèn)題 需要使用時(shí)間段作為條件查詢.

2017年5月8日 04:07