简体   繁体   English

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

[英]Search Template in Elastic for filter and should

I am setting up a search template to get a list of bool filters within a bool should query.我正在设置一个搜索模板以获取 bool 应查询中的 bool 过滤器列表。 I have 3 fields on es [A,B,C].我在 es [A,B,C] 上有 3 个字段。 The query should return back buckets of combination of these three fields.查询应该返回这三个字段组合的桶。 i should be able to give [a1,b1,c1] [a2,b2,c2]... [an,bn,cn] as params to the template我应该能够将 [a1,b1,c1] [a2,b2,c2]... [an,bn,cn] 作为模板的参数

I tried creating a template that has a should query with a {{#toJson}}clauses{{/toJson}} to create the inner term filter objects like我尝试创建一个模板,该模板具有一个带有 {{#toJson}}clauses{{/toJson}} 的应该查询来创建内部术语过滤器对象,例如

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

I was hoping to have a section around it inside the "should": [{{#shouldBlock}}{{#toJson}}clauses{{/toJson}}{{/ShouldBlock}}]我希望在“应该”中有一个部分: [{{#shouldBlock}}{{#toJson}}clauses{{/toJson}}{{/ShouldBlock}}]

is there any way to have like a filter inside the should that takes the " toJson "有什么办法可以在应该采用“ toJson ”的情况下使用过滤器

Like喜欢

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

This is my query.这是我的查询。 and this is expected as a response for the 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}}
               ]
           }
          }
         ]
       }
     }
}

My first try was to add the inside condition for should as text inside a section我的第一次尝试是将 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 } }
        ]

    }
}

that gave an error这给出了一个错误

{
  "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
}

I tried some thing like a section with a param to hold我尝试了一些东西,比如一个带有参数的部分

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

    }
}

Things I had to keep in mind.我必须记住的事情。 section that loops: 100 times循环部分:100 次

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

Each one object of the above 100 has aa different filter block with combination of 3fields with different values for those 3 fields [fieldA,fieldB,fieldC]上述 100 个中的每一个 object 都有一个不同的过滤器块,其中包含 3 个字段的组合,这 3 个字段的值不同 [fieldA,fieldB,fieldC]

I read that the array for "filter" can be achieved by the 'toJson' function of elastic search but the outer part of it is difficult for me to decipher我读到“过滤器”的数组可以通过弹性搜索的“toJson”function 来实现,但它的外部部分对我来说很难破译

It would be great if some one could help me out如果有人可以帮助我,那就太好了

Your query is not correct.您的查询不正确。 Once you fix that it should work:一旦你修复它应该工作:

渲染模板

Here's the one that works:这是一个有效的:

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

Edit based on comment:根据评论编辑:


All your queries will then be part of the request parameters.然后,您的所有查询都将成为请求参数的一部分。 mustache is logic-less templates so you cannot have if-else or loops 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