我有一个问题,如何将查询转换为 DSL。

huangapple go评论85阅读模式
英文:

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"
      }
    }
  ]
}

huangapple
  • 本文由 发表于 2021年8月16日 10:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/68796823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定