🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

github.com/KingSolvewer/elasticsearch-query-builder

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/KingSolvewer/elasticsearch-query-builder

v0.0.0-20241202014833-5fc88b24e7d7
Source
Go
Version published
Created
Source

使用go语言构建DSL查询语句

特点

  • 支持链式调用
  • 学习简单,使用方便
  • 支持ES常用查询和聚合语法
  • 支持原生dsl语句查询

要求

  • 熟悉go语言基础知识,熟悉es查询语句
  • 简单校验,不符合规范的参数,会进行过滤和跳过组合。使用时,请严格按照es语法输入参数
  • go 运行版本 1.18及以上

使用

go get github.com/KingSolvewer/elasticsearch-query-builder

默认全局初始化Builder,如果要构建新的查询语句,请重置:

elastic.Where...

...

// 重置后继续使用
elastic.Reset()
elastic.Where...

手动初始化Builder,,如果要构建新的查询语句,请重置:

builer := elastic.NewBuilder()
builer.Where...

builer.Reset().Where

Where,对应must, OrWhere,对应should, WhereNot,对应must_not, Filter,对应filter 都是并列关系。构建后,在bool查询条件下同级。

import (
    elastic "github.com/KingSolvewer/elasticsearch-query-builder"
)

elastic.Where("status", 1).Where("title", "中国").OrWhere("status", 1).WhereNot("country", "日本").Filter("city", "合肥")


DSL:
{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "status": 1
                    }
                },
                {
                "term": {
                        "title": "中国"
                    }
                }
            ],
            "must_not": [
                {
                    "term": {
                        "country": "日本"
                    }
                }
            ],
            "should": [
                {
                    "term": {
                        "status": 1
                    }
                }
            ],
            "filter": [
                {
                    "term": {
                        "city": "合肥"
                    }
                }
            ]
        }
    }
}

使用闭包的方式嵌套子句,例如:must 语句中嵌套 should 语句,可以使用,WhereNested。同理还有WhereNotNested,OrWhereNested,FilterNested

import (
    elastic "github.com/KingSolvewer/elasticsearch-query-builder"
)

elastic.Where("title", "中国").WhereNested(func(b *elastic.Builder) *elastic.Builder {
    return b.OrWhere("title", "美国").OrWhere("title", "日本")
})

