简体   繁体   中英

Elasticsearch 5.5 getting error 'not_x_content_exception' using CURL

I'm getting below error: while I trying to create mapping and its properties

Array
(
[error] => Array
    (
        [root_cause] => Array
            (
                [0] => Array
                    (
                        [type] => not_x_content_exception
                        [reason] => Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes
                    )

            )

        [type] => not_x_content_exception
        [reason] => Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes
    )

[status] => 500

)

I'm Using CURL-here is my code using Codeigniter and elasticsearch

    $create ='
                {
                "properties": {
                    "message": {
                      "type": "text"
                        }
                  }
                }
                ';
    $response = $this->elasticsearch->custome_function("_mapping/tweet","PUT", $create);

Here is my class file:

class ElasticSearch
{
public $index;
/**
 * constructor setting the config variables for server ip and index.
 */
   public function __construct()
   {
    $ci = &get_instance();
    $ci -> config -> load("elasticsearch");
    $this -> server = 'http://localhost:9200'; //$ci -> config -> item('es_server');
    $this -> index = "my_index";        // configured in constant file //$ci -> config -> item('index');
}

private function call($path, $method = 'GET', $data = null)
{
    if (!$this -> index) {
        throw new Exception('$this->index needs a value');
    }
    $url = $this -> server . '/' . $this -> index . '/' . $path;
    $headers = array('Accept: application/json', 'Content-Type: application/json', );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    switch($method) {
        case 'GET' :
            break;
        case 'POST' :
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        case 'PUT' :
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            break;
        case 'DELETE' :
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            break;
    }
    $response = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    return json_decode($response, true);
}


 public function custome_function($type,$method,$query)
 {
    return $this -> call($type,$method,$query);
 }
 }

Can anyone suggest me how to create mappings and properties or is there any alternate way to create mappings

For completeness, I'm adding this as an answer, though the comments identified the problem.

According to an answer on this page ( https://github.com/elastic/elasticsearch-rails/issues/606 ), that error can happen when you send in a string rather than a JSON document.

Calling the json_encode in the PUT on a string that was already JSON-encoded resulted in sending in a non-JSON document to the Elasticsearch endpoint. Removing the unnecessary json_encode fixes the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM