繁体   English   中英

在 Elastic 中搜索过滤器和应该的模板

[英]Search Template in Elastic for filter and should

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

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

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

我希望在“应该”中有一个部分: [{{#shouldBlock}}{{#toJson}}clauses{{/toJson}}{{/ShouldBlock}}]

有什么办法可以在应该采用“ 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 个中的每一个 object 都有一个不同的过滤器块,其中包含 3 个字段的组合,这 3 个字段的值不同 [fieldA,fieldB,fieldC]

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

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

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

渲染模板

这是一个有效的:

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

    ]
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM