es

package
v1.20.76 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

ESClient - golang es api 封装

配置项
[es]
ES_SEARCH_HOST = 127.0.0.1 es服务器地址
ES_SEARCH_PORT = 9200   es服务端口
ES_AUTH_USER = ""   es服务认证用户
ES_AUTH_SECRET = "" es服务认证密码
使用方式
创建实例
    // ctx 必须
    esClient := NewESClient(ctx).Use(indexName).Select(docType) // 当indexName和docType一样时,Select可以省略
向es提交新数据
    esClient.Push(id, data) // id为字符串, data必须是能够序列化的数据,比如data中如果存在decimal数据类型则会报错
更新数据
    esClient.Update(data, filters) // data为要更新的数据;filters为定位更新文档的查询条件
查询
    params := map[string]interface{}{
        "pageInfo": vanilla.ExtractPageInfoFromRequest(ctx),
        "sortAttrs": []string{"-id"},
        "rawAggs": "xxx", // 聚合查询参数的字符串形式,一般使用此参数作为聚合查询
        "aggs": ..., // elasticSDK中提供的聚合实现,高阶用法,使用此参数代表你熟悉elasticSDK中的相关实现
    }
    var records []*EsRecords // 注意,此处的EsRecords需要按照预期es查询返回的数据定义,详细参考es_test.go
    // BindRecords 方法将es返回的数据绑定到传入的参数,注意参数一定要是指针值
    esClient.Search(filters, params).BindRecords(&records) // filters为查询条件,params为其他辅助参数,比如分页、排序等
    nestPageInfo := esClient.GetPageResult() // 获取分页结果
聚合查询
    aggStr = `
        {
            "total_order_money": {
                "sum": {
                    "field": "final_money"
                }
            },
            "total_money": {
                "nested": {
                    "path": "transfers"
                },
                "aggs": {
                    "total_money": {
                        "sum": {
                            "field": "transfers.money"
                        }
                    },
                    "total_artist_money": {
                        "filter": {
                            "term": {
                                "transfers.user_type": "artist"
                            }
                        },
                        "aggs": {
                            "total_artist_money": {
                                "sum": {
                                    "field": "transfers.money"
                                }
                            }
                        }
                    },
                }
            }
        }
    `
    esClient.NoHits() // 如果只需要聚合查询的数据,则调用此方法后search的result即为空
    esClient.Search(filters, map[string]interface{}{
        "rawAggs": aggStr,
    })
    totalOrderMoney := esClient.GetSumAgg("total_order_money")
    totalMoney := esClient.GetNestedSumAgg("total_money", "total_money")
    totalArtistMoney := esClient.GetNestedSumAggWithFilter("total_money", "total_artist_money")

    // 此处只实现了sum聚合,更多的聚合查询可以自己实现,参考上述方法的源码
辅助方法
    // 为了支持更高的自由度,esClient暴露了几个elasticSDK的实现
    agg := esClient.GetAggregation() // 获取聚合查询数据
    searchResult := esClient.GetSearchResult() // 获取search查询的结果
    rawRecords := esClient.GetSearchRecords() // 获取查询结果集
注意es文档字段类型 keyword和text
  • text类型的英文用contains查询时,因为分词的原因,英文以空格为分届符,因此contains只可以查找到被空格分开的词或整个字符串;而中文可以任意查询
  • 不要用term查询text类型的field,只要term的value符合其中一个分词就会被搜索到,而不是预期的精确匹配
上述例子都可以在es_test.go中查看到

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ESClient

type ESClient struct {
	vanilla.ServiceBase
	// contains filtered or unexported fields
}

func NewESClient

func NewESClient(ctx context.Context) *ESClient

func (*ESClient) BindRecords

func (this *ESClient) BindRecords(container interface{})

BindRecords 将搜索记录绑定到一个包含struct的slice中 container一定是某个slice的地址,如:

var orders []*Order
es.BindRecords(&orders)

