升级 Elasticsearch 2.2 到 5.2

elasticsearch 5.2在1月底release了,parana使用的是2.2版本,近日已升级到5.2.release,下面简单讲述一下升级需要改动的一些地方。

Index Level Settings

在5.0之前允许在elasticsearch.yml设置Index Level Settings,在5.0之后做了限制,只允许部分特定的配置项。parana之前在elasticsearch.yml配置的这段settings就需要搬家了:

index:  
  "analysis": {
    "tokenizer": {
      "ngram_tokenizer": {
        "type": "nGram",
        "min_gram": 1,
        "max_gram": 10
      }
    },
    "analyzer": {
      "ngram_tokenizer_analyzer": {
        "type": "custom",
        "tokenizer": "ngram_tokenizer"
      }
    }
  }

我们可以使用index templates 来达到相同目的

curl -XPUT 'localhost:9200/_template/parana_template?pretty' -H 'Content-Type: application/json' -d'  
{
  "template": "*",
  "settings": {
    "analysis": {
      "tokenizer": {
        "ngram_tokenizer": {
          "type": "nGram",
          "min_gram": 1,
          "max_gram": 10
        }
      },
      "analyzer": {
        "ngram_tokenizer_analyzer": {
          "type": "custom",
          "tokenizer": "ngram_tokenizer"
        }
      }
    }
  }
}'

Mapping

string数据类型被text和keyword代替,其中text用于要分词的字段,keyword则是用于不需分词的字段。下面分别对比一下升级前后parana item_mapping的配置

  • 升级前
   {
  "item": {
    "_all": {
      "enabled": false
    },
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "string",
        "analyzer": "ngram_tokenizer_analyzer",
        "search_analyzer": "ngram_tokenizer_analyzer",
        "index_options": "offsets"
      },
      "itemCode": {
        "type": "string",
        "index": "not_analyzed"
      },
      "shopId": {
        "type": "long"
      },
      "spuId": {
        "type": "long"
      },
      "shopName": {
        "type": "string",
        "index": "no"
      },
      "brandId": {
        "type": "long"
      },
      "brandName": {
        "type": "string",
        "index": "not_analyzed"
      },
      "brandLogo": {
        "type": "string",
        "index": "no"
      },
      "mainImage": {
        "type": "string",
        "index": "no"
      },
      "price": {
        "type": "integer"
      },
      "stockQuantity": {
        "type": "integer"
      },
      "saleQuantity": {
         "type": "integer"
      },
      "status": {
         "type": "integer"
      },
      "specification":{
        "type": "string",
        "index": "not_analyzed"
      },
      "type": {
        "type": "integer"
      },
      "categoryIds":{
        "type": "integer"
      },
      "attributes": {
        "type": "string",
        "index": "not_analyzed"
      },
      "shopCategoryIds":{
        "type": "integer"
      },
      "promotionTypes":{
        "type": "integer"
      },
      "updatedAt": {
        "type": "date"
      }
    }
  }
}
  • 升级后
  {
  "item": {
    "_all": {
      "enabled": false
    },
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "text",
        "analyzer": "ngram_tokenizer_analyzer",
        "search_analyzer": "ngram_tokenizer_analyzer",
        "index_options": "offsets"
      },
      "itemCode": {
        "type": "keyword"
      },
      "shopId": {
        "type": "long"
      },
      "spuId": {
        "type": "long"
      },
      "shopName": {
        "type": "keyword",
        "index": false
      },
      "brandId": {
        "type": "long"
      },
      "brandName": {
        "type": "keyword"
      },
      "brandLogo": {
        "type": "keyword",
        "index": false
      },
      "mainImage": {
        "type": "keyword",
        "index": false
      },
      "price": {
        "type": "integer"
      },
      "stockQuantity": {
        "type": "integer"
      },
      "saleQuantity": {
        "type": "integer"
      },
      "status": {
        "type": "integer"
      },
      "specification":{
        "type": "keyword"
      },
      "type": {
        "type": "integer"
      },
      "categoryIds":{
        "type": "integer"
      },
      "attributes": {
        "type": "keyword"
      },
      "shopCategoryIds":{
        "type": "integer"
      },
      "promotionTypes":{
        "type": "integer"
      },
      "updatedAt": {
        "type": "date"
      }
    }
  }
}

5.2其实兼容2.2版本的mapping,但既然换汤了,还是顺便换一下药吧。

Search and Query DSL

