Elasticsearch Mustache可选参数

问题描述 投票:3回答:3

我一直在使用Elasticsearch模板,特别是在可选参数方面。我想在其中添加可选过滤器。这是我正在尝试的代码片段:

{
  "filter" : {
    "bool" : {
      "must" : [
        {{#ProductIDs.0}}
        { "terms" : { "Product.ProductID" : [{{#ProductIDs}}{{.}},{{/ProductIDs}}] } }
        {{/ProductIDs.0}}
      ]
    }
  }
}

当然我用"替换了\",将其丑化,然后将其包裹在{ "template" :"_snippet_above_" }中。

现在,当我尝试使用以下命令进行调用时:

GET /statistic/_search/template
{
    "template": {
        "id": "test" 
    },
    "params": {
        "ProductIDs": [1,2]
    }
}

它忽略了我提供的参数,但是当我尝试在官方mustache.io演示页面中执行此操作时-很好。

我也尝试过{{#ProductIDs.length}}选项-没成功。经过一些研究,我发现mustache.jsmustache.java之间有一个区别。我假设Elasticsearch使用JAVA版本,并且不支持length参数,所以我必须依靠isEmpty。因此,我将查询重写如下:

{
  "filter" : {
    "bool" : {
      "must" : [
        {{^ProductIDs.isEmpty}}
        { "terms" : { "Product.ProductID" : [{{#ProductIDs}}{{.}},{{/ProductIDs}}] } }
        {{/ProductIDs.isEmpty}}
      ]
    }
  }
}

现在,当我使用ProductIDs列表查询模板时,它可以正常工作,但是,如果删除参数,则不会带来任何结果。我认为它会生成此:

{
  "filter" : {
    "bool" : {
      "must" : [
        { "terms" : { "Product.ProductID" : [] } }
      ]
    }
  }
}

如果我发送空数组作为参数-可以正常工作。

GET /statistic/_search/template
{
    "template": {
        "id": "test" 
    },
    "params": {
        "ProductIDs": []
    }
}

我想是因为"ProductIDs"undefined而不是空。

是否有办法在mustache.java中处理这种情况,所以我可以忽略这些参数?

tl; dr;

问题是,如果我未通过模板在搜索请求中指定参数,则我的条件将呈现为空数组,请参见:
{
  "filter" : {
    "bool" : {
      "must" : [
        { "terms" : { "Product.ProductID" : [] } }
      ]
    }
  }
}

如果我将空数组作为参数传递,请参见:

GET /statistic/_search/template
{
    "template": {
        "id": "test" 
    },
    "params": {
        "ProductIDs": []
    }
}

它按预期工作,并且不生成模板中描述的过滤条件,因为数组中没有任何数据。

我想要这个:

GET /statistic/_search/template
{
    "template": {
        "id": "test" 
    },
    "params": {
    }
}

与此相同:

GET /statistic/_search/template
{
    "template": {
        "id": "test" 
    },
    "params": {
        "ProductIDs": []
    }
}

我一直在使用Elasticsearch模板,特别是在可选参数方面。我想在此添加可选的过滤器。这是我正在尝试的代码片段:{“ filter”:{“ bool”:...

java templates elasticsearch mustache elasticsearch-template
3个回答
© www.soinside.com 2019 - 2024. All rights reserved.