在 Elasticsearch 模板中使用 mustache False 值功能的最佳方式是什么?
目前我正在嘗試根據布林值選擇函式。渲染似乎按照邏輯作業,但它列印出空雙引號,我無法擺脫它們。
代碼示例小胡子模板片段:
"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),
}
沒有小胡子值,它作業正常。我還注意到,在某些情況下,如果你用隨機函式將空雙引號括起來,它有時會起作用。似乎 Elastic 不喜歡必須以空雙引號開頭的情況。
到目前為止,我還在 ELK 社區中問過同樣的問題,但運氣不佳: 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
}
}
uj5u.com熱心網友回復:
模板中的查詢必須是完整的查詢,而不僅僅是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\" }}}}"
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537900.html
標籤:弹性搜索胡子
