yiisoft / yii2-elasticsearch

Yii 2 Elasticsearch extension
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
427 stars 253 forks source link

how to use updateAll #128

Closed terrywaterzhao closed 4 years ago

terrywaterzhao commented 7 years ago
/**
     * Updates all records whos primary keys are given.
     * For example, to change the status to be 1 for all customers whose status is 2:
     *
     * ~~~
     * Customer::updateAll(['status' => 1], ['status' => 2]);
     * ~~~
     *
     * @param array $attributes attribute values (name-value pairs) to be saved into the table
     * @param array $condition the conditions that will be passed to the `where()` method when building the query.
     * Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
     * @see [[ActiveRecord::primaryKeysByCondition()]]
     * @return integer the number of rows updated
     * @throws Exception on error.
     */
    public static function updateAll($attributes, $condition = [])
    {
        $primaryKeys = static::primaryKeysByCondition($condition);
        if (empty($primaryKeys)) {
            return 0;
        }

        $bulkCommand = static::getDb()->createBulkCommand([
            "index" => static::index(),
            "type" => static::type(),
        ]);
        foreach ($primaryKeys as $pk) {
            $bulkCommand->addAction(["update" => ["_id" => $pk]], ["doc" => $attributes]);
        }
        $response = $bulkCommand->execute();

        $n = 0;
        $errors = [];
        foreach ($response['items'] as $item) {
            if (isset($item['update']['status']) && $item['update']['status'] == 200) {
                $n++;
            } else {
                $errors[] = $item['update'];
            }
        }
        if (!empty($errors) || isset($response['errors']) && $response['errors']) {
            throw new Exception(__METHOD__ . ' failed updating records.', $errors);
        }

        return $n;
    }

updateAll($attributes, $condition = []) , my work is:

$condition is array, like mysql "in" search,

$condition = [ 'in','customer_id',$delete_customer_id_arr ] , ,$delete_customer_id_arr is array

but it can not use, can you help me? thank you

beowulfenator commented 7 years ago

Exactly what error are you getting?

fancyecommerce commented 7 years ago
EsCustomerData::updateAll(
                    [
                        'customer_id' => $primary_customer_id
                    ], 
                    [
                        'in','customer_id',$delete_customer_id_arr
                    ]
                    );

first param is: array(1) { ["customer_id"]=> int(11762180) } last param is: array(3) { [0]=> string(2) "in" [1]=> string(11) "customer_id" [2]=> array(1) { [0]=> int(11840550) } } error info:

Exception 'yii\elasticsearch\Exception' with message 'Elasticsearch request failed with code 400.'

in /www/web/tomtop_market/vendor/yiisoft/yii2-elasticsearch/Connection.php:509

Error Info:
Array
(
    [requestMethod] => GET
    [requestUrl] => http://192.168.0.210:9200/_search/scroll?scroll_id=c2Nhbjs1OzI5NDMwOl9zc0t2MjdHVDMtWjdTRGN5ZVR4UlE7Mjk0Mjg6X3NzS3YyN0dUMy1aN1NEY3llVHhSUTsyOTQzMjpfc3NLdjI3R1QzLVo3U0RjeWVUeFJROzI5NDMxOl9zc0t2MjdHVDMtWjdTRGN5ZVR4UlE7Mjk0Mjk6X3NzS3YyN0dUMy1aN1NEY3llVHhSUTsxO3RvdGFsX2hpdHM6Mjs%3D&scroll=1m
    [requestBody] => 
    [responseCode] => 400
    [responseHeaders] => Array
        (
            [content-type] => application/json; charset=UTF-8
            [content-length] => 319
        )

    [responseBody] => Array
        (
            [error] => Array
                (
                    [root_cause] => Array
                        (
                            [0] => Array
                                (
                                    [type] => illegal_argument_exception
                                    [reason] => Unknown parameter [_source] in request body or parameter is of the wrong type[START_ARRAY] 
                                )

                        )

                    [type] => illegal_argument_exception
                    [reason] => Unknown parameter [_source] in request body or parameter is of the wrong type[START_ARRAY] 
                )

            [status] => 400
        )

)

