【问题标题】:Search Template in Elastic for filter and should在 Elastic 中搜索过滤器和应该的模板
【发布时间】:2019-10-08 20:15:30
【问题描述】:

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

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

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

我希望在“应该”里面有一个部分: [{{#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
}
]

这是我的查询。这应该是对渲染的响应

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 的内部条件添加为节内的文本

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]

我读到“过滤器”的数组可以通过弹性搜索的'toJson'函数来实现,但外部部分我很难破译

如果有人可以帮助我,那就太好了

【问题讨论】:

    标签: templates elasticsearch mustache elasticsearch-aggregation elasticsearch-template


    【解决方案1】:

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

    这是一个有效的方法:

    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
                  }
                }
              ]
            }
            }
    
        ]
      }
    }
    

    【讨论】:

    • 谢谢你..但在你的场景中..你只考虑应该数组的对象..但我需要给应该数组100个对象..每个对象都会有 3 个术语过滤器#polynomilProton @polynomialProton
    • 您编辑的答案有效.. 非常感谢,我确实尝试为子句提供对象数组,但是当我直接转义 \" 而不是使用 """{" 时错过了一个或两个'{'查询"}""" .. 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 2022-09-24
    • 1970-01-01
    相关资源
    最近更新 更多