美文网首页
4.9-使用SearchTemplate和IndexAlias进

4.9-使用SearchTemplate和IndexAlias进

作者: 落日彼岸 | 来源:发表于2020-04-03 14:40 被阅读0次

    Search Template – 解耦程序 & 搜索 DSL

    • Elasticsearch 的查询语句

    • 对相关性算分 / 查询性能都⾄关重要

    • 在开发初期,虽然可以明确查询参数,但是往 往还不能最终定义查询的DSL的具体结构

    • 通过 Search Template 定义⼀个 Contract

    • 各司其职,解耦

    • 开发⼈员 / 搜索⼯程师 / 性能⼯程师

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

    使⽤ Search Template 进⾏查询

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

    Index Alias 实现零停机运维

    image.png

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

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

    本节知识点回顾

    • Search Template 的使⽤场景

    • 如果通过 Mocha 语法定义⼀个 Search Template

    • Index Alias 的使⽤场景

    • 如何创建与使⽤ Index Alias

    • 通过 Index Alias 创建不同的查询视图

    课程Demo

    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"
        }
    }
    
    
    PUT movies-2019/_doc/1
    {
      "name":"the matrix",
      "rating":5
    }
    
    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": {}
      }
    }
    
    POST _aliases
    {
      "actions": [
        {
          "add": {
            "index": "movies-2019",
            "alias": "movies-lastest-highrate",
            "filter": {
              "range": {
                "rating": {
                  "gte": 4
                }
              }
            }
          }
        }
      ]
    }
    
    POST movies-lastest-highrate/_search
    {
      "query": {
        "match_all": {}
      }
    }
    

    Index Alias 经测试,可以对不同的索引起相同的别名

    #Index Alias 经测试,可以对不同的索引起相同的别名
    #alias可以指定多个index,但是只能指定一个index用于写入数据
    
    DELETE /titles
    DELETE /titles2
    PUT /titles
    {
      "mappings": {
        "properties": {
          "title": {
            "type": "text",
            "analyzer": "english"
          }
        }
      }
    }
    PUT /titles2
    {
      "mappings": {
        "properties": {
          "title": {
            "type": "text",
            "analyzer": "english",
            "fields": {"std": {"type": "text","analyzer": "standard"}}
          }
        }
      }
    }
    POST titles/_bulk
    { "index": { "_id": 1 }}
    { "title": "My dog barks" }
    { "index": { "_id": 2 }}
    { "title": "I see a lot of barking dogs on the road " }
    POST titles2/_bulk
    { "index": { "_id": 3 }}
    { "title": "My dog barks" }
    { "index": { "_id": 4 }}
    { "title": "I see a lot of barking dogs on the road " }
    POST _aliases
    {
      "actions": [
        {
          "add": {
            "index": "titles",
            "alias": "aliastitle"
          }
        }
      ]
    }
    POST _aliases
    {
      "actions": [
        {
          "add": {
            "index": "titles2",
            "alias": "aliastitle"
          }
        }
      ]
    }
    GET /aliastitle/_search
    {
       "query": {
            "multi_match": {
                "query": "barking dogs",
                "type": "most_fields",
                "fields": [ "title", "title.std" ]
            }
        }
    }
    GET /aliastitle/_search
    {
       "query": {
            "multi_match": {
                "query": "barking dogs",
                "type": "most_fields",
                "fields": [ "title^10", "title.std" ]
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:4.9-使用SearchTemplate和IndexAlias进

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