Stack trace:
#0 /www/web/tomtop_market/vendor/yiisoft/yii2-elasticsearch/Connection.php(271): yii\elasticsearch\Connection->httpRequest('GET', Array, NULL, false)
#1 /www/web/tomtop_market/vendor/yiisoft/yii2-elasticsearch/Command.php(392): yii\elasticsearch\Connection->get(Array, Array)
#2 /www/web/tomtop_market/vendor/yiisoft/yii2-elasticsearch/BatchQueryResult.php(166): yii\elasticsearch\Command->scroll(Array)
#3 /www/web/tomtop_market/vendor/yiisoft/yii2-elasticsearch/BatchQueryResult.php(127): yii\elasticsearch\BatchQueryResult->fetchData()
#4 /www/web/tomtop_market/vendor/yiisoft/yii2-elasticsearch/BatchQueryResult.php(117): yii\elasticsearch\BatchQueryResult->next()
#5 /www/web/tomtop_market/vendor/yiisoft/yii2-elasticsearch/ActiveRecord.php(580): yii\elasticsearch\BatchQueryResult->rewind()
#6 /www/web/tomtop_market/vendor/yiisoft/yii2-elasticsearch/ActiveRecord.php(604): yii\elasticsearch\ActiveRecord::primaryKeysByCondition(Array)
#7 /www/web/tomtop_market/appadmin/code/Ta/console/user/IdentityController.php(137): yii\elasticsearch\ActiveRecord::updateAll(Array, Array)
#8 [internal function]: appadmin\code\Ta\console\user\IdentityController->actionProcess('2017_03_16')
#9 /www/web/tomtop_market/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)
#10 /www/web/tomtop_market/vendor/yiisoft/yii2/base/Controller.php(154): yii\base\InlineAction->runWithParams(Array)
#11 /www/web/tomtop_market/vendor/yiisoft/yii2/console/Controller.php(108): yii\base\Controller->runAction('process', Array)
#12 /www/web/tomtop_market/vendor/yiisoft/yii2/base/Module.php(454): yii\console\Controller->runAction('process', Array)
#13 /www/web/tomtop_market/vendor/yiisoft/yii2/console/Application.php(167): yii\base\Module->runAction('ta/user/identit...', Array)
#14 /www/web/tomtop_market/vendor/yiisoft/yii2/console/Application.php(143): yii\console\Application->runAction('ta/user/identit...', Array)
#15 /www/web/tomtop_market/vendor/yiisoft/yii2/base/Application.php(375): yii\console\Application->handleRequest(Object(yii\console\Request))
#16 /www/web/tomtop_market/yii(27): yii\base\Application->run()
#17 {main}
#################### mapreduce Complete ####################
fancyecommerce commented 7 years ago

elasticserach active mapping is:

