Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SearchSlice7 ¶
func SearchSlice7[T any](r *es7.SearchResult, element T) (slice []T, err error)
Types ¶
type A ¶
type A []any
A Array.(abbr) JSON Array 类型别名 多用于Search().Source(es.M{"...": es.A{"",""})目的是为了提高代码可读性.
type DecodeSearchResultError ¶
type DecodeSearchResultError struct { SearchHit []byte Element any DecodeFailMessage string DecodeError error }
func AsDecodeSearchResultError ¶
func AsDecodeSearchResultError(err error) (decodeSearchResultError *DecodeSearchResultError, as bool)
func (*DecodeSearchResultError) Error ¶
func (e *DecodeSearchResultError) Error() string
func (*DecodeSearchResultError) Unwrap ¶
func (e *DecodeSearchResultError) Unwrap() error
type Example ¶
func (Example) Index ¶
func (Example) Index()
Index see Example
Example ¶
ExampleExample_Index 索引文档
package main import ( "context" es "github.com/goclub/es" xjson "github.com/goclub/json" ) var example es.Example func main() { /* POST /test/_doc { "name": "nimo", "age" : 18 } */ var err error ctx := context.Background() user := struct { Name string `json:"name"` Age uint8 `json:"age"` }{Name: "nimo", Age: 18} indexResp, err := example.Client.Index(). Index("test"). BodyJson(user). Do(ctx) if err != nil { return } xjson.Print("indexResp", indexResp) return }
Output:
func (Example) NewClient ¶
func (Example) NewClient()
Example ¶
/* 请提前准备数据 PUT /bank { "mappings": { "properties": { "address": {"type": "text"} } } } https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json" */ client, err := es7.NewClient( es7.SetURL(ExampleConnectURL), es7.SetSniff(false), es7.SetHealthcheck(false), ) if err != nil { xerr.PrintStack(err) } example.Client = client
Output:
func (Example) Search ¶
func (Example) Search()
Search see Example
Example ¶
ExampleExample_Search 搜索全部数据
/* GET /bank/_search { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ], "size": 10 } */ var err error /* only test use */ defer func() { if err != nil { xerr.PrintStack(err) } }() ctx := context.Background() searchResult, err := example.Client.Search(). Index("bank"). Sort("account_number", true). Size(3). Do(ctx) if err != nil { return } xjson.Print("searchResult", searchResult) xjson.Print("TotalHits", searchResult.TotalHits()) var itemType ExampleAccount var eachAccountList []ExampleAccount log.Print("------------------") log.Print("使用 Each 遍历,json解析失败的会赋空值") for _, item := range searchResult.Each(reflect.TypeOf(itemType)) { if account, ok := item.(ExampleAccount); ok { eachAccountList = append(eachAccountList, account) } } xjson.Print("eachAccountList", eachAccountList) log.Print("------------------") log.Print("使用 es.SearchSlice7 获取 slice, json 解释失败会返回错误( goclub/json )") slice7AccountList, err := es.SearchSlice7(searchResult, ExampleAccount{}) if err != nil { if decodeErr, as := es.AsDecodeSearchResultError(err); as { log.Print(string(decodeErr.SearchHit)) log.Printf("%+#v", decodeErr.Element) } return } xjson.Print("slice7AccountList", slice7AccountList) return
Output:
func (Example) SearchAggsAggs ¶
func (Example) SearchAggsAggs()
Example ¶
ExampleExample_SearchAggsAggs 搜索-合并聚合
/* GET /bank/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state" }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } } */ var err error /* only test use */ defer func() { if err != nil { xerr.PrintStack(err) } }() ctx := context.Background() result, err := example.Client.Search().Source(es.M{ "size": 0, "aggs": es.M{ "group_by_state": es.M{ "terms": es.M{ "field": "state", }, "aggs": es.M{ "average_balance": es.M{ "avg": es.M{ "field": "balance", }, }, }, }, }, }).Do(ctx) if err != nil { return } xjson.PrintIndent("AggsAggs", result.Aggregations["group_by_state"]) return
Output:
func (Example) SearchBool ¶
func (Example) SearchBool()
SearchBool see example
Example ¶
ExampleExample_SearchBool 搜索 bool 条件
/* GET /bank/_search { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } } */ var err error /* only test use */ defer func() { if err != nil { xerr.PrintStack(err) } }() ctx := context.Background() searchResult, err := example.Client.Search(). Index("bank"). Query(es7.NewBoolQuery().Must( es7.NewMatchQuery("age", "40"), ).MustNot( es7.NewMatchQuery("state", "ID"), ), ). Do(ctx) if err != nil { return } xjson.Print("searchResult.TotalHits()", searchResult.TotalHits()) matchBoolList, err := es.SearchSlice7(searchResult, ExampleAccount{}) if err != nil { return } xjson.Print("matchBoolList", matchBoolList) return
Output:
func (Example) SearchBoolFilter ¶
func (Example) SearchBoolFilter()
SearchBoolFilter see example
Example ¶
ExampleExample_SearchBool 搜索 bool 条件
/* GET /bank/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } } */ var err error /* only test use */ defer func() { if err != nil { xerr.PrintStack(err) } }() ctx := context.Background() searchResult, err := example.Client.Search(). Index("bank"). Source(es.M{ "query": es.M{ "bool": es.M{ "must": es.M{"match_all": es.M{}}, "filter": es.M{ "range": es.M{ "balance": es.M{ "gte": 20000, "lte": 30000, }, }, }, }, }, }). Do(ctx) if err != nil { return } xjson.Print("searchResult.TotalHits()", searchResult.TotalHits()) matchBoolFilterList, err := es.SearchSlice7(searchResult, ExampleAccount{}) if err != nil { return } xjson.Print("matchBoolFilterList", matchBoolFilterList) return
Output:
func (Example) SearchFromSize ¶
func (Example) SearchFromSize()
SearchFromSize see Example
Example ¶
ExampleExample_SearchFromSize 分页搜索
/* GET /bank/_search { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ], "from": 10, "size": 10 } */ var err error /* only test use */ defer func() { if err != nil { xerr.PrintStack(err) } }() ctx := context.Background() searchResult, err := example.Client.Search(). Index("bank"). Sort("account_number", true). From(10). Size(10). Do(ctx) if err != nil { return } formSizeList, err := es.SearchSlice7(searchResult, ExampleAccount{}) if err != nil { return } xjson.Print("formSizeList", formSizeList) return
Output:
func (Example) SearchGroupBy ¶
func (Example) SearchGroupBy()
SearchGroupBy see example
Example ¶
ExampleExample_SearchGroupBy 搜索-聚合分析-分组
/* GET /bank/_search { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state" } } } } */ var err error /* only test use */ defer func() { if err != nil { xerr.PrintStack(err) } }() ctx := context.Background() result, err := example.Client.Search().Source(es.M{ "size": 0, "aggs": es.M{ "group_by_state": es.M{ "terms": es.M{ "field": "state", }, }, }, }).Do(ctx) if err != nil { return } xjson.PrintIndent("group_by_state", result.Aggregations["group_by_state"]) return
Output:
func (Example) SearchMatch ¶
func (Example) SearchMatch()
SearchMatch see example
Example ¶
ExampleExample_SearchMatch 搜索匹配分词 确定一下 bank 是否配置了mappings address,如果 mappings 类型是 keyword 则无法分词搜索
/* GET /bank/_search { "query": { "match": { "address": "mill lane" } } } */ var err error /* only test use */ defer func() { if err != nil { xerr.PrintStack(err) } }() ctx := context.Background() searchResult, err := example.Client.Search(). Index("bank"). Query(es7.NewMatchQuery( "address", "mill lane", )). Size(10). Do(ctx) if err != nil { return } xjson.Print("searchResult.TotalHits()", searchResult.TotalHits()) matchList, err := es.SearchSlice7(searchResult, ExampleAccount{}) if err != nil { return } xjson.Print("matchList", matchList) return
Output:
func (Example) SearchMatchPhrase ¶
func (Example) SearchMatchPhrase()
SearchMatchPhrase see example
Example ¶
ExampleExample_SearchMatchPhrase 搜索匹配短语
/* GET /bank/_search { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } } */ var err error /* only test use */ defer func() { if err != nil { xerr.PrintStack(err) } }() ctx := context.Background() searchResult, err := example.Client.Search(). Index("bank"). Query(es7.NewMatchPhraseQuery( "address", "mill lane", )). Size(10). Do(ctx) if err != nil { return } xjson.Print("searchResult.TotalHits()", searchResult.TotalHits()) matchList, err := es.SearchSlice7(searchResult, ExampleAccount{}) if err != nil { return } xjson.Print("matchList", matchList) return
Output: