ElasticSearch дает ошибку 400

Общие вопросы по использованию второй версии фреймворка. Если не знаете как что-то сделать и это про Yii 2, вам сюда.
Ответить
wwwlopment
Сообщения: 7
Зарегистрирован: 2018.07.18, 22:45

ElasticSearch дает ошибку 400

Сообщение wwwlopment »

Разбираюсь с Еластиком. Цель - сделать полноценный текстовый поиск. То, что у меня вышло, дает ошибку 400. Пробовал отключать csrf. Не дает результата. Возможно что-то я сделал не так?

Код: Выделить всё

  class ElasticController extends Controller
{
public function actionSearch()
  {
    $elastic = new Search();
      $result  = $elastic->Searches(Yii::$app->request->queryParams);
    $query = Yii::$app->request->queryParams;
    return $this->render('search', [
      'searchModel'  => $elastic,
      'dataProvider' => $result,
      'query'        => $query['search'],
    ]);
}
  }

Код: Выделить всё

class Search extends ElasticProducts

{

[code]  public function Searches($value)
  {
    $searchs      = $value['search'];
    $query = ElasticProducts::find()->query([
      "fuzzy_like_this" => [
        "fields" => ["title", "description"],
        "like_text" => $searchs,
      ]
    ]);
    $query->all();
$query = $query->search();
    $dataProvider = new ActiveDataProvider([
      'query'      => $query,
      'pagination' => ['pageSize' => 10],
    ]);
    return $dataProvider;
  }
}

Код: Выделить всё

class ElasticProducts extends yii\elasticsearch\ActiveRecord
{
  public function rules()
  {
    return [
      [$this->attributes(), 'safe'],
    ];
  }

  public function attributes()
  {
    return [
      'id',
      'title',
      'category_id',
      'price',
      'vendor',
      'description',
      'image',
      'available',
      'updated_at',
      'created_at',
    ];
  }
  public static function index()
  {
    return 'ishop';
  }

  public static function type()
  {
    return 'products';
  }

  public static function mapping()
  {
    return [
      static::type() => [
        'properties' => [
          'id'           => ['type' => 'long'],
          'title'           => ['type' => 'string'],
          'description'    => ['type' => 'string'],
          'vendor' => ['type' => 'string'],
          'price'     => ['type' => 'long'],
          'category_id'     => ['type' => 'long'],
          'image'     => ['type' => 'string'],
          'available'     => ['type' => 'long'],
          ],
      ],
    ];
  }
}
Аватара пользователя
SiZE
Сообщения: 2817
Зарегистрирован: 2011.09.21, 12:39
Откуда: Perm
Контактная информация:

Re: ElasticSearch дает ошибку 400

Сообщение SiZE »

Текст ошибки, версия эластика, версия пакета?
wwwlopment
Сообщения: 7
Зарегистрирован: 2018.07.18, 22:45

Re: ElasticSearch дает ошибку 400

Сообщение wwwlopment »

Yii2 advanced, Elastic 2.0

Elasticsearch Database Exception – yii\elasticsearch\Exception
Elasticsearch request failed with code 400.
Error Info: Array
(
[requestMethod] => GET
[requestUrl] => http://127.0.0.1:9200/inetshop/products/_search
[requestBody] => {"size":10,"query":{"fuzzy_like_this":{"like_text":"Конструктор"}}}
[responseCode] => 400
[responseHeaders] => Array
(
[content-type] => application/json; charset=UTF-8
[content-length] => 481
)

[responseBody] => Array
(
[error] => Array
(
[root_cause] => Array
(
[0] => Array
(
[type] => query_parsing_exception
[reason] => No query registered for [fuzzy_like_this]
[index] => inetshop
[line] => 1
[col] => 39
)

)

[type] => search_phase_execution_exception
[reason] => all shards failed
[phase] => query
[grouped] => 1
[failed_shards] => Array
(
[0] => Array
(
[shard] => 0
[index] => inetshop
[node] => qgMTtw9MTU6SatRqFPN5_A
[reason] => Array
(
[type] => query_parsing_exception
[reason] => No query registered for [fuzzy_like_this]
[index] => inetshop
[line] => 1
[col] => 39
)

)

)

)

[status] => 400
)

)
1. in /home/bogdan/www/inetshop/vendor/yiisoft/yii2-elasticsearch/Connection.php at line 548
539540541542543544545546547548549550551552553554555556557 'requestBody' => $requestBody,
'responseCode' => $responseCode,
'responseHeaders' => $headers,
'responseBody' => $this->decodeErrorBody($body),
]);
}
} elseif ($responseCode == 404) {
return false;
} else {
throw new Exception("Elasticsearch request failed with code $responseCode.", [
'requestMethod' => $method,
'requestUrl' => $url,
'requestBody' => $requestBody,
'responseCode' => $responseCode,
'responseHeaders' => $headers,
'responseBody' => $this->decodeErrorBody($body),
]);
}
}

2. in /home/bogdan/www/inetshop/vendor/yiisoft/yii2-elasticsearch/Connection.php at line 301 – yii\elasticsearch\Connection::httpRequest('GET', 'http://127.0.0.1:9200/inetshop/p...', '{"size":10,"query":{"fuzzy_like_...', false)
295296297298299300301302303304305306307 * @throws Exception
* @throws InvalidConfigException
*/
public function get($url, $options = [], $body = null, $raw = false)
{
$this->open();
return $this->httpRequest('GET', $this->createUrl($url, $options), $body, $raw);
}

/**
* Performs HEAD HTTP request
*
* @param string|array $url URL

3. in /home/bogdan/www/inetshop/vendor/yiisoft/yii2-elasticsearch/Command.php at line 68 – yii\elasticsearch\Connection::get(['inetshop', 'products', '_search'], [], '{"size":10,"query":{"fuzzy_like_...')
62636465666768697071727374 $url = [$this->index !== null ? $this->index : '_all'];
if ($this->type !== null) {
$url[] = $this->type;
}
$url[] = '_search';

return $this->db->get($url, array_merge($this->options, $options), $query);
}

/**
* Sends a request to the delete by query
* @param array $options
* @return mixed

4. in /home/bogdan/www/inetshop/common/models/Search.php at line 58 – yii\elasticsearch\Command::search()
52535455565758596061626364 // $query->source('*');
//$query->where(['fuzzy_like_this'=>$searchs]);


//build and execute the query
$command = $query->createCommand();
$rows = $command->search(); // this way you get the raw output of elasticsearch.
print_r($rows);die();

/* $params = [
"fuzzy_like_this" => [
// "fields" => ["title", "description"],
// "like_text" => $searchs,

5. in /home/bogdan/www/inetshop/frontend/controllers/ElasticController.php at line 61 – common\models\Search::Searches(['search' => 'Конструктор'])
55565758596061626364656667
}

public function actionSearch()
{
$elastic = new Search();
$result = $elastic->Searches(Yii::$app->request->queryParams);
$query = Yii::$app->request->queryParams;
return $this->render('search', [
'searchModel' => $elastic,
'dataProvider' => $result,
'query' => $query['search'],
]);

6. frontend\controllers\ElasticController::actionSearch()
7. in /home/bogdan/www/inetshop/vendor/yiisoft/yii2/base/InlineAction.php at line 57 – call_user_func_array([frontend\controllers\ElasticController, 'actionSearch'], [])
8. in /home/bogdan/www/inetshop/vendor/yiisoft/yii2/base/Controller.php at line 157 – yii\base\InlineAction::runWithParams(['search' => 'Конструктор'])
9. in /home/bogdan/www/inetshop/vendor/yiisoft/yii2/base/Module.php at line 528 – yii\base\Controller::runAction('search', ['search' => 'Конструктор'])
10. in /home/bogdan/www/inetshop/vendor/yiisoft/yii2/web/Application.php at line 103 – yii\base\Module::runAction('elastic/search', ['search' => 'Конструктор'])
11. in /home/bogdan/www/inetshop/vendor/yiisoft/yii2/base/Application.php at line 386 – yii\web\Application::handleRequest(yii\web\Request)
12. in /home/bogdan/www/inetshop/frontend/web/index.php at line 17 – yii\base\Application::run()
11121314151617 require __DIR__ . '/../../common/config/main.php',
require __DIR__ . '/../../common/config/main-local.php',
require __DIR__ . '/../config/main.php',
require __DIR__ . '/../config/main-local.php'
);

(new yii\web\Application($config))->run();

$_GET = [
'search' => 'Конструктор',
];

$_COOKIE = [
'_identity-backend' => '907fe50aefa09aa2f55a7d1bb814cb6fad3bebd88ab41dc38292ddc631e5aedfa:2:{i:0;s:17:"_identity-backend";i:1;s:46:"[1,"XUHuQIePqmksZGMvnkd6fhO2HV6cUzWK",2592000]";}',
'advanced-frontend' => '52vcjiu6unn955epupopag8966',
'_csrf-frontend' => '6b096692be12ca825f9ed6ce21828b37b6e0844fa9a3cd721887528264d3b9c3a:2:{i:0;s:14:"_csrf-frontend";i:1;s:32:"r2_eV9_80g0rFLErF7qz9LNW4FCHZ7ES";}',
];

$_SESSION = [
'__flash' => [],
];
Аватара пользователя
SiZE
Сообщения: 2817
Зарегистрирован: 2011.09.21, 12:39
Откуда: Perm
Контактная информация:

Re: ElasticSearch дает ошибку 400

Сообщение SiZE »

https://www.elastic.co/guide/en/elastic ... query.html
The fuzzy_like_this or flt query has been removed. Instead use the fuzziness parameter with the match query or the More Like This Query.
Ответить