[elasticsearch笔记] search-template/mustache

本文详细介绍了Elasticsearch中的search-template功能,包括如何使用post、get、delete和render操作来管理和执行模板。重点讲解了mustache语法,如{{#toJson}}{{/toJson}}用于对象转JSON,{{#join}}用于数组连接,{{var}}{{^var}}defaultValue{{/var}}进行条件判断,以及{{#url}}value{{/url}}对值进行URL编码。通过这些知识点,读者可以更好地掌握Elasticsearch的搜索模板机制。

search-template

post/get/delete/render/search template

POST _scripts/test_template
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "term": {
          "{{param_key}}": "{{param_value}}"
        }
      }
    }
  }
}

GET _scripts/test_template

DELETE _scripts/test_template

GET _render/template
{
  "id": "test_template", 
  "params": {
      "param_key": "geo.src",
      "param_value":"IN"
  },
  "explain":true
}

GET kibana_sample_data_logs/_search?size=1

GET kibana_sample_data_logs/_search/template
{
  "id": "test_template", 
  "params": {
      "param_key": "geo.src",
      "param_value":"IN"
  },
  "explain":true
}

GET _msearch/template
{
  "index": "kibana_sample_data_logs"
}
{
  "id": "test_template",
  "params": {
    "param_key": "geo.src",
    "param_value": "IN"
  }
}
{
  "index": "kibana_sample_data_logs"
}
{
  "id": "test_template",
  "params": {
    "param_key": "geo.dest",
    "param_value": "IN"
  }
}

render

POST _scripts/test_template
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "term": {
          "{{param_key}}": "{{param_value}}"
        }
      }
    }
  }
}

GET _render/template
{
  "source": {
    "query": {
      "term": {
        "{{param_key}}": "{{param_value}}"
      }
    }
  },
  "params": {
  	"param_key":"age",
    "param_value": 32
  }
}

GET _render/template
{
  "id": "test_template",
  "params": {
  	"param_key":"age",
    "param_value": 32
  }
}

{{#toJson}}{{/toJson}}

GET _search/template
{
  "source": """{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}""",
  "params": {
    "statuses": {
      "age": [
        "32",
        "34"
      ]
    }
  }
}

GET _render/template
{
  "source": """{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}""",
  "params": {
    "statuses": {
      "age": [
        "32",
        "34"
      ]
    }
  }
}

GET _search/template
{
  "source": """{"query":{"bool":{"must": {{#toJson}}clauses{{/toJson}} }}}""",
  "params": {
    "clauses": [
      {
        "match": {
          "firstname": "Amber"
        }
      },
      {
        "match": {
          "lastname": "Duke"
        }
      }
    ]
  }
}

{{#join}}array{{/join}}

GET _search/template
{
  "source": {
    "query": {
      "match": {
        "emails": "{{#join}}emails{{/join}}"
      }
    }
  },
  "params": {
    "emails": [
      "amberduke@pyrami.com",
      "cottonchristensen@prowaste.com"
    ]
  }
}

GET _render/template
{
  "source": {
    "query": {
      "match": {
        "emails": "{{#join}}emails{{/join}}"
      }
    }
  },
  "params": {
    "emails": [
      "amberduke@pyrami.com",
      "cottonchristensen@prowaste.com"
    ]
  }
}

GET _render/template
{
  "source": {
    "query": {
      "range": {
        "born": {
          "gte": "{{date.min}}",
          "lte": "{{date.max}}",
          "format": "{{#join delimiter='||'}}date.formats{{/join delimiter='||'}}"
        }
      }
    }
  },
  "params": {
    "date": {
      "min": "2016",
      "max": "31/12/2017",
      "formats": [
        "dd/MM/yyyy",
        "yyyy"
      ]
    }
  }
}

GET _search/template
{
  "source": {
    "query": {
      "range": {
        "born": {
          "gte": "{{date.min}}",
          "lte": "{{date.max}}",
          "format": "{{#join delimiter='||'}}date.formats{{/join delimiter='||'}}"
        }
      }
    }
  },
  "params": {
    "date": {
      "min": "2016",
      "max": "31/12/2017",
      "formats": [
        "dd/MM/yyyy",
        "yyyy"
      ]
    }
  }
}

{{var}}{{^var}}defaultValue{{/var}}

GET _render/template
{
  "source": {
    "query": {
      "range": {
        "age": {
          "gte": "{{start}}",
          "lte": "{{end}}{{^end}}36{{/end}}"
        }
      }
    }
  },
  "params": {
    "start": 35
  }
}

GET _render/template
{
  "source": {
    "query": {
      "range": {
        "age": {
          "gte": "{{start}}",
          "lte": "{{end}}{{^end}}36{{/end}}"
        }
      }
    }
  },
  "params": {
    "start": 35,
    "end": 38
  }
}

# end default is 36
GET _search/template
{
  "source": {
    "query": {
      "range": {
        "age": {
          "gte": "{{start}}",
          "lte": "{{end}}{{^end}}36{{/end}}"
        }
      }
    }
  },
  "params": {
    "start": 35
  }
}

{{#url}}value{{/url}}

GET _render/template
{
  "source" : {
      "query" : {
          "term": {
              "http_access_log": "{{#url}}{{host}}/{{page}}{{/url}}"
          }
      }
  },
  "params": {
      "host": "https://www.elastic.co/",
      "page": "learn"
  }
}

GET _search/template
{
  "source" : {
      "query" : {
          "term": {
              "http_access_log": "{{#url}}{{host}}/{{page}}{{/url}}"
          }
      }
  },
  "params": {
      "host": "https://www.elastic.co/",
      "page": "learn"
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值