Home » Php » Elasticsearch-PHP – Malformed action/metadata line [1], expected a simple value for field [properties] but found [START_OBJECT]

Elasticsearch-PHP – Malformed action/metadata line [1], expected a simple value for field [properties] but found [START_OBJECT]

Posted by: admin October 26, 2017 Leave a comment

Questions:

Here is an example I have written to demonstrate the problem.

I am trying to use Elasticsearch-PHP to create an index with a mapping, and then bulk-index documents into it.

But I am getting the following ERRONEOUS output of the following example program:

Elasticsearch PHP client created successfully!
Result: Array ( [acknowledged] => 1 [shards_acknowledged] => 1 [index] => packets-2017-10-25 ) 


Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Malformed action/metadata line [1], expected a simple value for field [properties] but found [START_OBJECT]"}],"type":"illegal_argument_exception","reason":"Malformed action/metadata line [1], expected a simple value for field [properties] but found [START_OBJECT]"},"status":400} in /var/www/html/Utilities/Elasticsearch-PHP/composer/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:610 Stack trace: #0 /var/www/html/Utilities/Elasticsearch-PHP/composer/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(273): Elasticsearch\Connections\Connection->process4xxError(Array, Array, Array) #1 /var/www/html/Utilities/Elasticsearch-PHP/composer/vendor/react/promise/src/FulfilledPromise.php(25): Elasticsearch\Connections\Connection->Elasticsearch\Connections\{closure}(Array) #2 /var/www/html/Utilities/Elasticsearch-PHP/composer/v in /var/www/html/Utilities/Elasticsearch-PHP/composer/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 610

The question is how to fix this?

index.php

<?php 

require_once('composer/vendor/autoload.php');
use Elasticsearch\ClientBuilder;
$esClient = ClientBuilder::create()->build();

echo 'Elasticsearch PHP client created successfully!<br>';//check




$indexParams = [
    'index' => 'packets-2017-10-25',

    'body' => [
        'mappings' => [
            'pcap_file' => [


                'properties' => [

                    'ip' => [
                        'properties' => [
                            'ip_ip_src_host' => [ 'type' => 'keyword' ],
                            'ip_ip_dst_host' => [ 'type' => 'keyword' ]
                        ]
                    ]

                    /**
                    'ip_ip_src_host' => [ 'type' => 'keyword' ],
                    'ip_ip_dst_host' => [ 'type' => 'keyword' ]
                    */

                ]

            ]
        ]
    ]



];

$indexingResponse = $esClient->indices()->create($indexParams);
echo 'Result: '; print_r($indexingResponse); echo '<br>';//check

echo "<br><br>";



for($i = 0; $i < 100; $i++) {
    $params['body'][] = [
        'index' => [
            '_index' => 'packets-2017-10-25',
            '_type' => 'pcap_file',
        ]
    ];

    $params['body'][] = [
        'my_field' => 'my_value',
        'second_field' => 'some more values'
    ];
}


$indexingResponse = $esClient->bulk($indexParams);
echo "<br><br>"; $print_r($indexingResponse); echo "<br><br>";
Answers: