Commit 39ff11a3 by Carsten Brandt

first draft of elasticsearch AR

parent 9f121853
......@@ -58,31 +58,21 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/
public function createCommand($db = null)
{
/** @var $modelClass ActiveRecord */
/** @var ActiveRecord $modelClass */
$modelClass = $this->modelClass;
if ($db === null) {
$db = $modelClass::getDb();
}
$index = $modelClass::indexName();
$type = $modelClass::indexType();
if (is_array($this->where) && Activerecord::isPrimaryKey(array_keys($this->where))) {
// TODO what about mixed queries?
$query = array();
foreach((array) reset($this->where) as $pk) {
$doc = array(
'_id' => $pk,
);
$db->getQueryBuilder()->buildSelect($doc, $this->select);
$query['docs'][] = $doc;
}
$command = $db->createCommand($query, $index, $type);
$command->api = '_mget';
return $command;
} else {
$query = $db->getQueryBuilder()->build($this);
return $db->createCommand($query, $index, $type);
if ($this->type === null) {
$this->type = $modelClass::type();
}
if ($this->index === null) {
$this->index = $modelClass::index();
$this->type = $modelClass::type();
}
$query = $db->getQueryBuilder()->build($this);
return $db->createCommand($query, $this->index, $this->type);
}
/**
......@@ -94,16 +84,15 @@ class ActiveQuery extends Query implements ActiveQueryInterface
public function all($db = null)
{
$command = $this->createCommand($db);
$rows = $command->queryAll();
if (!empty($rows)) {
$models = $this->createModels($rows);
if (!empty($this->with)) {
$this->populateRelations($models, $this->with);
}
return $models;
} else {
return array();
$result = $command->queryAll();
if ($result['total'] == 0) {
return [];
}
$models = $this->createModels($result['hits']);
if (!empty($this->with)) {
$this->findWith($this->with, $models);
}
return $models;
}
/**
......@@ -117,23 +106,22 @@ class ActiveQuery extends Query implements ActiveQueryInterface
public function one($db = null)
{
$command = $this->createCommand($db);
$row = $command->queryOne();
if ($row !== false) {
if ($this->asArray) {
$model = $row;
} else {
/** @var $class ActiveRecord */
$class = $this->modelClass;
$model = $class::create($row);
}
if (!empty($this->with)) {
$models = array($model);
$this->populateRelations($models, $this->with);
$model = $models[0];
}
return $model;
} else {
$result = $command->queryOne();
if ($result['total'] == 0) {
return null;
}
if ($this->asArray) {
$model = reset($result['hits']);
} else {
/** @var ActiveRecord $class */
$class = $this->modelClass;
$model = $class::create(reset($result['hits']));
}
if (!empty($this->with)) {
$models = [$model];
$this->findWith($this->with, $models);
$model = $models[0];
}
return $model;
}
}
......@@ -7,6 +7,7 @@ namespace yii\elasticsearch;
use yii\base\Component;
use yii\db\Exception;
use yii\helpers\Json;
// camelCase vs. _
......@@ -25,9 +26,6 @@ class Command extends Component
* @var Connection
*/
public $db;
public $api = '_search';
/**
* @var string|array the indexes to execute the query on. Defaults to null meaning all indexes
* @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search.html#search-multi-index
......@@ -43,90 +41,35 @@ class Command extends Component
*/
public $query;
// private function createUrl($endPoint = null)
// {
// if ($endPoint === null) {
// $endPoint = $this->api;
// }
// if ($this->index === null && $this->type === null) {
// return '/' . $endPoint;
// }
// $index = $this->index;
// if ($index === null) {
// $index = '_all';
// } elseif (is_array($index)) {
// $index = implode(',', $index);
// }
// $type = $this->type;
// if (is_array($type)) {
// $type = implode(',', $type);
// }
// return '/' . $index . '/' . (empty($type) ? '' : $type . '/') . $endPoint;
// }
//
// public function queryAll()
// {
// $query = $this->query;
// if (empty($query)) {
// $query = '{}';
// }
// if (is_array($query)) {
// $query = Json::encode($query);
// }
// $http = $this->db->http();
// $response = $http->post($this->createUrl(), null, $query)->send();
// $data = Json::decode($response->getBody(true));
// // TODO store query meta data for later use
// $docs = array();
// switch ($this->api) {
// default:
// case '_search':
// if (isset($data['hits']['hits'])) {
// $docs = $data['hits']['hits'];
// }
// break;
// case '_mget':
// if (isset($data['docs'])) {
// $docs = $data['docs'];
// }
// break;
// }
// $rows = array();
// foreach($docs as $doc) {
// // TODO maybe return type info
// if (isset($doc['exists']) && !$doc['exists']) {
// continue;
// }
// $row = $doc['_source'];
// $row['id'] = $doc['_id'];
// $rows[] = $row;
// }
// return $rows;
// }
//
// public function queryOne()
// {
// // TODO set limit
// $rows = $this->queryAll();
// return reset($rows);
// }
//
// public function queryCount()
// {
// //http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-count.html
// $query = $this->query;
// if (empty($query)) {
// $query = '';
// }
// if (is_array($query)) {
// $query = Json::encode($query);
// }
// $http = $this->db->http();
// $response = $http->post($this->createUrl('_count'), null, $query)->send();
// $data = Json::decode($response->getBody(true));
// // TODO store query meta data for later use
// return $data['count'];
// }
public function queryAll($options = [])
{
$query = $this->query;
if (empty($query)) {
$query = '{}';
}
if (is_array($query)) {
$query = Json::encode($query);
}
$url = [
$this->index !== null ? $this->index : '_all',
$this->type !== null ? $this->type : '_all',
'_search'
];
$response = $this->db->http()->post($this->createUrl($url, $options), null, $query)->send();
return Json::decode($response->getBody(true))['hits'];
}
public function queryOne($options = [])
{
$options['size'] = 1;
return $this->queryAll($options);
}
public function queryCount($options = [])
{
$options['search_type'] = 'count';
return $this->queryAll($options);
}
/**
......@@ -161,8 +104,15 @@ class Command extends Component
*/
public function get($index, $type, $id, $options = [])
{
$response = $this->db->http()->post($this->createUrl([$index, $type, $id], $options))->send();
return Json::decode($response->getBody(true));
$httpOptions = [
'exceptions' => false,
];
$response = $this->db->http()->get($this->createUrl([$index, $type, $id], $options), null, $httpOptions)->send();
if ($response->getStatusCode() == 200 || $response->getStatusCode() == 404) {
return Json::decode($response->getBody(true));
} else {
throw new Exception('Elasticsearch request failed.');
}
}
/**
......
......@@ -298,7 +298,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
*/
public static function getTableSchema()
{
throw new NotSupportedException('getTableSchema() is not supported by redis ActiveRecord');
throw new NotSupportedException('getTableSchema() is not supported by redis ActiveRecord.');
}
/**
......@@ -306,7 +306,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
*/
public static function findBySql($sql, $params = [])
{
throw new NotSupportedException('findBySql() is not supported by redis ActiveRecord');
throw new NotSupportedException('findBySql() is not supported by redis ActiveRecord.');
}
/**
......
......@@ -17,15 +17,9 @@ class Customer extends ActiveRecord
public $status2;
public static function columns()
public static function attributes()
{
return array(
'id' => 'integer',
'name' => 'string',
'email' => 'string',
'address' => 'string',
'status' => 'integer',
);
return ['name', 'email', 'address', 'status'];
}
public function getOrders()
......
......@@ -11,12 +11,8 @@ namespace yiiunit\data\ar\elasticsearch;
*/
class Item extends ActiveRecord
{
public static function columns()
public static function attributes()
{
return array(
'id' => 'integer',
'name' => 'string',
'category_id' => 'integer',
);
return ['name', 'category_id'];
}
}
......@@ -12,29 +12,24 @@ namespace yiiunit\data\ar\elasticsearch;
*/
class Order extends ActiveRecord
{
public static function columns()
public static function attributes()
{
return array(
'id' => 'integer',
'customer_id' => 'integer',
'create_time' => 'integer',
'total' => 'integer',
);
return ['customer_id', 'create_time', 'total'];
}
public function getCustomer()
{
return $this->hasOne('Customer', array('id' => 'customer_id'));
return $this->hasOne('Customer', ['id' => 'customer_id']);
}
public function getOrderItems()
{
return $this->hasMany('OrderItem', array('order_id' => 'id'));
return $this->hasMany('OrderItem', ['order_id' => 'id']);
}
public function getItems()
{
return $this->hasMany('Item', array('id' => 'item_id'))
return $this->hasMany('Item', ['id' => 'item_id'])
->via('orderItems', function ($q) {
// additional query configuration
})->orderBy('id');
......@@ -42,9 +37,9 @@ class Order extends ActiveRecord
public function getBooks()
{
return $this->hasMany('Item', array('id' => 'item_id'))
->viaTable('tbl_order_item', array('order_id' => 'id'))
->where(array('category_id' => 1));
return $this->hasMany('Item', ['id' => 'item_id'])
->viaTable('tbl_order_item', ['order_id' => 'id'])
->where(['category_id' => 1]);
}
public function beforeSave($insert)
......
......@@ -12,23 +12,18 @@ namespace yiiunit\data\ar\elasticsearch;
*/
class OrderItem extends ActiveRecord
{
public static function columns()
public static function attributes()
{
return array(
'order_id' => 'integer',
'item_id' => 'integer',
'quantity' => 'integer',
'subtotal' => 'integer',
);
return ['order_id', 'item_id', 'quantity', 'subtotal'];
}
public function getOrder()
{
return $this->hasOne('Order', array('id' => 'order_id'));
return $this->hasOne('Order', ['id' => 'order_id']);
}
public function getItem()
{
return $this->hasOne('Item', array('id' => 'item_id'));
return $this->hasOne('Item', ['id' => 'item_id']);
}
}
......@@ -2,7 +2,6 @@
namespace yiiunit\framework\elasticsearch;
use yii\db\Query;
use yii\elasticsearch\Connection;
use yii\elasticsearch\ActiveQuery;
use yii\helpers\Json;
......@@ -25,42 +24,53 @@ class ActiveRecordTest extends ElasticSearchTestCase
$db->http()->delete('_all')->send();
$customer = new Customer();
$customer->setAttributes(array('id' => 1, 'email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1), false);
$customer->primaryKey = 1;
$customer->setAttributes(array('email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1), false);
$customer->save(false);
$customer = new Customer();
$customer->setAttributes(array('id' => 2, 'email' => 'user2@example.com', 'name' => 'user2', 'address' => 'address2', 'status' => 1), false);
$customer->primaryKey = 2;
$customer->setAttributes(array('email' => 'user2@example.com', 'name' => 'user2', 'address' => 'address2', 'status' => 1), false);
$customer->save(false);
$customer = new Customer();
$customer->setAttributes(array('id' => 3, 'email' => 'user3@example.com', 'name' => 'user3', 'address' => 'address3', 'status' => 2), false);
$customer->primaryKey = 3;
$customer->setAttributes(array('email' => 'user3@example.com', 'name' => 'user3', 'address' => 'address3', 'status' => 2), false);
$customer->save(false);
// INSERT INTO tbl_category (name) VALUES ('Books');
// INSERT INTO tbl_category (name) VALUES ('Movies');
$item = new Item();
$item->setAttributes(array('id' => 1, 'name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1), false);
$item->primaryKey = 1;
$item->setAttributes(array('name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1), false);
$item->save(false);
$item = new Item();
$item->setAttributes(array('id' => 2, 'name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1), false);
$item->primaryKey = 2;
$item->setAttributes(array('name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1), false);
$item->save(false);
$item = new Item();
$item->setAttributes(array('id' => 3, 'name' => 'Ice Age', 'category_id' => 2), false);
$item->primaryKey = 3;
$item->setAttributes(array('name' => 'Ice Age', 'category_id' => 2), false);
$item->save(false);
$item = new Item();
$item->setAttributes(array('id' => 4, 'name' => 'Toy Story', 'category_id' => 2), false);
$item->primaryKey = 4;
$item->setAttributes(array('name' => 'Toy Story', 'category_id' => 2), false);
$item->save(false);
$item = new Item();
$item->setAttributes(array('id' => 5, 'name' => 'Cars', 'category_id' => 2), false);
$item->primaryKey = 5;
$item->setAttributes(array('name' => 'Cars', 'category_id' => 2), false);
$item->save(false);
$order = new Order();
$order->setAttributes(array('id' => 1, 'customer_id' => 1, 'create_time' => 1325282384, 'total' => 110.0), false);
$order->primaryKey = 1;
$order->setAttributes(array('customer_id' => 1, 'create_time' => 1325282384, 'total' => 110.0), false);
$order->save(false);
$order = new Order();
$order->setAttributes(array('id' => 2, 'customer_id' => 2, 'create_time' => 1325334482, 'total' => 33.0), false);
$order->primaryKey = 2;
$order->setAttributes(array('customer_id' => 2, 'create_time' => 1325334482, 'total' => 33.0), false);
$order->save(false);
$order = new Order();
$order->setAttributes(array('id' => 3, 'customer_id' => 2, 'create_time' => 1325502201, 'total' => 40.0), false);
$order->primaryKey = 3;
$order->setAttributes(array('customer_id' => 2, 'create_time' => 1325502201, 'total' => 40.0), false);
$order->save(false);
// $orderItem = new OrderItem();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment