在 Elastic 中搜索模板以查找过滤器和应该

Search Template in Elastic for filter and should

我正在设置搜索模板以获取 bool should 查询中的 bool 过滤器列表。我在 es [A,B,C] 上有 3 个字段。查询应该 return 返回这三个字段组合的桶。我应该能够将 [a1,b1,c1] [a2,b2,c2] ... [an,bn,cn] 作为参数提供给模板

我尝试创建一个模板,该模板具有带有 {{#toJson}} 子句{{/toJson}} 的 should 查询,以创建内部术语过滤器对象,例如

"clauses":[
{"term": {"fieldA": 30}},
{"term": {"fieldB": 0}},
{"term": {"fieldC": 1}}
]

我希望在 "should" 中有一个围绕它的部分: [{{#shouldBlock}}{{#toJson}}clauses{{/toJson}}{{/ShouldBlock}}]

有什么方法可以在 should 中添加一个过滤器,它采用“toJson

喜欢

"bool":{
"should":[
{
          // this loops for 100 times   
          "bool":{
                  "filter":[
                          {term for fieldA},
                          {term for fieldB},  // three terms for three fields
                          {term for fieldC}
                           ] 
                 }
          // end of loop
}
]

这是我的查询。这是对 render

的预期响应
GET adn/_search
{
  "size": 10,
  "query": {
       "bool": {
         "minimum_should_match": 1, 
         "should": [
           {
             "bool": {
               "filter": [
                 {"term": {"fieldA": 30}},
                 {"term": {"fieldB": 0}},
                 {"term": {"fieldC": 1}}
               ]
           }
          },
            {
             "bool": {
               "filter": [
                 {"term": {"fieldA": 0}},
                 {"term": {"fieldB": 1}},
                 {"term": {"fieldC": 0}}
               ]
           }
          }
         ]
       }
     }
}

我的第一个尝试是在一个部分中添加 should as text 的内部条件

GET _render/template
{
    "source": "{\"query\": {\"bool\": {\"should\":[ {{#section}} {\"bool\":{\"filter\": {{#toJson}}clauses{{/toJson}} } } {{/section}} }}}",
    "params": {
      "section":{
        "shouldBlock":[]
      },
      "clauses": [
            { "term": { "breakId" : 1 } },
            { "term": { "offset" : 0 } }
        ]

    }
}

出现错误

{
  "error": {
    "root_cause": [
      {
        "type": "json_parse_exception",
        "reason": "Unexpected close marker '}': expected ']' (for Array starting at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@5a909404; line: 1, column: 30])\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@5a909404; line: 1, column: 102]"
      }
    ],
    "type": "json_parse_exception",
    "reason": "Unexpected close marker '}': expected ']' (for Array starting at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@5a909404; line: 1, column: 30])\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@5a909404; line: 1, column: 102]"
  },
  "status": 500
}

我尝试了一些东西,比如带有参数的部分

GET _render/template
{
    "source": "{\"query\": {\"bool\": {\"should\":[ {{#section}} {{shouldBlock}} {{#toJson}}clauses{{/toJson}} }] {{/section}} } } }",
    "params": {
      "clauses": [
            { "term": { "fieldA" : 1 } },
            { "term": { "fieldB" : 0 } }
        ]

    }
}

我必须记住的事情。 循环部分:100 次

"bool":{
       "filter":[
                 {"term": {"fieldA": a1}},
                 {"term": {"fieldB": b1}},
                 {"term": {"fieldC": c1}},
                ]
}

以上 100 个对象中的每个对象都有一个不同的过滤器块,其中包含 3 个字段的组合,这 3 个字段具有不同的值 [fieldA,fieldB,fieldC]

我读到"filter"的数组可以通过elasticsearch的'toJson'函数实现,但是外面的部分我很难破译

如果有人能帮助我就太好了

您的查询不正确。一旦你修复它应该工作:

这是行之有效的方法:

GET _render/template
{
  "source": """{
  "query": {
    "bool": {
      {{#section}}
      "should": [
        {
          "bool": {
            "filter": [
              {{#toJson}}clauses{{/toJson}}
              ]
          }
        }
      ]
      {{/section}}
    }
  }
}""",
  "params": {
    "section":true, 
    "clauses": [
      {
        "term": {
          "fieldA": 1
        }
      },
      {
        "term": {
          "fieldB": 0
        }
      }
    ]
  }
}

根据评论编辑:


您的所有查询都将成为请求参数的一部分。 mustache 是无逻辑模板,所以你不能有 if-elseloops

GET _render/template
{
  "source": """
{
  "query": {
    "bool": {
      {{#section}}
      "should": {{#toJson}}clauses{{/toJson}}
      {{/section}}
    }
  }
}
""",
  "params": {
  "section":true,
    "clauses": [
      {
        "bool": {
          "filter": [
            {
              "term": {
                "fieldA": 1
              }
            },
            {
              "term": {
                "fieldB": 0
              }
            }
          ]
        }
      },
        {
          "bool": {
          "filter": [
            {
              "term": {
                "fieldA": 1
              }
            },
            {
              "term": {
                "fieldB": 0
              }
            }
          ]
        }
        },
        {
          "bool": {
          "filter": [
            {
              "term": {
                "fieldA": 1
              }
            },
            {
              "term": {
                "fieldB": 0
              }
            }
          ]
        }
        }

    ]
  }
}