1 Star 0 Fork 0

water/goweb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
page_es_request.go 12.68 KB
一键复制 编辑 原始数据 按行查看 历史
leijmdas 提交于 2个月前 . add
package pagees
import (
"context"
"gitee.com/leijmdas/goweb/goes/esconst"
"gitee.com/leijmdas/goweb/goes/esdto"
"gitee.com/leijmdas/goweb/gopage/base"
"gitee.com/leijmdas/goweb/gopage/dto"
"gitee.com/leijmdas/goweb/gopage/page"
"gitee.com/leijmdas/gobase/goconfig/common/base/baseconfig"
"gitee.com/leijmdas/gobase/goconfig/common/base/basedto"
"gitee.com/leijmdas/gobase/goconfig/common/base/jsonutils"
"github.com/olivere/elastic/v7"
"github.com/sirupsen/logrus"
"strings"
)
/*
@Title 文件名称: pageesrequest.go
@Description 描述: es请求消息
@Author 作者: leijianming@163.com 时间(2024-02-22 22:38:21)
@Update 作者: leijianming@163.com 时间(2024-02-21 22:38:21)
*/
/*
索是 ES 最为复杂精妙的地方,这里只示例项目中较为常用的查询。
ES 中的查询分为三大类,
一是 Term-level queries(我翻译成字段匹配),
二是 Full-text queries(全文搜索)
,三是不常用的 Specialized queries(专门查询)。各自可细分为如下几种:
Term-level queries
exists query 字段是否存在值
fuzzy query 模糊查询
ids query ID 查询
prefix query 前缀查询
range query 范围查询
regexp query 正则查询
term query 精确匹配单个字段
terms query 精确匹配单个字段,但使用多值进行匹配,类似于 SQL 中的 in 操作
terms_set query 字段集合查询。文档需包含字段集合中指定的最少数量字段
wildcard query 通配符查询
Full-text queries
match query 单字段搜索(匹配分词结果,不需要全文匹配)
Specialized queries
script query 脚本查询
原文链接:https://blog.csdn.net/K346K346/article/details/120906440
*/
type PageEsRequest struct {
basedto.BaseEntity
*page.PageRequest
*esdto.EsRequest
AttachResp bool `json:"-"`
PageEsRequests []*PageEsRequest
PageEsBoolTypes []int
MustQuery []elastic.Query `json:"-"`
MustnotQuery []elastic.Query `json:"-"`
ShouldQuery []elastic.Query `json:"-"`
FilterQuery []elastic.Query `json:"-"`
}
func Default() *PageEsRequest {
var req = &PageEsRequest{
PageEsRequests: make([]*PageEsRequest, 0),
PageEsBoolTypes: make([]int, 0),
PageRequest: page.Default(),
MustQuery: make([]elastic.Query, 0),
MustnotQuery: make([]elastic.Query, 0),
ShouldQuery: make([]elastic.Query, 0),
FilterQuery: make([]elastic.Query, 0),
}
req.EsRequest = esdto.NewEsRequest()
req.EsBoolType = base.EsTypeFilter
req.InitPage()
req.InitProxy(req)
return req
}
func NewEsRequest(pageSize, current int) *PageEsRequest {
var esRequest = Default()
esRequest.PageCurrent = current
esRequest.PageSize = pageSize
return esRequest
}
func NewPageEsRequest(indexName string) *PageEsRequest {
var esRequest = Default()
esRequest.IndexName = indexName
return esRequest
}
func (this *PageEsRequest) Nest(subs ...*PageEsRequest) *PageEsRequest {
for _, _ = range subs {
this.PageEsBoolTypes = append(this.PageEsBoolTypes, this.EsBoolType)
}
this.PageEsRequests = append(this.PageEsRequests, subs...)
return this
}
func (this *PageEsRequest) Clear() {
this.PageRequest.Clear()
this.PageEsRequests = make([]*PageEsRequest, 0)
}
func (this *PageEsRequest) Default() {
var clientDto = baseconfig.NewElasticClientDto()
clientDto.URL = "http://192.168.14.58:9200"
clientDto.Username = "elastic"
clientDto.Password = "123456"
this.Ini(clientDto)
}
func (this *PageEsRequest) AddGroup(field *dto.QueryField, query elastic.Query) elastic.Query {
switch field.EsBoolType {
case base.EsTypeMust:
this.MustQuery = append(this.MustQuery, query)
case base.EsTypeMustNot:
this.MustnotQuery = append(this.MustnotQuery, query)
case base.EsTypeFilter:
this.FilterQuery = append(this.FilterQuery, query)
case base.EsTypeShould:
this.ShouldQuery = append(this.ShouldQuery, query)
default:
this.FilterQuery = append(this.FilterQuery, query)
}
return query
}
func (this *PageEsRequest) ValueOfPageRequest(that *PageEsRequest) *PageEsRequest {
this.PageRequest = that.PageRequest
this.IndexName = that.IndexName
//this.AggFields = that.AggFields
this.PageRequest.InitProxy(this.PageRequest)
return this
}
func (this *PageEsRequest) Ini(clientDto *baseconfig.ElasticClientDto) {
this.EsClient.ClientDto = clientDto
}
func (self *PageEsRequest) IfIndexMulti() bool {
return strings.Contains(self.IndexName, ",")
}
func (this *PageEsRequest) Open() {
if this.EsClient.ClientDto == nil {
this.Default()
}
this.EsClient.Open()
}
func (this *PageEsRequest) esOrderBy(searchService *elastic.SearchService) *elastic.SearchService {
for _, v := range this.OrderBys {
searchService.Sort(v.Field, v.Sort == "asc")
}
return searchService
}
func (self *PageEsRequest) BuildBoolQuery() *elastic.BoolQuery {
return NewPageEsQuery(self).BuildBoolQuery(self)
}
func (this *PageEsRequest) EsQueryResult() (*PageEsResult, error) {
var result = NewIchubPageEsResult()
searchResult, err := this.EsBoolQuery()
if err != nil {
logrus.Error(err)
return result.FailMsg(err.Error()), err
}
result.PageEsResultOf(this, searchResult)
if !this.AttachResp {
result.SearchResult = nil
}
this.Save2File(this, result)
return result, err
}
func (this *PageEsRequest) EsBoolQueryResult() (*PageEsResult, error) {
var result = NewIchubPageEsResult()
searchResult, err := this.EsBoolQuery()
if err != nil {
logrus.Error(err)
return result.FailMsg(err.Error()), err
}
result.PageEsResultOf(this, searchResult)
if !this.AttachResp {
result.SearchResult = nil
}
this.Save2File(this, result)
return result, err
}
func (this *PageEsRequest) EsId(opValue interface{}) *PageEsRequest {
return this.EsIds([]interface{}{opValue})
}
func (this *PageEsRequest) EsIds(opValues []interface{}) *PageEsRequest {
this.PageRequest.QueryFields("ids", base.EsIds, opValues)
return this
}
func (this *PageEsRequest) EsRegexp(field string, opValue interface{}) *PageEsRequest {
this.PageRequest.QueryFields(field, base.EsRegexp, []interface{}{opValue})
return this
}
func (this *PageEsRequest) EsFuzzy(field string, opValue interface{}) *PageEsRequest {
this.PageRequest.QueryFields(field, base.EsFuzzy, []interface{}{opValue})
return this
}
func (this *PageEsRequest) EsTerm(field string, opValue any) *PageEsRequest {
this.PageRequest.QueryFields(field, base.EsTerm, []any{opValue})
return this
}
func (this *PageEsRequest) EsMatch(field string, opValue interface{}) *PageEsRequest {
this.PageRequest.QueryFields(field, base.EsMatch, []interface{}{opValue})
return this
}
func (this *PageEsRequest) EsMatchPhrase(field string, opValue interface{}) *PageEsRequest {
this.PageRequest.QueryFields(field, base.EsMatchPhrase, []interface{}{opValue})
return this
}
func (this *PageEsRequest) EsRange(field string, opValue ...interface{}) *PageEsRequest {
this.PageRequest.QueryFields(field, base.EsRange, opValue)
return this
}
func (this *PageEsRequest) EsMatchAll() *PageEsRequest {
this.PageRequest.QueryFields("MatchAll", base.EsMatchAll, []interface{}{})
return this
}
func (this *PageEsRequest) EsTerms(field string, opValues ...interface{}) *PageEsRequest {
this.PageRequest.QueryFields(field, base.EsTerms, opValues)
return this
}
func (this *PageEsRequest) EsWildcard(field string, opValues ...interface{}) *PageEsRequest {
this.PageRequest.QueryFields(field, base.EsWildcard, opValues)
return this
}
// 排序字段是keyword
func (this *PageEsRequest) EsCount() (int64, error) {
logrus.Info(this.ToPrettyString())
countService := this.Client().Count(this.IndexName)
var query = NewPageEsQuery(this).BuildQuery()
// this.esOrderBy(countService)
if query != nil {
countService.Query(query)
}
count, err := countService.Do(context.Background())
if err != nil {
logrus.Error(err)
}
logrus.Info(count)
return count, err
}
func (this *PageEsRequest) BuildSource(service *elastic.SearchService) {
if len(this.Source) > 0 {
var fsc = elastic.NewFetchSourceContext(true).Include(strings.Split(this.Source, ",")...)
service.FetchSourceContext(fsc)
}
}
func (this *PageEsRequest) EsBoolQuery() (*elastic.SearchResult, error) {
if len(this.IndexName) == 0 {
this.IndexName = "empty"
}
logrus.Info(this.ToPrettyString())
searchService := this.Client().Search().Index(this.IndexName). // 设置索引名 设置查询条件 设置排序字段,根据Created字段升序排序,第二个参数false表示逆序
From(this.Start()).Size(this.Limit()).Pretty(true) // 查询结果返回可读性较好的JSON格式
var query = NewPageEsQuery(this).BuildBoolQuery(this)
if query != nil {
searchService.Query(query)
}
this.esOrderBy(searchService)
this.BuildSource(searchService)
searchResult, err := searchService.Do(context.Background())
if err != nil {
logrus.Error(err)
}
logrus.Info(jsonutils.ToJsonPretty(searchResult))
return searchResult, err
}
func (this *PageEsRequest) EsQuery() (*elastic.SearchResult, error) {
if len(this.IndexName) == 0 {
this.IndexName = "empty"
}
logrus.Info(this.ToPrettyString())
searchService := this.Client().Search().Index(this.IndexName). // 设置索引名 设置查询条件 设置排序字段,根据Created字段升序排序,第二个参数false表示逆序
From(this.Start()).Size(this.Limit()).Pretty(true) // 查询结果返回可读性较好的JSON格式
var query = NewPageEsQuery(this).BuildQuery()
if query != nil {
searchService.Query(query)
}
this.esOrderBy(searchService)
this.BuildSource(searchService)
searchResult, err := searchService.Do(context.Background())
if err != nil {
logrus.Error(err)
}
logrus.Info(jsonutils.ToJsonPretty(searchResult))
return searchResult, err
}
func (this *PageEsRequest) EsFindId(id string) (*elastic.GetResult, error) {
result, err := this.EsClient.Client().Get().
Index(this.IndexName).Id(id).
Do(context.Background())
if err != nil {
logrus.Error(err)
return nil, err
}
logrus.Info(jsonutils.ToJsonPretty(result))
return result, err
}
func (this *PageEsRequest) EsFindIds(ids ...string) (*elastic.SearchResult, error) {
query := elastic.NewIdsQuery().Ids(ids...)
result, err := this.Client().Search().
Index(this.IndexName).Query(query).
Do(context.Background())
if err != nil {
logrus.Error(err)
return nil, err
}
logrus.Info(jsonutils.ToJsonPretty(result))
return result, err
}
func (this *PageEsRequest) EsFindTerms(key string, values ...interface{}) (*elastic.SearchResult, error) {
query := elastic.NewTermsQuery(key, values...)
result, err := this.EsClient.Client().Search().
Index(this.IndexName).Query(query).From(this.Start()).Size(this.Limit()).
Do(context.Background())
if err != nil {
logrus.Error(err)
return nil, err
}
logrus.Info(jsonutils.ToJsonPretty(result))
return result, err
}
func (this *PageEsRequest) field2Keyword(field string) string {
return field + ".keyword"
}
func (this *PageEsRequest) EsFindTerm(field string, value interface{}) (*elastic.SearchResult, error) {
query := elastic.NewTermQuery(field, value)
result, err := this.EsClient.Client().Search().
Index(this.IndexName).Query(query).From(this.Start()).Size(this.Limit()).
Do(context.Background())
if err != nil {
logrus.Error(err)
return nil, err
}
logrus.Info(jsonutils.ToJsonPretty(result))
return result, err
}
func (this *PageEsRequest) EsGetMapping(indexName string) (map[string]interface{}, error) {
return this.Client().GetMapping().Index(indexName).Type().Do(context.Background())
}
func (this *PageEsRequest) IfMust() bool {
return this.CmdType == esconst.Must
}
func (this *PageEsRequest) IfShould() bool {
return this.CmdType == esconst.Should
}
func (this *PageEsRequest) IfMustNot() bool {
return this.CmdType == esconst.MustNot
}
func (this *PageEsRequest) IfFilter() bool {
return this.CmdType == esconst.Filter
}
func (this *PageEsRequest) EsMust() *PageEsRequest {
this.EsBoolType = base.EsTypeMust
this.CmdType = esconst.CMDTYPE(this.EsBoolType)
return this
}
func (this *PageEsRequest) EsShould() *PageEsRequest {
this.EsBoolType = base.EsTypeShould
this.CmdType = esconst.CMDTYPE(this.EsBoolType)
return this
}
func (this *PageEsRequest) EsMustNot() *PageEsRequest {
this.EsBoolType = base.EsTypeMustNot
this.CmdType = esconst.CMDTYPE(this.EsBoolType)
return this
}
func (this *PageEsRequest) EsFilter() *PageEsRequest {
this.EsBoolType = base.EsTypeFilter
this.CmdType = esconst.CMDTYPE(this.EsBoolType)
return this
}
func (self *PageEsRequest) SetPageSize(size int) *PageEsRequest {
self.PageSize = size
return self
}
func (self *PageEsRequest) SetPageCurrent(current int) *PageEsRequest {
self.PageCurrent = current
return self
}
func (self *PageEsRequest) SortField(field string, asc bool) elastic.Sorter {
if asc {
return elastic.NewFieldSort(field).Asc()
}
return elastic.NewFieldSort(field).Desc()
}
func (self *PageEsRequest) SetIndexName ( name string){
self.IndexName=name
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/leijmdas/goweb.git
git@gitee.com:leijmdas/goweb.git
leijmdas
goweb
goweb
v1.2.9

搜索帮助