ElasticSearch笔记

Elasticsearch 分布式搜索引擎

ElasticSearch的搜索

https://learnku.com/docs/elasticsearch73/7.3

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;
    }

}

https://gitee.com/owenzhang24/elasticsearch_note

Elasticsearch 核心知识图谱

下载

https://www.elastic.co/guide/en/elasticsearch/reference/7.1/install-elasticsearch.html

启用

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

Buy me a cup of coffee :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值