Search Template 和 Index Alias

使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值