我有下一个查询:
GET /index/type/_search
{
"query": {
"filtered": {
"query": {
"match": {
"project": "ABC"
}
},
"filter": {
"bool": {
"must": [
{"term": {
"subtech": 2
}},
{
"term": {
"tech": 1
}
},
{
"term": {
"country": 1
}
}
]
}
}
}
}
}
响应有50个匹配。一切都很好,因为我有100个文档,其中包括tech:1,country:1和五十五十个subtech:1,subtech:2
当我用groovy api编写此查询时,我已经过滤了最后一项值:
-最后的值(value)子技术,点击= 50
-最终值(value)技术,点击= 100
-最终值国家/地区,点击数= 100
查询:
client.search {
indices 'index'
types 'type'
source {
query {
filtered {
query {
match(project: 'ABC')
}
filter {
bool {
must {
term(subtech:2)
}
must {
term(tech:1)
}
must {
term(country:1)
}
}
}
}
}
}
}
任何建议。
最佳答案
受支持的Groovy DSL与JSON非常相似。
{
indices "index"
types "type"
source {
query {
filtered {
query {
match {
project = "ABC"
}
}
filter {
bool {
must [
{
term {
subtech = 2
}
},
{
term {
tech = 1
}
},
{
term {
country = 1
}
}
]
}
}
}
}
}
}
在您的Groovy版本中,我注意到您的过滤器与JSON过滤器不同。具体来说,您来自JSON的术语适用于
subtech
,tech
和country
。对于Groovy API,您要针对agent
,subtech
和country
进行过滤。您还将在
must
中重新分配bool
,而不是为其分配过滤器数组。must {
term(agent:1)
}
must {
term(subtech:2)
}
must {
term(country:1)
}
请引用上面的示例以查看应如何进行。这是麻烦的
must
的重新分配/调用。传递Map
是有效的,尽管我个人建议更紧密地镜像JSON。关于groovy - 带有Groovy API的Elasticsearch Bool过滤器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31681827/