DSL:
{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "title": "中国"
                    }
                },
                {
                "bool": {
                     "should": [
                           {
                                "term": {
                                    "title": "美国"
                                }
                            },
                            {
                                "term": {
                                    "title": "日本"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

方法

Select(fields ...string)
    elastic.Select("title", "content")
AppendField(fields ...string)
    elastic.AppendField("post_time", "author")
From(value uint)
    elastic.From(0)
Size(value uint)
    elastic.Size(0)
OrderBy(field string, orderType esearch.OrderType)
	esearch.Asc
    esearch.Desc

    elastic.OrderBy("post_time", esearch.Desc)
Raw(raw string)
    elastic.Raw(`{"query":{"bool":{"must":[{"term":{"news_uuid":"*********"}}]}}}`)
Where(field string, value any)
    elastic.Where("post_time", "2024-11-01 00:00:23")
WherePrefix(field string, value any)
    elastic.WherePrefix("username", "ki")
WhereExists(field string)
    elastic.WhereExists("username")
WhereRegexp(field string, value string, fn termlevel.RegexpParamFunc)
    elastic.WhereRegexp("username", "ki.*y", nil)
    elastic.WhereRegexp("username", "ki.*y", func() termlevel.RegexpParam {
        return termlevel.RegexpParam{}
    })
WhereWildcard(field string, value string, fn termlevel.WildcardParamFunc)
    elastic.WhereWildcard("username", "ki*y", nil)
    elastic.WhereWildcard("username", "ki*y", func() termlevel.WildcardParam {
        return termlevel.WildcardParam{}
    })
WhereIn(field string, value []any)
    any 只允许 go的基础类型 int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64, string, bool
    
    elastic.WhereIn("news_emotion", elastic.SliceToAny([]string{"中性", ""})
WhereBetween(field string, value1, value2 any)
    any 只允许 go的基础类型 int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64, string, bool
    
    elastic.WhereBetween("publish_time", "2024-11-01 00:00:00", "2024-12-01 00:00:00")
WhereRange(field string, value any, rangeType esearch.RangeType)
    any 只允许 go的基础类型 int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64, string, bool
    esearch.Gte
    esearch.Gt
    esearch.Lt
    esearch.Lte    

    elastic.WhereRange("age", 18, esearch.Gt)
WhereMatch(field string, value string, matchType esearch.MatchType, fn fulltext.AppendParamsFunc)
    esearch.BestFields
    esearch.MostFields
    esearch.CrossFields
    esearch.Phrase
    esearch.PhrasePrefix
    esearch.BoolPrefix

    elastic.WhereMatch("title", "中国电信", esearch.MatchPhrasePrefix, nil)
    elastic.WhereMatch("title", "中国电信", esearch.MatchPhrasePrefix, func() fulltext.AppendParams {
        return fulltext.AppendParams{}
    })
WhereMultiMatch(field []string, value string, fieldType esearch.FieldType, fn fulltext.AppendParamsFunc)
    esearch.Match,esearch.MatchPhrase,esearch.MatchPhrasePrefix

    elastic.WhereMultiMatch([]string{"title", "content"}, "中国电信", esearch.BestFields, nil)
    elastic.WhereMultiMatch([]string{"title", "content"}, "中国电信", esearch.BestFields, func() fulltext.AppendParams {
        return fulltext.AppendParams{}
    })
WhereNested(fn NestWhereFunc)
    elastic.WhereNested(func(b *elastic.Builder) {
        b.Where().WhereIn().OrWhere().WhereNot().Filter()...
    })

WhereNot,OrWhere,Filter 都有以上对应的方法,使用方式相同

其他方法

Scroll(scroll string) 使用游标查询
    elastic.Scroll("2m")
GetScroll() string
    elastic.GetScroll()
ScrollId(scrollId string) 设置游标值
    elastic.ScrollId("**************")
GetScrollId() string
    elastic.GetScrollId()
Collapse(field string) 去重
    elastic.Collapse("simhash")
GetCollapse()
    elastic.GetScrollId()
Dsl() string 获取DSL语句
    elastic.Dsl()
Marshal() (string, error) 获取DSL语句和构建语句时的错误
    elastic.Marshal()
GetQuery() *esearch.ElasticQuery 获取构建DSL语句的结构体实例
    elastic.GetQuery()
Clone() *Builder 复制Builder实例的字段和方法,生成一个新的Builder实例
    elastic.Clone()
Reset() *Builder 重置同一个Builder实例,重复使用
    elastic.Reset()

常用 Bucket Aggregations

名称ES语法方法参数说明
分组聚合termselastic.GroupBy()aggs.TermsParam{}对某个字段进行分组聚合
直方图聚合histogramelastic.Histogram()aggs.HistogramParam{}
日期直方图聚合date_histogramelastic.DateGroupBy()aggs.HistogramParam{}严格遵照日期直方图聚合的写法
数值范围聚合rangeelastic.Range()aggs.RangeParam{}必须是数值类型的数据
日期范围聚合date_rangeelastic.DateRange()aggs.RangeParam{}严格遵照日期范围聚合的写法

常用 Metrics Aggregations

名称ES语法方法参数说明
平均数计算avgelastic.Avg()aggs.MetricParam
最大值计算maxelastic.Mx()aggs.MetricParam
最小值计算minelastic.Min()aggs.MetricParam
总和计算sumelastic.Sum()aggs.MetricParam
数量计算value_countelastic.ValueCount()类似于 total
统计statselastic.Stats()aggs.MetricParam
扩展统计extended_statselastic.ExtendedStats()aggs.CardinalityParam
分组聚合的数据top_hitselastic.TopHits()aggs.TopHitsParam
分组聚合的数据top_hitselastic.TopHitsFunc()闭包函数支持 b.From(0).Size(10).Select().Sort()

函数

elastic.SliceToAny[T SliceInterface](sets []T) 将满足约束的任意类型转换成any类型
elastic.SliceToAny([]string{"a", "b", "c"})

FAQs

Package last updated on 02 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts