elasticsearch,Groovy,elasticsearch" /> elasticsearch,Groovy,elasticsearch" />

使用groovy api的Elasticsearch布尔过滤器

使用groovy api的Elasticsearch布尔过滤器,groovy,elasticsearch,Groovy,elasticsearch,我有下一个问题: GET /index/type/_search { "query": { "filtered": { "query": { "match": { "project": "ABC" } }, "filter": { "bool": { "must": [ {"term": { "subtech"

我有下一个问题:

GET /index/type/_search
{
  "query": {
    "filtered": {
      "query": {
        "match": {
          "project": "ABC"
        }
      },
      "filter": {
        "bool": {
          "must": [
            {"term": {
              "subtech": 2
            }},
            {
              "term": {
                "tech": 1
              }
            },
            {
              "term": {
                "country": 1
              }
            }
          ]
        }
      }
    }
  }  
}
结果有50次点击。所有这些都很好,因为我有100个文档,其中包含技术:1、国家:1和50个子技术:1、子技术:2

当我使用groovy api编写此查询时,我使用最后一个术语值进行了过滤: -最后值子技术,点击次数=50 -最后值技术,点击次数=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中的术语用于
子技术
技术
,以及
国家
。对于Groovy API,您要根据
代理
子技术
国家
进行过滤

您还将在
bool
中重新分配
must
,而不是为其分配一个过滤器数组

must {
  term(agent:1)
}
must {
  term(subtech:2)
}
must {
  term(country:1)
}

请参考上面的示例,了解应该如何执行此操作。这里的麻烦在于
必须
的重新分配/调用。传递
映射是有效的,尽管我个人建议更紧密地镜像JSON。

感谢您的回复,我修复了示例,结果是相同的:(.我尝试了您的示例,但我有一个例外:
org.elasticsearch.search.SearchParseException:[index][4]:from[-1],size[-1]:解析失败[未能分析源[{“查询”:{“筛选”:{“查询”:{“匹配”:{“项目”:“ABC”},筛选”:{“bool”:{“must.getAt”:[{“term”:{“subtech”:2}},{“term”:{“tech”:1},{“term”:{“country”:1}}}}}]]]
您正在使用的Groovy客户端的哪个版本?另外,尝试将其更改为
must=[
must=[
from
must>[
。我有groovy 2.4.3。我更改了must=[for must[一切正常:)。非常感谢!!!我将很快推出一个补丁,以满足对
=
的需求。这应该不是必需的,但我很高兴有解决办法!
{
  indices "index"
  types "type"
  source {
    query {
      filtered {
        query {
          match {
            project = "ABC"
          }
        }
        filter {
          bool {
            must [
              {
                term {
                  subtech = 2
                }
              },
              {
                term {
                  tech = 1
                }
              },
              {
                term {
                  country = 1
                }
              }
            ]
          }
        }
      }
    }
  }
}