php es使用类库
<?php class ElasticService { const NO_SEARCH_CATEGORY = [ '粽子','粽' ]; const NO_SEARCH_NAME = [ '粽子','粽' ]; private $master = [ "https://127.0.0.1:9200", "https://127.0.0.1:9201", "https://127.0.0.1:9202", ]; private $backup = [ "http://127.0.0.1:9200", ]; private $username = "elastic"; private $password = "123456"; private static $instance = null; public static function new($env = 'master') { if (is_null(self::$instance)) { self::$instance = new self($env); } return self::$instance; } /* * 取模或ip_hash或url_hash */ public function __construct($env) { if ($env == 'master') { $this->baseUrl = $this->master[ip_hash(count($this->master))]; }else{ $this->baseUrl = $this->backup[ip_hash(count($this->backup))]; } } /* * 查看状态 */ public function health() { $method = "_cat/health?v&format=json"; $request = "GET"; return $this->curl($method, $request); } /* * 查看分词 */ public function analyze($text, $analyzer = "ik_max_word") { $method = "_analyze"; $request = "POST"; $params = [ "analyzer" => $analyzer, "text" => $text, ]; return $this->curl($method, $request, $params); } public function analyzeForSearch($text,$saparator = " ") { $arr = $this->analyze($text); $word = []; if(is_array($arr)){ foreach ($arr as $key => $value){ if(!is_array($value)) continue; foreach ($value as $k => $v){ if(isset($v['token']) && $v['token'] != $text || count($value) == 1){ if($v['token'] == 1) continue; $word[] = $v['token']; } } } }else{ $word[] = $text; } if(mb_strlen($text) <= 2) $word[] = $text; return implode($saparator,$word); } // 查看当前索引中字段的结构和属性 public function mapping($indexName) { $method = "$indexName/_mapping"; $request = "GET"; return $this->curl($method, $request); } /** * $data = ['suning_skus' => ['type' => 'text']]; */ public function updateMapping($indexName, $indexProperties) { $method = "$indexName/_mapping"; $request = "PUT"; $params = [ "properties" => $indexProperties, ]; return $this->curl($method, $request, $params); } /* * 查看索引 */ public function index() { $method = "_cat/indices?v&format=json"; $request = "GET"; return $this->curl($method, $request); } /* * 创建索引 */ public function create($indexName, $indexProperties, $indexSettings = []) { $method = "$indexName"; $request = "PUT"; $defaultSettings = [ "number_of_shards" => 1, "number_of_replicas" => 0, ]; $params = [ "settings" => [ "index" => array_merge($defaultSettings, $indexSettings), "analysis" => [ "analyzer" => [ "default" =>[ "type" => "ik_max_word", ], ], ], ], "mappings" => [ "properties" => $indexProperties, ], ]; return $this->curl($method, $request, $params); } /* * 设置索引 */ public function setting($indexName, $indexSettings = []) { $method = "$indexName/_settings"; $request = "PUT"; $defaultSettings = [ "number_of_replicas" => 2, "max_result_window" => 1000000, ]; $params = [ "index" => array_merge($defaultSettings, $indexSettings), ]; return $this->curl($method, $request, $params); } /* * 设置日志级别 */ public function logger() { $method = "_cluster/settings"; $request = "PUT"; $params = [ "transient" => [ "logger._root" => "error", ], ]; return $this->curl($method, $request, $params); } /* * 删除索引 */ public function drop($indexName) { $method = "$indexName"; $request = "DELETE"; return $this->curl($method, $request); } /* * 增加文档 * $doc = [ * filed => value * ]; */ public function insert($indexName, $index, $doc) { $method = "$indexName/_doc/$index"; $request = "POST"; return $this->curl($method, $request, $doc); } /* * 批量增加文档 */ public function insertAll($indexName, $docs) { $method = "_bulk"; $reuqest = "POST"; $batch = []; foreach ($docs as $doc) { $batch[] = json_encode(["index"=>["_index"=>$indexName, "_id"=>$doc['id']]]); $batch[] = json_encode($doc); } return $this->curl($method, $reuqest, $batch, true); } /* * 删除文档 */ public function delete($indexName, $index) { $method = "$indexName/_doc/$index"; $request = "DELETE"; return $this->curl($method, $request); } /* * 批量删除文档 * $query = [ * 'match' => [ * ] * ] */ public function deleteAll($indexName, $query) { $method = "$indexName/_delete_by_query"; $request = "POST"; $params = [ 'query' => $query, ]; return $this->curl($method, $request, $params); } /* * 更改文档 * $doc = [ * filed => value * ]; */ public function update($indexName, $index, $doc) { $method = "$indexName/_update/$index"; $request = "POST"; $params = [ "doc" => $doc, ]; return $this->curl($method, $request, $params); } /* * 查询文档 * $query = [ * 'match' => [ * ] * ] * $sort = [ * "id" => [ * 'order' => "asc" * ] * ] */ public function getFind($indexName, $query = [], $field = [], $highlight = []) { $method = "$indexName/_search"; $request = "GET"; $params = [ "query" => $query, ]; if ($field) { $params['_source'] = $field; } if ($highlight) { $params['highlight'] = $highlight; } $response = $this->curl($method, $request, $params); $hits = $response['hits']['hits']; if(!$hits){ return null; } $data = array_map(function ($hit) { return $hit['_source']; }, $hits); return $data[0]; } public function getAggs($indexName, $query = [], $field = [], $highlight = []) { $map['bool']['must'][]['range']['id'] = ['lte'=>10000000]; $method = "$indexName/_search"; $request = "GET"; $params = [ "query" => $map, "sort" =>[ 'id'=>'desc', ], 'size'=>1, ]; if ($field) { $params['_source'] = $field; } if ($highlight) { $params['highlight'] = $highlight; } $response = $this->curl($method, $request, $params); $hits = $response['hits']['hits']; if(!$hits){ return null; } $data = array_map(function ($hit) { return $hit['_source']; }, $hits); return $data[0]['id'] ; } /** * 查询一列值 * @param $indexName * @param $query * @param $column * @param $sort * @param $field * @param $highlight * @return array */ public function getColumn($indexName, $query,$column, $sort = [], $field = [], $highlight = []) { $method = "$indexName/_search"; $request = "GET"; $params = [ "track_total_hits" => true, "query" => $query, "sort" => $sort, ]; if ($field) { $params['_source'] = $field; } if ($highlight) { $params['highlight'] = $highlight; } $response = $this->curl($method, $request, $params); $hits = $response['hits']['hits']; if(!$hits){ return []; } $data = array_map(function ($hit) { return $hit['_source']; }, $hits); return array_column($data,$column); } /** * 获取单个值 * @param $indexName * @param $query * @param $column * @param $sort * @param $field * @param $highlight * @return mixed|null */ public function getValue($indexName, $query,$column, $sort = ['id'=>'asc'], $field = [], $highlight = []) { $method = "$indexName/_search"; $request = "GET"; $params = [ "track_total_hits" => true, "query" => $query, "sort" => $sort, 'size'=>10000, 'from'=>0, ]; if ($field) { $params['_source'] = $field; } if ($highlight) { $params['highlight'] = $highlight; } $response = $this->curl($method, $request, $params); $hits = $response['hits']['hits']; if(!$hits){ return null; } $data = array_map(function ($hit) { return $hit['_source']; }, $hits); return $data[0][$column]; } public function reindex($oldIndexName , $newIndexName) { $method = '_reindex'; $request = 'POST'; $params = [ "source" => [ "index" => $oldIndexName, ], "dest" => [ "index" => $newIndexName, ], ]; return $this->curl($method, $request, $params); } public function selectData($indexName, $query, $field = [], $highlight = []) { $method = "$indexName/_search"; $request = "GET"; $params = [ "track_total_hits" => true, "from" => 0, "size" => 10000, ]; if($query)$params['query'] = $query; if ($field) { $params['_source'] = $field; } if ($highlight) { $params['highlight'] = $highlight; } $response = $this->curl($method, $request, $params); $hits = $response['hits']['hits']; if(!$hits){ $rows['count'] = 0; }else{ $rows['count'] = $response['hits']['total']['value']; // 总匹配数 } return $rows['count'] == 0 ? [] : array_map(function ($hit) { return $hit['_source']; }, $hits); } public function select($indexName, $query, $sort = [], $from = 0, $size = 20, $field = [], $highlight = [],$type = 1 ) { if($size <= 0) $size = 10000; $method = "$indexName/_search"; $request = "POST"; $params = [ "track_total_hits" => true, "sort" => $sort, "query" => $query, "from" => $from, "size" => $size, ]; if ($field) { $params['_source'] = $field; } if ($highlight) { $params['highlight'] = $highlight; } $data = $this->curl($method, $request, $params); $total = $data['hits']['total']['value'] ?? 0; $hits = $data['hits']['hits'] ?? []; $rows['total'] = $total; $rows['per_page'] = $size; $rows['last_page'] = 1; if($total > 0){ $rows['last_page'] = (int)ceil($total/$size); } if($type == 1){ $rows['data'] = $total == 0 ? [] : array_map(function ($hit) { return $hit['_source']; }, $hits); }else{ if($hits){ $brandList = db('brands')->column('stars','id'); foreach ($hits as $key=>$item) { $rows['data'][$key] = $item['_source']; $rows['data'][$key]['stars'] = $brandList[$item['_source']['brand_id']] ?? 0; if (isset($item['highlight'])) $rows['data'][$key]['goods_name'] = $item['highlight']['goods_name'][0]; } }else{ $rows['data'] = []; } } return $rows; } /** * 查询ES(SQL查询) * @param $indexName * @param $sql * @return mixed */ public function _sqlSearch($indexName, $sql = '') { $method = '_sql?format=json'; $request = 'GET'; $params = [ "query" => $sql, ]; $response = $this->curl($method, $request, $params); if(count($response['rows']) == 0){ return ['rows'=>[],'total'=>0]; } $data = []; foreach($response['rows'] as $key=>$value){ foreach ($value as $k=>$v){ $data[$key][$response['columns'][$k]['name']] = $v; } } return ['rows'=>$data,'total'=>count($data)]; } /** * 映射字段 * @param $indexName * @return mixed */ public function getMapping($indexName) { $method = $indexName.'/_mapping'; $request = 'GET'; $params = []; $mapping = $this->curl($method, $request, $params); $mapping = $mapping[$indexName]; $params = ['index' => 'yc_product']; $params['settings'] = [ 'analysis' => [ 'analyzer' => [ 'ik_max_word' => [ 'type' => 'ik_max_word', ], 'ik_smart' => [ 'type' => 'ik_smart', ], ], ], ]; foreach ($mapping['mappings']['properties'] as $key => $value) { $params['mappings']['properties'][$key] = $value; if ($key == 'goods_name' || $key == 'model' ) { // if($key == 'name'){ // $params['mappings']['properties'][$key]['type'] = 'text'; // $params['mappings']['properties'][$key]['fields']['keyword'] = [ // 'type'=>'keyword', // 'ignore_above'=>256 // ]; // } $params['mappings']['properties'][$key]['analyzer'] = 'ik_max_word'; $params['mappings']['properties'][$key]['search_analyzer'] = 'ik_smart'; } } $params['body']['settings'] = $params['settings']; $params['body']['mappings'] = $params['mappings']; unset($params['settings']); unset($params['mappings']); $request = "PUT"; $params = $params['body']; return $this->curl('yc_product', $request, $params); } /** * 查询ES(SQL查询)查询单条数据 * @param $sql * @return mixed */ public function _sqlSearchFind($sql = '') { $method = '_sql?format=json'; $request = 'GET'; $params = [ "query" => $sql, ]; $response = $this->curl($method, $request, $params); if(count($response['rows']) == 0){ return []; } $data = []; foreach($response['rows'] as $key=>$value){ foreach ($value as $k=>$v){ $data[$response['columns'][$k]['name']] = $v; } } return $data; } /** * 查询ES(SQL查询)查询单条数据 * @param $indexName * @param $sql * @return mixed */ public function _sqlSearchColumn( $sql = '',$field) { $method = '_sql?format=json'; $request = 'GET'; $params = [ "query" => $sql, "fetch_size" => 1000000, ]; $response = $this->curl($method, $request, $params); if(!isset($response['rows']) || count($response['rows']) == 0){ return null; } foreach($response['rows'] as $key=>$value){ foreach ($value as $k=>$v){ $data[$key][$response['columns'][$k]['name']] = $v; } } return array_column($data,$field); } /** * 查询ES(SQL查询)查询单个值 * @param $indexName * @param $sql * @return mixed */ public function _sqlSearchValue( $sql = '') { $method = '_sql?format=json'; $request = 'GET'; $params = [ "query" => $sql, ]; $response = $this->curl($method, $request, $params); if(count($response['rows']) == 0){ return null; } foreach($response['rows'] as $key=>$value){ foreach ($value as $k=>$v){ return $v; } } } /** * 查询ES(SQL查询)获取总数 * @param $indexName * @param $sql * @return mixed */ public function _sqlSearchCount( $sql = '') { $method = '_sql?format=json'; $request = 'GET'; $params = [ "query" => $sql, ]; $response = $this->curl($method, $request, $params); return $response['rows']; } public function aggregate($indexName, $query, $aggs) { if(isset($query['delivery_type'])) unset($query['delivery_type']); $method = "$indexName/_search"; $request = "GET"; $params = [ "query" => $query, "size" => 0, "aggs" => $aggs, ]; return $this->curl($method, $request, $params); } private function curl($method, $request, $params = [], $batch = false) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->baseUrl.'/'.$method); curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if ($params) { $headers = [ "Content-Type: application/json;charset=utf-8", ]; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); if ($batch) { curl_setopt($ch, CURLOPT_POSTFIELDS, join("\n", $params) . "\n"); }else{ curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); } } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $res = curl_exec($ch); curl_close($ch); return json_decode($res, true); } /** * 同步分类 * @param $id * @return array|mixed */ public function mallCategory($id) { $category = db('mall_category')->where(['id' => $id])->find(); if (!$category) { return []; } return ElasticService::new()->insert("mall_category", $id, $category); } /** * 供应链 * ES同步标准商品表的同时同步所有关联商品 * ElasticService::new()->addMallProduct(1); * ElasticService::new()->addMallProduct([1,2,3]); * @param $ids * @return mixed */ public function addMallProduct($ids) { if(empty($ids)) return []; try { $this->mall_product($ids); $this->AddXhElasticGoods($ids,2); }catch (\Exception $exception){ LogService::responseLog('ElasticService',$ids,$exception->getMessage() . $exception->getLine()); } } /** * @param $id * @return \app\common\service\mixted|false */ public function mall_product($id) { $data = db('product')->where(['id' => ['in', $id]])->select(); $products = []; foreach ($data as $product){ if ($product['spuid'] > 0) { $spuid_product = db('product')->where(['id'=>$product['spuid']])->find(); $product['image'] = $spuid_product['image']; $product['image_list'] = $spuid_product['image_list']; $product['description'] = $spuid_product['description']; $product['shop_amount1'] = $spuid_product['shop_amount1'] * $product['specs_num']; $product['shop_amount2'] = $spuid_product['shop_amount2'] * $product['specs_num']; $product['shop_amount3'] = $spuid_product['shop_amount3'] * $product['specs_num']; $product['shop_amount4'] = $spuid_product['shop_amount4'] * $product['specs_num']; } $products[] = $product; } if (empty($products)) { return false; } return ElasticService::new()->insertAll("mall_product",$products); } /** * 添加/更新 * ElasticService::new()->AddXhElasticGoods(1); * ElasticService::new()->AddXhElasticGoods([1,2,3]); * @param $ids * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function AddXhElasticGoods($ids,$type = 1) { // type = 2 时 ids = product_id 只适用于store_goods if($type == 2){ $ids = db('store_goods')->where(['product_id' => ['in',$ids]])->column('id'); } if (is_numeric($ids)) { if ($ids < 100000000) { $this->store_goods($ids); }else{ $this->store_product($ids); } }else if (is_array($ids)) { foreach ($ids as $id) { if ($id < 100000000) { $this->store_goods($id); }else{ $this->store_product($id); } } } } /** * ElasticService::new()->delXhElasticGoods(1); * ElasticService::new()->delXhElasticGoods([1,2,3]); * 删除商品 * @param $ids * @return void */ public function delXhElasticGoods($ids) { if (is_numeric($ids)) { $this->delete('store_product',$ids); }else if (is_array($ids)) { foreach ($ids as $id) { $this->delete('store_product',$id); } } } /** * @param $id * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function store_goods($id,$data = []) { if(!empty($data)){ $store_goods_list = $data; }else{ $store_goods = db('store_goods')->where(['id'=>$id])->find(); if ( !$store_goods) { return; } $store_goods_list = [$store_goods]; } $products = []; foreach ($store_goods_list as $store_goods){ if($store_goods['product_id'] > 100000000){ $mall_product = db('store_product')->where(['id'=>$store_goods['product_id']])->find(); }else{ $mall_product = db('product')->where(['id'=>$store_goods['product_id']])->find(); $sellablePlatform = db('product_sellable_platform')->where('product_id', $store_goods['product_id'])->column('api_platform_id'); $sellablePlatform = array_unique($sellablePlatform); if (in_array(0, $sellablePlatform)) { $sellablePlatform = 0; } } if ( !$mall_product) { continue; } if (isset($mall_product['spuid']) && $mall_product['spuid'] > 0) { $spuid_product = db('product')->where(['id'=>$mall_product['spuid']])->find(); $mall_product['image'] = $spuid_product['image']; $mall_product['image_list'] = $spuid_product['image_list']; $mall_product['description'] = $spuid_product['description']; } $distributor_id = db('store_user')->where(['id'=>$store_goods['store_id']])->value('distributor_id'); $distributor = db('distributor')->where(['id'=>$distributor_id])->find(); $cost = $this->get_distributor_amount($mall_product, $distributor); $products[] = [ "id" => $store_goods['id'], "store_id" => $store_goods["store_id"], "shop_cat" => $store_goods["store_shop_cat"], "brand_id" => $mall_product["brand_id"], "brand" => $mall_product['brand'] ?? '', "goods_name" => $mall_product["goods_name"], "store_goods_name"=> $store_goods["store_goods_name"], "model" => $mall_product["model"], "materiel" => $mall_product["materiel"], "barcode" => $mall_product["barcode"], "jd_sku" => $mall_product["jd_sku"], "unit_text" => $mall_product["unit_text"] ?? '', "weight" => $mall_product["weight"], "color" => $mall_product["color"], "attr" => $mall_product["attr"], "image" => $mall_product["image"], "image_list" => $mall_product["image_list"] ?? $mall_product['images'] ?? '', "description" => $mall_product["description"], "cost" => $cost, "profit" => $store_goods['profit'], "shop_amount1" => $store_goods["shop_amount1"], "shop_amount2" => $store_goods["shop_amount2"], "shop_amount3" => $store_goods["shop_amount3"], "shop_amount4" => $store_goods["shop_amount4"], "shop_market" => $store_goods["shop_market"], "stock" => $mall_product["stock"], "product_id" => $store_goods["product_id"], "freight" => $mall_product["freight"], "status" => $store_goods["status"], "createtime" => $store_goods["createtime"], "updatetime" => $store_goods["updatetime"], "marketing" => $mall_product["marketing"], "markering" => $store_goods["markering"], "is_hot" => $mall_product["is_hot"], "is_top" => $mall_product["is_top"], "device_model" => $mall_product["device_model"] ?? '', "delivery_type" => $mall_product["delivery_type"] ?? '', "specs_num" => $mall_product["specs_num"], "specs_unit" => $mall_product["specs_unit"], "spuid" => isset($mall_product["spuid"]) ? $mall_product["spuid"] : 0, "is_enterprise" => $mall_product["is_enterprise"], // "is_cherry_pick" => $store_goods["is_cherry_pick"], "is_cherry_pick" => $mall_product["is_cherry_pick"], "shipping_time" => $mall_product["shipping_time"], "distributor_ids" => $mall_product["distributor_ids"], "moq" => $mall_product["moq"], "supplier_id" => $mall_product["supplier_id"], "is_special" => $store_goods["is_special"], "special_stock" => $store_goods["special_stock"], "sort" => $store_goods["sort"], // "is_sale" => $mall_product["is_sale"] ?? $mall_product['status'], "is_sale" => $store_goods["status"], 'sellable_platform' => $sellablePlatform ?? 0 ]; } return ElasticService::new()->insertAll("store_product",$products); } /** * * @param $id * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function store_product($id,$data = [],$batch = false) { $products = $store_product_list = []; if($batch){ $store_product_list = $data; }else{ $store_product = db('store_product')->where(['id'=>$id])->find(); if ( !$store_product) { return; } $store_product_list[] = $store_product; } foreach ($store_product_list as $store_product){ $brand = db("brands")->where(['id'=>$store_product['brand_id']])->value("title"); $unit_text = db('unit')->where(['id'=>$store_product["unit"]])->value('name'); $profit = $store_product["cost"] > 0 ? ($store_product["shop_market"] - $store_product["cost"]) / $store_product["cost"] * 100 : $store_product["shop_market"]; $product = [ "id" => $store_product['id'], "store_id" => $store_product["store_id"], "shop_cat" => $store_product["shop_cat"], "brand_id" => $store_product["brand_id"], "brand" => $brand, "goods_name" => $store_product["goods_name"], "store_goods_name"=> $store_product["goods_name"], "model" => $store_product["model"], "materiel" => $store_product["materiel"], "barcode" => $store_product["barcode"], "jd_sku" => $store_product["jd_sku"], "unit_text" => $unit_text, "weight" => $store_product["weight"], "color" => $store_product["color"], "attr" => $store_product["attr"], "image" => $store_product["image"], "image_list" => $store_product["images"], "description" => $store_product["description"], "cost" => $store_product["cost"], "profit" => $profit, "shop_amount1" => $store_product["shop_amount1"], "shop_amount2" => $store_product["shop_amount2"], "shop_amount3" => $store_product["shop_amount3"], "shop_amount4" => $store_product["shop_amount4"], "shop_market" => $store_product["shop_market"], "stock" => $store_product["stock"], "product_id" => $store_product["product_id"], "freight" => $store_product["freight"], "status" => $store_product["status"], "createtime" => $store_product["createtime"], "updatetime" => $store_product["updatetime"], "marketing" => $store_product["marketing"], "markering" => $store_product["markering"], "is_hot" => $store_product["is_hot"], "is_top" => $store_product["is_top"], "device_model" => "", "delivery_type" => $store_product["delivery_type"], "specs_num" => $store_product["specs_num"], "specs_unit" => $store_product["specs_unit"], "spuid" => 0, "is_enterprise" => $store_product["is_enterprise"], "is_cherry_pick" => $store_product["is_cherry_pick"], "shipping_time" => $store_product["shipping_time"], "distributor_ids" => $store_product["distributor_ids"], "moq" => $store_product["moq"], "supplier_id" => $store_product["supplier_id"], "is_special" => $store_product["is_special"], "special_stock" => $store_product["special_stock"], "sort" => $store_product["sort"], "is_huimin" => $store_product["is_huimin"], "huimin_marker" => $store_product["huimin_marker"], 'sellable_platform' => 0 ]; if ($store_product["product_id"] == 0) { if ( !$product["image"]) { if ($product["image_list"]) { $image_list = explode(',', $product["image_list"]); if ($image_list) { $product['image'] = $image_list[0]; } } } }else{ $mall_product = db('product')->where(['id'=>$store_product['product_id']])->find(); if ($mall_product) { if ($mall_product['spuid'] > 0) { $spuid_product = db('product')->where(['id'=>$mall_product['spuid']])->find(); $mall_product['image'] = $spuid_product['image']; $mall_product['image_list'] = $spuid_product['image_list']; } $product['image'] = $mall_product['image']; $product['image_list'] = $mall_product['image_list']; $product['device_model'] = $mall_product['device_model']; $product['specs_num'] = $mall_product['specs_num']; $product['spuid'] = $mall_product['spuid']; } } $products[] = $product; } // ElasticService::new('')->insert("store_product", $id, $product); return ElasticService::new()->insertAll("store_product",$products); } public function kaProduct($id, $data = []) { if(!empty($data)){ $store_goods_list = $data; }else{ $store_goods = db('ka_product')->where(['id'=>$id])->find(); if ( !$store_goods) { return; } $store_goods_list = [$store_goods]; } $products = []; foreach ($store_goods_list as &$store_goods){ $store_goods['store_id'] = 91000043; $store_goods['store_shop_cat'] = $store_goods['yc_shop_cat']; $store_goods['store_goods_name'] = $store_goods['goods_name']; $mall_product = &$store_goods; $cost = $mall_product['shop_amount1']; $store_goods['profit'] = '0.00'; $store_goods['status'] = $mall_product['is_sale']; $products[] = [ "id" => $store_goods['id'], "store_id" => $store_goods["store_id"], "shop_cat" => $store_goods["store_shop_cat"], "brand_id" => $mall_product["brand_id"], "brand" => $mall_product['brand'] ?? '', "goods_name" => $mall_product["goods_name"], "store_goods_name"=> $store_goods["store_goods_name"], "model" => $mall_product["model"], "materiel" => $mall_product["materiel"], "barcode" => $mall_product["barcode"], "jd_sku" => $mall_product["jd_sku"] ?? '', "unit_text" => $mall_product["unit_text"] ?? '', "weight" => $mall_product["weight"], "color" => $mall_product["color"], "attr" => $mall_product["attr"], "image" => $mall_product["image"], "image_list" => $mall_product["image_list"] ?? $mall_product['images'] ?? '', "description" => $mall_product["description"], "cost" => $cost, "profit" => $store_goods['profit'], "shop_amount1" => $store_goods["shop_amount1"], "shop_amount2" => $store_goods["shop_amount2"], "shop_amount3" => $store_goods["shop_amount3"], "shop_amount4" => $store_goods["shop_amount4"], "shop_market" => $store_goods["shop_market"], "stock" => $mall_product["stock"], "product_id" => $store_goods["id"], "freight" => $mall_product["freight"], "status" => $store_goods["status"], "createtime" => $store_goods["createtime"], "updatetime" => $store_goods["updatetime"], "marketing" => $mall_product["marketing"], "markering" => $store_goods["markering"], "is_hot" => $mall_product["is_hot"], "is_top" => $mall_product["is_top"], "device_model" => $mall_product["device_model"] ?? '', "delivery_type" => $mall_product["delivery_type"] ?? '', "specs_num" => $mall_product["specs_num"] ?? 0, "specs_unit" => $mall_product["specs_unit"] ?? '', "spuid" => isset($mall_product["spuid"]) ? $mall_product["spuid"] : 0, "is_enterprise" => $mall_product["is_enterprise"], // "is_cherry_pick" => $store_goods["is_cherry_pick"], "is_cherry_pick" => $mall_product["is_cherry_pick"], "shipping_time" => $mall_product["shipping_time"], "distributor_ids" => $mall_product["distributor_ids"] ?? '', "moq" => $mall_product["moq"], "supplier_id" => $mall_product["supplier_id"] ?? 0, "is_special" => $store_goods["is_special"], "special_stock" => $store_goods["special_stock"], "sort" => $store_goods["sort"], // "is_sale" => $mall_product["is_sale"] ?? $mall_product['status'], "is_sale" => $store_goods["status"], 'sellable_platform' => 0, ]; } return ElasticService::new()->insertAll("store_product",$products); } /** * 将 TP ORM 查询条件转换为 Elasticsearch 查询条件 * * @param array $map TP ORM 查询条件 * @return array Elasticsearch 查询条件 */ function convertTpToEsQuery(array $map): array { $esQuery = ['bool' => ['must' => []]]; foreach ($map as $field => $condition) { if (is_array($condition)) { // 处理数组条件,例如 ['in', 'field', [1, 2, 3]] $operator = strtolower($condition[0]); $value = $condition[1] ?? null; switch ($operator) { case 'in': $esQuery['bool']['must'][] = ['terms' => [$field => $value]]; break; case 'not in': $esQuery['bool']['must_not'][] = ['terms' => [$field => $value]]; break; case 'between': $esQuery['bool']['must'][] = ['range' => [$field => ['gte' => $value[0], 'lte' => $value[1]]]]; break; case 'not between': $esQuery['bool']['must_not'][] = ['range' => [$field => ['gte' => $value[0], 'lte' => $value[1]]]]; break; case 'like': $esQuery['bool']['must'][] = ['wildcard' => [$field => '*' . $value . '*']]; break; case 'not like': $esQuery['bool']['must_not'][] = ['wildcard' => [$field => '*' . $value . '*']]; break; case 'exp': // 处理复杂表达式 if (is_string($value)) { $esQuery['bool']['must'][] = ['script' => ['script' => ['source' => $value]]]; } break; case 'gt': case 'lt': $esQuery['bool']['must'][]['range'][$field] = [$operator => $value]; break; case 'egt': $esQuery['bool']['must'][]['range'][$field] = ['gte' => $value]; break; case 'elt': $esQuery['bool']['must'][]['range'][$field] = ['lte' => $value]; break; case 'neq': $esQuery['bool']['must_not'][]['term'][$field] = $value; } } else { // 处理简单条件,例如 ['field' => 'value'] $esQuery['bool']['must'][] = ['term' => [$field => $condition]]; } } return $esQuery; } }
滴水成冰,世间不存在毫无意义的付出,时间终会给你答案。