Elasticsearch 分布式搜索引擎
ElasticSearch的搜索
GET /es_saas_user_log_alias/_search { "query": { "match_all": {} } } GET /es_saas_user_log_alias/_search { "query": { "bool": { "must": [ { "term": { "user_id": 35 } }, { "term": { "behavior_type": 2 } } ], "must_not": [ { "term": { "item_type": { "value": 4 } } } ], "filter": [ { "range": { "created_at": { "gte": 1629781809, "lte": 1632460203 } } } ] } }, "size": 5, "from": 0, "sort": [ { "created_at": { "order": "desc" } } ] } $query = [ 'query' => [ 'bool' => [ 'must' => [ [ 'term' => [ 'user_id' => (int)$id, ], ], [ 'term' => [ 'behavior_type' => 2,//浏览,文章方案 ], ], ], 'must_not' => [ 'term' => [ 'item_type' => 4,//不是公众号 ], ], 'filter' => [ 'range' => [ 'created_at' => [ 'gte' => time() - 30 * 86400,//近一个月 'lte' => time() + 86400, ] ], ], ] ], 'sort' => [ 'created_at' => [ 'order' => 'desc' ] ] ]; //es查询 $query = [ //时间倒序 order 'sort' => [ 'created_at' => [ 'order' => 'desc' ] ], //每个用户只显示一条 'collapse' => [ 'field' => 'user_id' ], ]; //只显示触达 where1 $query['query']['bool']['must'][] = [ 'term' => [ 'behavior_type' => 5, ] ]; //只显示指定工作室 where2 $query['query']['bool']['must'][] = [ 'term' => [ 'store_studio_id' => (int)$storeStudioId, ] ]; //不记录游客信息 wherenot $query['query']['bool']['must_not'][] = [ 'term' => [ 'user_id' => 0, ] ]; //用户数组存在wherein array if ($userIdArr) { $query['query']['bool']['filter'][] = [ 'terms' => [ 'user_id' => $userIdArr, ] ]; } //创建时间 wherebetween if (!empty($createdAt)) { if (!empty($createdAt[0]) && !empty($createdAt[1])) { $query['query']['bool']['filter'][] = [ 'range' => [ 'created_at' => [ 'gte' => strtotime($createdAt[0]), 'lte' => strtotime($createdAt[1]), ] ] ]; } } $params = [ 'index' => 'es_saas_user_log_alias', '_source' => ['user_id', 'store_studio_id', 'behavior_type', 'item_type', 'item_id', 'created_at', 'source_type', 'touch_type', 'item_type', 'item_id'], 'body' => $query, 'from' => ($page - 1) * $pageSize, 'size' => $pageSize ]; $data = ElasticsearchFactory::client()->search($params); ///// "hyperf/elasticsearch": "~2.0.0", ElasticsearchFactory.php <?php /** * Created by PhpStorm. * Created by lkz at 2021/9/14 17:16 */ namespace App\Common; use App\Constants\ErrorCode; use App\Exception\BusinessException; use Elasticsearch\ClientBuilder; use Hyperf\Guzzle\RingPHP\PoolHandler; use Swoole\Coroutine; class ElasticsearchFactory { /** * elasticsearch用户日志记录表别名 */ public const ES_USER_LOG_ALIAS = 'es_saas_user_log_alias'; /** * Describe:获取elasticsearch连接客户端 * @return \Elasticsearch\Client * Created by lkz at 2021/09/17 09:26 */ static public function client() { $builder = ClientBuilder::create(); if (Coroutine::getCid() > 0) { $handler = make(PoolHandler::class, [ 'option' => [ 'max_connections' => 50, ], ]); $builder->setHandler($handler); } $client = $builder->setHosts([env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200')])->build(); return $client; } /** * Describe:批量添加数据 * @param string $index 索引名称 * @param string $body 数据 * @return mixed * Created by xuqy at 2021/09/17 09:26 */ public static function bulk($index, $list) { if (empty($index) || empty($list)) { throw new BusinessException(ErrorCode::BUSINESS_ERROR, '参数都不能为空'); } foreach ($list as $value) { $params['body'][] = [ 'index' => [ '_index' => $index, ] ]; $params['body'][] = $value; } $result = self::client()->bulk($params); return $result; } }
下载
启用
D:\elasticsearch-7.1.0>.\bin\elasticsearch.bat