在Elastic中搜索模板以获取过滤器,并且应该

问题描述 投票:0回答:1

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

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

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

我希望在“ should”中有一个围绕它的部分:[{{#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}}
               ]
           }
          }
         ]
       }
     }
}

我的第一次尝试是将应有的内部条件添加为节内的文本

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个对象中的每个对象都有一个不同的过滤器块,其中三个字段的组合对于这三个字段[fieldA,fieldB,fieldC]具有不同的值

[我读到“ filter”的数组可以通过弹性搜索的'toJson'函数来实现,但是它的外部部分让我难以解读

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

我正在设置搜索模板,以获取bool应查询的bool过滤器列表。我在es [A,B,C]上有3个字段。查询应返回这三个字段的组合的存储桶。 i ...

templates elasticsearch mustache elasticsearch-aggregation elasticsearch-template
1个回答
0
投票

您的查询不正确。修复后,它应该可以工作:

© www.soinside.com 2019 - 2024. All rights reserved.