使用Mustache语言作为搜索请求的预处理
Search Template – 解耦程序 & 搜索 DSL
创建搜索模板
POST _scripts/tmdb
{
"script": {
"lang": "mustache",
"source": {
"_source": [
"title","overview"
],
"size": 20,
"query": {
"multi_match": {
"query": "{{q}}",
"fields": ["title","overview"]
}
}
}
}
}
删除模板
DELETE _scripts/tmdb
查看搜索模板
GET _scripts/tmdb
使用搜索模板
POST tmdb/_search/template
{
"id":"tmdb",
"params": {
"q": "basketball with cartoon aliens"
}
}
验证模板的效果
ES提供了_render/template
方法,来对内联模板或者已经存储的模板进行验证。
POST _render/template
{
"source": {
"query": {
"multi_match": {
"query": "{{basketball with cartoon aliens}}",
"fields": ["title","overview"]
}
}
},
"params": {
"verview" : "Michael Jordan agrees to help the Looney Tunes play a basketball game against alien slavers to determine their freedom.",
"title" : "Space Jam"
}
}
Index Alias 实现零停机运维
PUT movies-2019/_doc/1
{
"name":"the matrix",
"rating":5
}
GET movies-2019/_search
PUT movies-2019/_doc/2
{
"name":"Speed",
"rating":3
}
配置别名
POST _aliases
{
"actions": [
{
"add": {
"index": "movies-2019",
"alias": "movies-latest"
}
}
]
}
使用别名查询
POST movies-latest/_search
{
"query": {
"match_all": {}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "movies-2019",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "the matrix",
"rating" : 5
}
},
{
"_index" : "movies-2019",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Speed",
"rating" : 3
}
}
]
}
}
设置别名并配置查询条件
POST _aliases
{
"actions": [
{
"add": {
"index": "movies-2019",
"alias": "movies-lastest-highrate",
"filter": {
"range": {
"rating": {
"gte": 4
}
}
}
}
}
]
}
查询结果
POST movies-lastest-highrate/_search
{
"query": {
"match_all": {}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "movies-2019",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "the matrix",
"rating" : 5
}
}
]
}
}
查看这个索引
GET movies-2019
{
"movies-2019" : {
"aliases" : {
"movies-lastest-highrate" : {
"filter" : {
"range" : {
"rating" : {
"gte" : 4
}
}
}
},
"movies-latest" : { }
},
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"rating" : {
"type" : "long"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1585901507315",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "A3p5j3oaSUGlwpcnus6LuA",
"version" : {
"created" : "7010099"
},
"provided_name" : "movies-2019"
}
}
}
}
有两个别名:movies-lastest-highrate和movies-latest