public static function mapConfig(){
        return [
            'properties' => [
                'customer_date_id'  => ['type' => 'string',"index" => "not_analyzed"],

                'category'          => ['type' => 'string',"index" => "not_analyzed"],
                'order_status'      => ['type' => 'string',"index" => "not_analyzed"],

                'customer_id'       => ['type' => 'long',"index" => "not_analyzed"],
                'pv'                => ['type' => 'integer',"index" => "not_analyzed"],
                'ip'                => ['type' => 'string',"index" => "not_analyzed"],
                'service_date_str'  => ['type' => 'date',"index" => "not_analyzed"],
                'service_datetime'  => ['type' => 'string',"index" => "not_analyzed"],
                'service_timestamp' => ['type' => 'integer',"index" => "not_analyzed"],
                'devide'            => ['type' => 'string',"index" => "not_analyzed"],
                'user_agent'        => ['type' => 'string',"index" => "not_analyzed"],
                'browser_name'      => ['type' => 'string',"index" => "not_analyzed"],
                'browser_version'   => ['type' => 'string',"index" => "not_analyzed"],
                'browser_date'      => ['type' => 'string',"index" => "not_analyzed"],
                'browser_lang'      => ['type' => 'string',"index" => "not_analyzed"],
                'operate'           => ['type' => 'string',"index" => "not_analyzed"],
                'operate_relase'    => ['type' => 'string',"index" => "not_analyzed"],
                'domain'            => ['type' => 'string',"index" => "not_analyzed"],
                'url'               => ['type' => 'string',"index" => "not_analyzed"],
                'title'             => ['type' => 'string',"index" => "not_analyzed"],
                'refer_url'         => ['type' => 'string',"index" => "not_analyzed"],
                'first_referrer_domain' => ['type' => 'string',"index" => "not_analyzed"],
                'is_return'         => ['type' => 'integer',"index" => "not_analyzed"],
                'uuid'              => ['type' => 'string',"index" => "not_analyzed"],
                'device_pixel_ratio'=> ['type' => 'string',"index" => "not_analyzed"],
                'resolution'        => ['type' => 'string',"index" => "not_analyzed"],
                'color_depth'       => ['type' => 'string',"index" => "not_analyzed"],
                'website_id'        => ['type' => 'integer',"index" => "not_analyzed"],
                'language'          => ['type' => 'string',"index" => "not_analyzed"],

                'country_code'      => ['type' => 'string',"index" => "not_analyzed"],
                'country_name'      => ['type' => 'string',"index" => "not_analyzed"],

                'order_status'      => ['type' => 'string',"index" => "not_analyzed"],
                'login_email'       => ['type' => 'string',"index" => "not_analyzed"],
                'register_email'    => ['type' => 'string',"index" => "not_analyzed"],
                'customer_email'    => ['type' => 'string',"index" => "not_analyzed"],

                'mid'               => ['type' => 'string',"index" => "not_analyzed"],
                'utm_content'       => ['type' => 'string',"index" => "not_analyzed"],
                'cl_market_group'   => ['type' => 'string',"index" => "not_analyzed"],
                'utm_campaign'      => ['type' => 'string',"index" => "not_analyzed"],
                'utm_source'        => ['type' => 'string',"index" => "not_analyzed"],
                'utm_medium'        => ['type' => 'string',"index" => "not_analyzed"],
                'utm_design'        => ['type' => 'string',"index" => "not_analyzed"],

                'sku'               => ['type' => 'string',"index" => "not_analyzed"],
                'sku_cart'          => ['type' => 'string',"index" => "not_analyzed"],
                'sku_order'         => ['type' => 'string',"index" => "not_analyzed"],
                'sku_order_success' => ['type' => 'string',"index" => "not_analyzed"],

                'category'          => ['type' => 'string',"index" => "not_analyzed"],
                'cart'              => ['type' => 'string',"index" => "not_analyzed"],
                'search'            => ['type' => 'string',"index" => "not_analyzed"],
                'order'             => ['type' => 'string',"index" => "not_analyzed"],

                'visit_page_sku'        => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_category'   => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_search'     => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_cart'       => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order'      => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order_processing'       => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order_pending'          => ['type' => 'integer',"index" => "not_analyzed"],

                'visit_page_order_amount'           => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order_processing_amount'=> ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order_pending_amount'   => ['type' => 'integer',"index" => "not_analyzed"],

                'data'              => ['type' => 'string',"index" => "not_analyzed"],

                /*
                'cart'          => ['type' => 'nested',
                    "properties" => [
                        'sku'   => ['type' => 'string',"index" => "not_analyzed"],
                        'qty'   => ['type' => 'integer',"index" => "not_analyzed"],
                        'currency'  => ['type' => 'string',"index" => "not_analyzed"],
                        'currency_rate' => ['type' => 'float',"index" => "not_analyzed"],
                    ],
                ],

                'search'            => ['type' => 'nested',
                    "properties" => [
                        'text'          => ['type' => 'string',"index" => "not_analyzed"],
                        'result_qty'    => ['type' => 'integer',"index" => "not_analyzed"],
                    ],
                ],
                */
                'currency'          => ['type' => 'string',"index" => "not_analyzed"],
                'url_new'           => ['type' => 'string',"index" => "not_analyzed"],
                'stay_seconds'      => ['type' => 'integer',"index" => "not_analyzed"],
                'first_visit_this_url'  => ['type' => 'string',"index" => "not_analyzed"],
            ]
        ];
    }
fancyecommerce commented 7 years ago

elasticserach active mapping is:

public static function mapConfig(){
        return [
            'properties' => [
                'customer_date_id'  => ['type' => 'string',"index" => "not_analyzed"],

                'category'          => ['type' => 'string',"index" => "not_analyzed"],
                'order_status'      => ['type' => 'string',"index" => "not_analyzed"],

                'customer_id'       => ['type' => 'long',"index" => "not_analyzed"],
                'pv'                => ['type' => 'integer',"index" => "not_analyzed"],
                'ip'                => ['type' => 'string',"index" => "not_analyzed"],
                'service_date_str'  => ['type' => 'date',"index" => "not_analyzed"],
                'service_datetime'  => ['type' => 'string',"index" => "not_analyzed"],
                'service_timestamp' => ['type' => 'integer',"index" => "not_analyzed"],
                'devide'            => ['type' => 'string',"index" => "not_analyzed"],
                'user_agent'        => ['type' => 'string',"index" => "not_analyzed"],
                'browser_name'      => ['type' => 'string',"index" => "not_analyzed"],
                'browser_version'   => ['type' => 'string',"index" => "not_analyzed"],
                'browser_date'      => ['type' => 'string',"index" => "not_analyzed"],
                'browser_lang'      => ['type' => 'string',"index" => "not_analyzed"],
                'operate'           => ['type' => 'string',"index" => "not_analyzed"],
                'operate_relase'    => ['type' => 'string',"index" => "not_analyzed"],
                'domain'            => ['type' => 'string',"index" => "not_analyzed"],
                'url'               => ['type' => 'string',"index" => "not_analyzed"],
                'title'             => ['type' => 'string',"index" => "not_analyzed"],
                'refer_url'         => ['type' => 'string',"index" => "not_analyzed"],
                'first_referrer_domain' => ['type' => 'string',"index" => "not_analyzed"],
                'is_return'         => ['type' => 'integer',"index" => "not_analyzed"],
                'uuid'              => ['type' => 'string',"index" => "not_analyzed"],
                'device_pixel_ratio'=> ['type' => 'string',"index" => "not_analyzed"],
                'resolution'        => ['type' => 'string',"index" => "not_analyzed"],
                'color_depth'       => ['type' => 'string',"index" => "not_analyzed"],
                'website_id'        => ['type' => 'integer',"index" => "not_analyzed"],
                'language'          => ['type' => 'string',"index" => "not_analyzed"],

                'country_code'      => ['type' => 'string',"index" => "not_analyzed"],
                'country_name'      => ['type' => 'string',"index" => "not_analyzed"],

                'order_status'      => ['type' => 'string',"index" => "not_analyzed"],
                'login_email'       => ['type' => 'string',"index" => "not_analyzed"],
                'register_email'    => ['type' => 'string',"index" => "not_analyzed"],
                'customer_email'    => ['type' => 'string',"index" => "not_analyzed"],

                'mid'               => ['type' => 'string',"index" => "not_analyzed"],
                'utm_content'       => ['type' => 'string',"index" => "not_analyzed"],
                'cl_market_group'   => ['type' => 'string',"index" => "not_analyzed"],
                'utm_campaign'      => ['type' => 'string',"index" => "not_analyzed"],
                'utm_source'        => ['type' => 'string',"index" => "not_analyzed"],
                'utm_medium'        => ['type' => 'string',"index" => "not_analyzed"],
                'utm_design'        => ['type' => 'string',"index" => "not_analyzed"],

                'sku'               => ['type' => 'string',"index" => "not_analyzed"],
                'sku_cart'          => ['type' => 'string',"index" => "not_analyzed"],
                'sku_order'         => ['type' => 'string',"index" => "not_analyzed"],
                'sku_order_success' => ['type' => 'string',"index" => "not_analyzed"],

                'category'          => ['type' => 'string',"index" => "not_analyzed"],
                'cart'              => ['type' => 'string',"index" => "not_analyzed"],
                'search'            => ['type' => 'string',"index" => "not_analyzed"],
                'order'             => ['type' => 'string',"index" => "not_analyzed"],

                'visit_page_sku'        => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_category'   => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_search'     => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_cart'       => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order'      => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order_processing'       => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order_pending'          => ['type' => 'integer',"index" => "not_analyzed"],

                'visit_page_order_amount'           => ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order_processing_amount'=> ['type' => 'integer',"index" => "not_analyzed"],
                'visit_page_order_pending_amount'   => ['type' => 'integer',"index" => "not_analyzed"],

                'data'              => ['type' => 'string',"index" => "not_analyzed"],

                /*
                'cart'          => ['type' => 'nested',
                    "properties" => [
                        'sku'   => ['type' => 'string',"index" => "not_analyzed"],
                        'qty'   => ['type' => 'integer',"index" => "not_analyzed"],
                        'currency'  => ['type' => 'string',"index" => "not_analyzed"],
                        'currency_rate' => ['type' => 'float',"index" => "not_analyzed"],
                    ],
                ],

                'search'            => ['type' => 'nested',
                    "properties" => [
                        'text'          => ['type' => 'string',"index" => "not_analyzed"],
                        'result_qty'    => ['type' => 'integer',"index" => "not_analyzed"],
                    ],
                ],
                */
                'currency'          => ['type' => 'string',"index" => "not_analyzed"],
                'url_new'           => ['type' => 'string',"index" => "not_analyzed"],
                'stay_seconds'      => ['type' => 'integer',"index" => "not_analyzed"],
                'first_visit_this_url'  => ['type' => 'string',"index" => "not_analyzed"],
            ]
        ];
    }
beowulfenator commented 7 years ago

Which version of ES, which version of this component?

beowulfenator commented 4 years ago

Could not reproduce