【问题标题】:Elasticsearch 5.5 getting error 'not_x_content_exception' using CURLElasticsearch 5.5 使用 CURL 获取错误“not_x_content_exception”
【发布时间】:2017-12-28 06:44:07
【问题描述】:

我遇到以下错误:当我尝试创建映射及其属性时

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

)

我正在使用 CURL——这是我使用 Codeigniter 和 elasticsearch 的代码

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

这是我的类文件:

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);
 }
 }

谁能建议我如何创建映射和属性,或者是否有任何替代方法来创建映射

【问题讨论】:

  • 能否添加curl_setopt($ch, CURLOPT_VERBOSE, true); 并提供执行时看到的调试信息?
  • 我想知道这是否可能与在您发送的已采用 JSON 格式的数据上调用 json_encode 有关:也许这会导致 Elasticsearch API 没有的文档不处理?你能检查一下你编码后到底得到了什么,或者尝试在你的PUT 案例中删除json_encode 调用吗?
  • 谢谢你们,删除json_encode后工作正常

标签: php codeigniter elasticsearch


【解决方案1】:

为了完整起见,我将其添加为答案,尽管 cmets 发现了问题。

根据此页面上的答案 (https://github.com/elastic/elasticsearch-rails/issues/606),当您发送字符串而不是 JSON 文档时,可能会发生该错误。

在 PUT 中对已进行 JSON 编码的字符串调用 json_encode 会导致将非 JSON 文档发送到 Elasticsearch 端点。删除不必要的json_encode 可以解决问题。

【讨论】:

    猜你喜欢
    • 2018-04-23
    • 1970-01-01
    • 2020-01-03
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2013-11-23
    • 1970-01-01
    相关资源
    最近更新 更多