//client => client *elastic.Client
ctx := context.Background()
var res *elastic.SearchResult
var err error
//实例化一个bool搜索器
boolQ := elastic.NewBoolQuery()
boolQ.Must(elastic.NewMatchQuery("rootCategory","鞋类")) //一级类目必须是鞋类
boolQ.Filter(elastic.NewRangeQuery("sellPoint").Gt("0")) //销量大于0
//打印查询语句
q,_ := boolQ.Source()
PrintQuery(q)
//组装查询,查2条,对应的index 和 type
res,err = client.Search("shihuo_goods").Type("goods_v4").Query(boolQ).From(0).Size(2).Do(ctx)
if err != nil {
panic(err)
}
//循环查到的数据并且以json方式输出
for _,item := range res.Hits.Hits {
//fmt.Printf("%+v",*item.Source)
fmt.Println(string(*item.Source))
}
//自定义打印函数
func PrintQuery(src interface{}) {
fmt.Println("*****")
data, err := json.MarshalIndent(src, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
```
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8