How to deal with date type properties in elasticsearch


(Prakhar Sharma) #1

I am very new to elasticsearch and implementing elasticsearch in php-laravel based API using elasticsearch-php client. I am saving 2 datetime fields (created_on, modified_on) from the sql db which I want to index in problem document. Ar first I didn't give date format for properties in mapping.

        'index' => 'newproblemindex',
        'type' => 'newproblems2',
        'body' => [
            'newproblems2' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'subject' => [
                        'type' => 'text',
                        'analyzer' => 'standard',
                    ],
                    'notes' => [
                        'type' => 'text',
                        'analyzer' => 'standard'
                    ],
                    'created_on' => [
                        'type' => 'date',
                    ],
                    'modified_on' => [
                        'type' => 'date',
                    ],
                    'created_by' => [
                        'type' => 'integer',
                    ],
                    'modified_by' => [
                        'type' => 'integer',
                    ],
                    'status_enum' => [
                        'type' => 'integer',
                    ],
                    'rating' => [
                        'type' => 'integer',
                    ]
                 ]
            ]
        ]
    ];

so when I tried to index my document I got the error
Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse [created_on]"}],"type":"mapper_parsing_exception","reason":"failed to parse [created_on]","caused_by":{"type":"illegal_state_exception","reason":"Can't get text on a START_OBJECT at 1:100"}},"status":400} in file /var/www/development/api/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 615

So I tried to update the mapping using put mapping

        'index' => 'newproblemindex',
        'type' => 'newproblems2',
        'body' => [
            'newproblems2' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'subject' => [
                        'type' => 'text',
                        'analyzer' => 'standard',
                    ],
                    'notes' => [
                        'type' => 'text',
                        'analyzer' => 'standard'
                    ],
                    'created_on' => [
                        'type' => 'date',
                        'format' => "Y-m-d H:i:s"
                    ],
                    'modified_on' => [
                        'type' => 'date',
                        'format' => "Y-m-d H:i:s"
                    ],
                    'created_by' => [
                        'type' => 'integer',
                    ],
                    'modified_by' => [
                        'type' => 'integer',
                    ],
                    'status_enum' => [
                        'type' => 'integer',
                    ],
                    'rating' => [
                        'type' => 'integer',
                    ]

                ]
            ]
        ]
    ];

but now I am getting error
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Invalid format: [Y-m-d H:i:s]: Illegal pattern component: i"}],"type":"illegal_argument_exception","reason":"Invalid format: [Y-m-d H:i:s]: Illegal pattern component: i","caused_by":{"type":"illegal_argument_exception","reason":"Illegal pattern component: i"}},"status":400}

how can I remove the error what am I doing wrong, any help would be appreciated
Thanks for your help in advance


(Junaid) #2

Once mappings are created they cannot be altered. Existing mappings can only be changed through reindexing.


(Prakhar Sharma) #3

I tried reindexing but the problem still remains


(Junaid) #4

The second error is infact due to incorrect format specification for date type. Try the builtin formats or your own custom formats by following the guide here.

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#custom-date-formats

Can you share the JSON document which you are indexing?


(Prakhar Sharma) #5

I am facing problem when I am trying to assigning this date format ---- 2018-06-12 09:42:30.920889
[created_on] => DateTime Object
(
=> 2018-06-12 09:43:56.216460
[timezone_type] => 3
[timezone] => UTC
)

        [created_by] => 4
        [status_enum] => 2
        [subject] => Elasticsearch Problem_12thJune
        [notes] => I am submitting problem in elasticsearch
        [id] => 1096

(Prakhar Sharma) #6

I have put new mappings and used this format "format"=> "yyyy-MM-dd HH:mm:ss.SSSZ", for date type fields but now I am getting this error

Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse [created_on]"}],"type":"mapper_parsing_exception","reason":"failed to parse [created_on]","caused_by":{"type":"illegal_argument_exception","reason":"Invalid format: "2018-06-12 09:45:29.960449" is malformed at "449""}},"status":400} in file /var/www/development/api/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 615


(Junaid) #7

@SharmaPrakhar25 This is because your pattern doesn't match the format that you have specified in your mappings. For your current date formats (2018-06-12 09:45:29.960449) use the following mapping.

{"properties": {"created_on": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss.SSSSSS"}}}


(system) #8

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.