使用 Search Template 和 Index Alias

2020-08-06  本文已影响0人  滴流乱转的小胖子

一、 Search Template 实现程序和搜索DSL的解耦

在开发初期,虽然可以明确查询参数,但是往往还不能最终确定查询的DSL的具体结构。通过Search Template定义一个Contract,让程序开发人员与搜索工程师、性能工程师各司其职,解耦

DELETE _scripts/tmdb
GET _scripts/tmdb
POST _scripts/tmdb
{
  "script": {
    "lang": "mustache",
    "source": {
      "_source": [
        "title","overview"
      ],
      "size": 20,
      "query": {
        "multi_match": {
          "query": "{{q}}",
          "fields": ["title","overview"]
        }
      }
    }
  }
}

"lang": "mustache"的解释

POST tmdb/_search/template
{
    "id":"tmdb",
    "params": {
        "q": "basketball with cartoon aliens"
    }
}

二、 Index Alias 给索引起别名 ----- 实现零停机运维

image.png
POST movies-latest/_search
{
  "query": {
    "match_all": {}
  }
}

使用Alias创建不同查询的视图

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "movies-2019",
        "alias": "movies-lastest-highrate",
        "filter": {
          "range": {
            "rating": {
              "gte": 4
            }
          }
        }
      }
    }
  ]
}
POST movies-lastest-highrate/_search
{
  "query": {
    "match_all": {}
  }
}

上一篇下一篇

猜你喜欢

热点阅读