1. 程式人生 > >elasticsearch高階功能系列之search template

elasticsearch高階功能系列之search template

四種search template方式: 

GET book/book/_search/template
{
  "file": "match_query",
  "params": {
    "field": "bookName.keyword",
    "value": "金明館叢稿二編"
  }
}


GET book/book/_search/template
{
  "file":"match_tojson",
  "params": {
    "matchCondition":{
      "bookName.keyword":"金明館叢稿二編"
    }
  }
}


GET book/book/_search/template
{
  "file":"match_join",
  "params": {
    "field":"bookName",
    "fields":["陳寅恪的最後20年","陳垣史源學雜文"]
  }
}


GET book/book/_search/template
{
  "file":"match_condition",
  "params": {
    "match_field":"bookAuthor",
    "match_value":"陳寅恪",
    "isCondition":true,
    "range_field":"bookPrice",
    "start":10,
    "end": 10
  }
}

以下4個模板檔案放置在es安裝目錄config/scripts資料夾下,重啟生效。

1、match_query.mustache檔案:

{
    "query":{
      "match": {
         "{{field}}": "{{value}}{{^value}}王小波{{/value}}"
      }
  }
}


2、match_tojson.mustache檔案:

{
    "query":{
      "match": {{#toJson}}matchCondition{{/toJson}}
      }
  }
}

3、match_join.mustache檔案:

{
    "query":{
      "match": {
	     "{{field}}":"{{#join delimiter=','}}fields{{/join delimiter=','}}"
	  }
  }
}


4、match_condition.mustache檔案:

{
    "query":{
      "match": {
             "{{match_field}}":"{{match_value}}"
	    }
      },
	  "post_filter":{
		{{#isCondition}}
			"range":{
				"{{range_field}}":{
				   {{#start}}
					  "gte":{{start}}
					   {{#end}},{{/end}}
				    {{/start}}
				   
				   {{#end}}
					   "lte":{{end}}
				   {{/end}}
				  }
				}
		
		{{/isCondition}}
	  
	  }
  }
}