首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于groovy的Elasticsearch bool过滤器

基于groovy的Elasticsearch bool过滤器
EN

Stack Overflow用户
提问于 2015-07-28 16:13:52
回答 1查看 434关注 0票数 1

我有下一个查询:

代码语言:javascript
复制
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编写这个查询时,我使用了最后一个项值:最后一个值subtech,hits = 50 -末后值技术,hits = 100最后值国家,hits = 100

查询:

代码语言:javascript
复制
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)
                                }
                            }
                        }
                    }
                }
            }
        }

任何建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-28 17:10:22

支持的Groovy非常类似于JSON。

代码语言:javascript
复制
{
  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,您正在对agentsubtechcountry进行过滤。

您还需要在must中重新分配bool,而不是给它分配一个过滤器数组。

代码语言:javascript
复制
must {
  term(agent:1)
}
must {
  term(subtech:2)
}
must {
  term(country:1)
}

请参考上面的示例,看看应该如何做。这里麻烦的是must的重新分配/调用。传入Map是有效的,尽管我个人建议更密切地镜像JSON。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31681827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档