前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ElasticSearch笔记

ElasticSearch笔记

作者头像
OwenZhang
发布2021-12-08 16:39:45
3280
发布2021-12-08 16:39:45
举报
文章被收录于专栏:Owen's World

Elasticsearch 分布式搜索引擎

代码语言:javascript
复制
ElasticSearch的搜索
复制代码

learnku.com/docs/elasti…

代码语言:javascript
复制
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;
    }

}
复制代码

gitee.com/owenzhang24…

下载

www.elastic.co/guide/en/el…

启用

D:\elasticsearch-7.1.0>.\bin\elasticsearch.bat

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年10月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Elasticsearch 分布式搜索引擎
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档