elasticsearch 搜索模板中的双引号问题

gc0ot86w  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(525)

在Elasticsearch模板中使用mustache False值功能的最佳方式是什么?
目前我正在尝试选择基于布尔值的函数。渲染似乎是根据逻辑工作的,但它打印了空的双引号,我无法摆脱这些。
代码示例mustache模板片段:

"must": {
                    "function_score": {
                        "functions": [
                            "{{^isLocationFunctionNeeded}}",
                            {
                                "exp": {
                                    "location": {
                                        "origin": {
                                            "lat": "0.0",
                                            "lon": "0.0"
                                        },
                                        "offset": "1km",
                                        "scale": "50km"
                                    }
                                }
                            },
                            "{{/isLocationFunctionNeeded}}",
                            {
                                "random_score": {},
                                "weight": 0.00001
                            }
                        ],
                        "score_mode": "sum"
                    }
                }

转译程式码片段:

"must": {
                "function_score": {
                    "functions": [
                        "",
                        {
                            "random_score": {},
                            "weight": 1.0E-5
                        }
                    ],
                    "score_mode": "sum"
                }
            }

尝试在ELK上运行模板时出现错误:

"error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "failed to parse [START_OBJECT]. malformed query, expected a [VALUE_STRING] while parsing functions but got a [function_score] instead",
                "line": x (where "" is visible in Render snippet),
                "col": x (where "" is visible in Render snippet)
            }
        ],
"type": "x_content_parse_exception",
        "reason": " x (where "" is visible in Render snippet),[bool] failed to parse field [must]",
        "caused_by": {
            "type": "parsing_exception",
            "reason": "failed to parse [START_OBJECT]. malformed query, expected a [VALUE_STRING] while parsing functions but got a [function_score] instead",
            "line":  x (where "" is visible in Render snippet),,
            "col":  x (where "" is visible in Render snippet),
        }

没有mustache值它工作的很好。而且我注意到在某些情况下,如果你用随机函数包围空双引号,它有时会工作。似乎Elastic不喜欢以空双引号开头的必须情况。
我在麋鹿社区也问过同样的问题,但迄今为止运气不佳:https://discuss.elastic.co/t/mustache-double-quotes-problem-in-search-templates/318736
作为渲染模板的示例,我们可以尝试使用以下内容:

{
    "script": {
        "lang": "mustache",
        "source": {
            "must": {
                "function_score": {
                    "functions": [
                        "{{^isLocationFunctionNeeded}}",
                        {
                            "exp": {
                                "location": {
                                    "lat": "0.0",
                                    "lon": "0.0"
                                },
                                "offset": "1km",
                                "scale": "50km"
                            }
                        },
                        "{{/isLocationFunctionNeeded}}",
                        {
                            "random_score": {},
                            "weight": 0.00001
                        }
                    ],
                    "score_mode": "sum"
                }
            }
        }
    }
}

Calling template with params:

{
    "id": "example_template",
    "params": {
    "isLocationFunctionNeeded" : true
    }
}
pengsaosao

pengsaosao1#

模板中的查询必须是完整的查询,而不仅仅是must

POST _scripts/example_template
{
  "script": {
    "lang": "mustache",
    "source": """
      {
        "query": {
          "bool": {
            "must": {
              "function_score": {
                "functions": [
                  {{^isLocationFunctionNeeded}}
                  {
                    "exp": {
                      "location": {
                        "lat": "0.0",
                        "lon": "0.0"
                      },
                      "offset": "1km",
                      "scale": "50km"
                    }
                  },
                  {{/isLocationFunctionNeeded}}
                  {
                    "random_score": {},
                    "weight": 0.00001
                  }
                ],
                "score_mode": "sum"
              }
            }
          }
        }
      }
    """
  }
}

如果不能使用三重引号(例如使用Postman时),则需要将其作为一行字符串发送,并转义所有引号字符:

POST _scripts/example_template
{
  "script": {
    "lang": "mustache",
    "source": " { \"query\": { \"bool\": { \"must\": { \"function_score\": { \"functions\": [ {{^isLocationFunctionNeeded}} { \"exp\": { \"location\": { \"lat\": \"0.0\",  \"lon\": \"0.0\" }, \"offset\": \"1km\", \"scale\": \"50km\" } }, {{/isLocationFunctionNeeded}} { \"random_score\": {}, \"weight\": 0.00001 } ], \"score_mode\": \"sum\" }}}}"
  }
}

相关问题