由于filtered被废弃了,需要用bool代替,所以我们的查询模板也得调整一下

  • 升级前
{
  "from":"{{from}}",
  "size":"{{size}}",
  "query": {
     "filtered": {
        "query": {
            {{#keyword}}
            {{#multiField}}
            "bool": {
              "should": [
                 {{#fields}}
                 {"match": { "{{field}}": "{{value}}" }} {{^last}},{{/last}}
                 {{/fields}}
               ]
            }
            {{/multiField}}
            {{^multiField}}
                {{#fields}}
                "match": { "{{field}}": "{{value}}" }
                {{/fields}}
            {{/multiField}}
            {{/keyword}}
            {{^keyword}}
            "match_all":{}
            {{/keyword}}
        },
        "filter": {
          "bool": {
            "must": [
              {{#term}}
                  {"term": {"{{field}}":"{{value}}"}} {{^last}},{{/last}}
              {{/term}}
              {{#terms}}
                  {"terms":
                    {"{{field}}":
                       [ {{#values}}
                          "{{value}}" {{^last}},{{/last}}
                         {{/values}}
                        ]
                    }
                  }{{^last}},{{/last}}
              {{/terms}}
              {{#ranges}}
                  {"range": {
                      "{{field}}": {
                         {{#low}}
                             "gte": "{{low}}"
                             {{#high}},{{/high}}
                         {{/low}}
                         {{#high}}
                             "lte": "{{high}}"
                         {{/high}}
                      }
                     }
                  } {{^last}},{{/last}}
              {{/ranges}}
            ]
          }
        }
     }
  }
  {{#hasSort}}
  ,"sort": [
     {{#sorts}}
         {"{{field}}": {"order":"{{order}}"}} {{^last}},{{/last}}
     {{/sorts}}
  ]
  {{/hasSort}}
  {{#hasAggs}}
  ,"aggs": {
     {{#aggs}}
        "{{id}}": {"terms": {"field": "{{field}}", "size": "{{size}}" }} {{^last}},{{/last}}
     {{/aggs}}
  }
  {{/hasAggs}}
  {{#hasHighlight}}
  ,"highlight": {
     "fields": {
         {{#highlights}}
           "{{field}}": {} {{^last}},{{/last}}
         {{/highlights}}
       }
  }
  {{/hasHighlight}}
}
  • 升级后
 {
  "from":"{{from}}",
  "size":"{{size}}",
  "query": {
     "bool": {
        "must": {
            {{#keyword}}
            {{#multiField}}
            "bool": {
              "should": [
                 {{#fields}}
                 {"match": { "{{field}}": "{{value}}" }} {{^last}},{{/last}}
                 {{/fields}}
               ]
            }
            {{/multiField}}
            {{^multiField}}
                {{#fields}}
                "match": { "{{field}}": "{{value}}" }
                {{/fields}}
            {{/multiField}}
            {{/keyword}}
            {{^keyword}}
            "match_all":{}
            {{/keyword}}
        },
        "filter": {
          "bool": {
            "must": [
              {{#term}}
                  {"term": {"{{field}}":"{{value}}"}} {{^last}},{{/last}}
              {{/term}}
              {{#terms}}
                  {"terms":
                    {"{{field}}":
                       [ {{#values}}
                          "{{value}}" {{^last}},{{/last}}
                         {{/values}}
                        ]
                    }
                  }{{^last}},{{/last}}
              {{/terms}}
              {{#ranges}}
                  {"range": {
                      "{{field}}": {
                         {{#low}}
                             "gte": "{{low}}"
                             {{#high}},{{/high}}
                         {{/low}}
                         {{#high}}
                             "lte": "{{high}}"
                         {{/high}}
                      }
                     }
                  } {{^last}},{{/last}}
              {{/ranges}}
            ]
          }
        }
     }
  }
  {{#hasSort}}
  ,"sort": [
     {{#sorts}}
         {"{{field}}": {"order":"{{order}}"}} {{^last}},{{/last}}
     {{/sorts}}
  ]
  {{/hasSort}}
  {{#hasAggs}}
  ,"aggs": {
     {{#aggs}}
        "{{id}}": {"terms": {"field": "{{field}}", "size": "{{size}}" }} {{^last}},{{/last}}
     {{/aggs}}
  }
  {{/hasAggs}}
  {{#hasHighlight}}
  ,"highlight": {
     "fields": {
         {{#highlights}}
           "{{field}}": {} {{^last}},{{/last}}
         {{/highlights}}
       }
  }
  {{/hasHighlight}}
}

至此,升级就算完成了,更多的breaking changes请移步官方文档

郭超鹏

Read more posts by this author.

Subscribe to The Terminus Blog

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!