groovy - 带有Groovy API的Elasticsearch Bool过滤器

标签 groovy elasticsearch

我有下一个查询:

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的术语适用于subtechtechcountry。对于Groovy API,您要针对agentsubtechcountry进行过滤。

您还将在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/

相关文章:

elasticsearch - 跨 token 化字段的Titan ES正则表达式查询?

elasticsearch - Elasticsearch建议过滤器按特定条件

java - IntelliJ IDEA 和 Gradle - 无法应用于 '(groovy.lang.Closure)'

groovy - 在 IntRange 上调用 .each{} 返回范围而不是每个整数

grails - 在Save()上引用 Controller 中的相关记录ID

groovy - Spock + Drools 测试

java - 在 Windows 上安装 Elasticsearch 时出错

groovy - 如何使用groovy设置时间+x分钟的SOAPUI属性值?

date - ElasticSearch - 日期直方图结果的分页

elasticsearch - 通过HTTPS的Filebeat