func (*ESClient) GetAggregation

func (this *ESClient) GetAggregation() elastic.Aggregations

func (*ESClient) GetNestedSumAgg

func (this *ESClient) GetNestedSumAgg(nestedName, aggName string) float64

GetNestedSumAgg 嵌套聚合查询

func (*ESClient) GetNestedSumAggWithFilter

func (this *ESClient) GetNestedSumAggWithFilter(nestedName, aggName string) float64

GetNestedSumAggWithFilter 带filter的嵌套聚合查询

func (*ESClient) GetPageResult

func (this *ESClient) GetPageResult() vanilla.INextPageInfo

func (*ESClient) GetSearchRecords

func (this *ESClient) GetSearchRecords() []*elastic.SearchHit

func (*ESClient) GetSearchResult

func (this *ESClient) GetSearchResult() *elastic.SearchResult

func (*ESClient) GetSumAgg

func (this *ESClient) GetSumAgg(name string) float64

GetSumAgg 聚合查询

func (*ESClient) GetSumAggWithFilter

func (this *ESClient) GetSumAggWithFilter(aggName string) float64

GetSumAggWithFilter 带 filter 的聚合

func (*ESClient) NoHits

func (this *ESClient) NoHits() *ESClient

func (*ESClient) Push

func (this *ESClient) Push(id string, data interface{})

func (*ESClient) Search

func (this *ESClient) Search(filters map[string]interface{}, args ...map[string]interface{}) *ESClient

Search 查询 args: [pageInfo, sortAttrs, rawAggs, aggs]

func (*ESClient) Select

func (this *ESClient) Select(docType string) *ESClient

func (*ESClient) Update

func (this *ESClient) Update(data map[string]interface{}, filters map[string]interface{}) error

func (*ESClient) Use

func (this *ESClient) Use(indexName string) *ESClient

func (*ESClient) UseSyncUpdate

func (this *ESClient) UseSyncUpdate(b bool) *ESClient

type NamedAggregation

type NamedAggregation struct {
	// contains filtered or unexported fields
}

NamedAggregation 供外部使用,可自由组合elastic提供的各类聚合

func NewNamedAggregation

func NewNamedAggregation(name string, aggregation elastic.Aggregation) *NamedAggregation

func (*NamedAggregation) GetAggregation

func (this *NamedAggregation) GetAggregation() elastic.Aggregation

func (*NamedAggregation) GetName

func (this *NamedAggregation) GetName() string

type Query

type Query struct {
	// contains filtered or unexported fields
}

func (*Query) RemoveKey

func (this *Query) RemoveKey(key string)

func (*Query) SetData

func (this *Query) SetData(data map[string]interface{})

func (*Query) Source

func (this *Query) Source() (interface{}, error)

type QueryParser

type QueryParser struct {
	vanilla.ServiceBase
	// contains filtered or unexported fields
}

将rest filter语法转换成es query语法、sort语法

func NewQueryParser

func NewQueryParser() *QueryParser

func (*QueryParser) Parse

func (this *QueryParser) Parse(filters map[string]interface{}) *Query

* 处理 [range, icontains, gte, lte, range, notin, not, in, gt, lt]

形如:{
	'bid': '1242464682341',
	'createdAt__range': ['2018-07-08 12:12:22', '2018-07-09 13:12:22'],
	'finalMoney__gte': 50,
	'corp_name__contains': u'小',
	'status__in': [1, 3],
} ===>>>

{
	'bool': {
		'must': [{
			'term': {
				'bid': '1242464682341'
			}
		}, {
			'range': {
				'created_at': {
					'gte': '2018-07-08 12:12:22',
					'lte': '2018-07-09 13:12:22'
				}
			}
		}, {
			'range': {
				'final_money': {
					'gte': 50
				}
			}
		}, {
			'match': {
				'corp.name': u'小'
			}
		}, {
			'terms': {
				'status': [1, 3]
			}
		}]
	}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL