英文:
I have got Query How can convert query to dsl
问题
我有一个问题,如何将查询转换为DSL?我不知道如何转换,你能帮助我吗?谢谢。
centroidQuery := elastic.NewBoolQuery().
Must(elastic.NewMatchQuery("centroid.bid.keyword", bid)).
Must(elastic.NewRangeQuery("centroid.count_user").Gte(2)).
MustNot(elastic.NewMatchQuery("centroid.source", "live")).
MustNot(elastic.NewQueryStringQuery("deleted:true"))
queryCountAggs := elastic.NewSumAggregation().Field("centroid.count")
queryCentroidResult, err := IntersectionClient.Client.
Search(queryCentroidIndex).
Query(centroidQuery).
Sort("centroid.count_user", false).
From(int((pageNum-1)*pageSize)).
Size(int(pageSize)).
Aggregation("queryCount", queryCountAggs).
Do(context.Background())
英文:
I have got Query How can convert query to dsl,I don't know how to convert,Can you help me
thanks
centroidQuery := elastic.NewBoolQuery().
Must(elastic.NewMatchQuery("centroid.bid.keyword", bid)).
Must(elastic.NewRangeQuery("centroid.count_user").Gte(2)).
MustNot(elastic.NewMatchQuery("centroid.source", "live")).
MustNot(elastic.NewQueryStringQuery("deleted:true"))
queryCountAggs := elastic.NewSumAggregation().Field("centroid.count")
queryCentroidResult, err := IntersectionClient.Client.
Search(queryCentroidIndex).
Query(centroidQuery).
Sort("centroid.count_user", false).
From(int((pageNum-1)*pageSize)).
Size(int(pageSize)).
Aggregation("queryCount", queryCountAggs).
Do(context.Background())
答案1
得分: 1
请尝试这个代码:
package main
import (
"fmt"
"github.com/olivere/elastic"
)
func main() {
centroidQuery := elastic.NewBoolQuery().
Must(elastic.NewMatchQuery("centroid.bid.keyword", "1")).
Must(elastic.NewRangeQuery("centroid.count_user").Gte(2)).
MustNot(elastic.NewMatchQuery("centroid.source", "live")).
MustNot(elastic.NewQueryStringQuery("deleted:true"))
queryCountAggs := elastic.NewSumAggregation().Field("centroid.count")
query := elastic.NewSearchRequest().Index("test").
Query(centroidQuery).
Sort("centroid.count_user", false).
From(0).
Size(1).
Aggregation("queryCount", queryCountAggs)
s, _ := query.Body()
fmt.Println(s)
}
输出结果:
{
"aggregations": {
"queryCount": {
"sum": {
"field": "centroid.count"
}
}
},
"from": 0,
"query": {
"bool": {
"must": [
{
"match": {
"centroid.bid.keyword": {
"query": "1"
}
}
},
{
"range": {
"centroid.count_user": {
"from": 2,
"include_lower": true,
"include_upper": true,
"to": null
}
}
}
],
"must_not": [
{
"match": {
"centroid.source": {
"query": "live"
}
}
},
{
"query_string": {
"query": "deleted:true"
}
}
]
}
},
"size": 1,
"sort": [
{
"centroid.count_user": {
"order": "desc"
}
}
]
}
英文:
Try this:
package main
import (
"fmt"
"github.com/olivere/elastic"
)
func main() {
centroidQuery := elastic.NewBoolQuery().
Must(elastic.NewMatchQuery("centroid.bid.keyword", "1")).
Must(elastic.NewRangeQuery("centroid.count_user").Gte(2)).
MustNot(elastic.NewMatchQuery("centroid.source", "live")).
MustNot(elastic.NewQueryStringQuery("deleted:true"))
queryCountAggs := elastic.NewSumAggregation().Field("centroid.count")
query := elastic.NewSearchRequest().Index("test").
Query(centroidQuery).
Sort("centroid.count_user", false).
From(0).
Size(1).
Aggregation("queryCount", queryCountAggs)
s,_ := query.Body()
fmt.Println(s)
}
Output:
{
"aggregations": {
"queryCount": {
"sum": {
"field": "centroid.count"
}
}
},
"from": 0,
"query": {
"bool": {
"must": [
{
"match": {
"centroid.bid.keyword": {
"query": "1"
}
}
},
{
"range": {
"centroid.count_user": {
"from": 2,
"include_lower": true,
"include_upper": true,
"to": null
}
}
}
],
"must_not": [
{
"match": {
"centroid.source": {
"query": "live"
}
}
},
{
"query_string": {
"query": "deleted:true"
}
}
]
}
},
"size": 1,
"sort": [
{
"centroid.count_user": {
"order": "desc"
}
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论