ElasticSearch笔记

ElasticSearch笔记,第1张

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

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

    
    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

下载

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

启用

D:elasticsearch-7.1.0>.binelasticsearch.bat

Buy me a cup of coffee :)

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5690493.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存