美文网首页
使用 Search Template 和 Index Alias

使用 Search Template 和 Index Alias

作者: 滴流乱转的小胖子 | 来源:发表于2020-08-06 18:12 被阅读0次

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

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

    • 定义 Search Template
    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"的解释

    • 使用Search Template
    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": {}
      }
    }
    
    

    相关文章

      网友评论

          本文标题:使用 Search Template 和 Index Alias

          本文链接:https://www.haomeiwen.com/subject/tvsprktx.html