簡體   English   中英

搜索模板中的小胡子雙引號問題

[英]Mustache double quotes problem in search templates

在 Elasticsearch 模板中使用 mustache False 值功能的最佳方法是什么?

目前我正在嘗試 select function 基於 boolean 值。 渲染似乎按照邏輯工作,但它打印出空雙引號,我無法擺脫它們。

代碼示例小胡子模板片段:

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

模板中的查詢必須是完整的查詢,而不僅僅是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\" }}}}"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM