弹性搜索:索引具有空值的日期字段

时间:2018-05-18 19:06:50

标签: python json elasticsearch

我在Python中使用Elasticsearch客户端为下面的字段创建索引,我仍然坚持创建具有空值的日期索引。 当数据中存在空值时,我很难看到为什么索引没有设置为date而不是string。 从在线和ES文档研究中,看起来您无法对空值进行索引。 所以,我正在关注这个https://www.elastic.co/guide/en/elasticsearch/reference/current/null-value.html文档来解决使用"null_value": "NULL"的问题,但是我还没有成功。

我尝试将实际日期日期更改为"yyyy-MM-dd", "MM/dd/yyyy" ...等格式以及许多其他组合。 对于json映射,我还尝试了{"type": "strict_date"}{"type": "strict_date": "MM/dd/yyyy"}。 有什么方法可以解决这个问题吗?

数据:

  id_name,team_name,team_members,date_info,date_sub
  123,"Biology, Neurobiology ","Ali Smith, Jon Doe",5/1/2015,5/1/2015
  234,Mathematics,Jane Smith ,8/12/2016,
  345,"Statistics, Probability","Matt P, Albert Shaw",5/15/2015,5/15/2015
  456,Chemistry,"Andrew M, Matt Shaw, Ali Smith",4/12/2017,
  678,Physics,"Joe Doe, Jane Smith, Ali Smith ",5/12/2017,5/12/2017

JSON / PYTHON MAPPING:

request_body = '''
        {
            "settings" : {
              "number_of_shards": 2,
              "number_of_replicas": 1
            },

            "mappings": {
                "team": {
                    "properties": {
                        "id_name": { "type": "text"},
                        "team_name": { "type": "text"},
                        "team_members": { "type": "text"},
                        "date_info": {"type": "date","null_value": "NULL"},
                        "date_sub": {"type": "date","null_value":"NULL"}
                        }
                    }
            }
        }
    '''

    res = self.es.indices.create(index=your_index_name, ignore = 400, body=request_body)

错误:

raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, 'mapper_parsing_exception', 'failed to parse [date_info]')

2 个答案:

答案 0 :(得分:0)

在您的映射中,您没有为日期字段指定日期格式,在这种情况下,Elastic将使用内置格式,后面是 - "strict_date_optional_time||epoch_millis",这意味着它应该是一个长的数字代表从纪元开始的毫秒数或strict_date_optional_time,实际上是严格格式

严格格式意味着,如果您有日期5/12/2017,则应填写缺少的数字。在这种情况下,正确的严格日期应为05/12/2017

有关日期格式的更多信息 - https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#built-in-date-formats

答案 1 :(得分:0)

首先,您的日期字段架构不得包含"null_value": "NULL"

我在Kibana试了一下

PUT stackoverflow {
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 1
  },
  "mappings": {
    "team": {
      "properties": {
        "id_name": {
          "type": "text"
        },
        "team_name": {
          "type": "text"
        },
        "team_members": {
          "type": "text"
        },
        "date_info": {
          "type": "date"
        },
        "date_sub": {
          "type": "date"
        }
      }
    }
  }
}

然后,我尝试插入带有空日期信息的数据

POST stackoverflow/team
{
  "id_name": 341,
  "team_name": "Gogologi",
  "team_members": "Wayern",
  "date_info": null,
  "date_sub": "2014-02-01"
}

并验证,我执行了GET命令GET stackoverflow/team/_search

{
  "_index": "stackoverflow",
  "_type": "team",
  "_id": "AWOCTEhoVu_LbUvfNt6J",
  "_score": 1,
  "_source": {
    "id_name": 341,
    "team_name": "Gogologi",
    "team_members": "Wayern",
    "date_info": null,
    "date_sub": "2014-02-01"
  }
}

希望它有所帮助!