官方给的方法没啥乱用,加个参数解决unless the include_type_name parameter is set to true问题

1、测试的三条商品信息

#新增第一个商品
PUT /ecommerce/product/1
{
  "name":"高露洁牙膏",
  "desc":"高效、美白、好用",
  "price":33.5,
  "producer":"高露洁生产厂家",
  "short":"gljyg",
  "tags":["防止蛀牙","美白"]
}
#新增第二个商品
PUT /ecommerce/product/2
{
  "name":"佳洁士牙膏",
  "desc":"高效、好用",
  "price":25,
  "producer":"高露洁生产厂家",
  "short":"jjsyg",
  "tags":["防止蛀牙"]
}
#新增第三个商品
PUT /ecommerce/product/3
{
  "name":"中华牙膏",
  "desc":"高效、美白",
  "price":18.6,
  "producer":"高露洁生产厂家",
  "short":"zhyg",
  "tags":["清新","美白"]
}

2、查询上面添加的三条数据

#查询所有的商品
GET /ecommerce/product/_search
{
  "query":{
    "match_all": {}
  }
}

2.1、查询结果

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ecommerce",
        "_type" : "product",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "中华牙膏",
          "desc" : "高效、美白",
          "price" : 18.6,
          "producer" : "高露洁生产厂家",
          "short" : "zhyg",
          "tags" : [
            "清新",
            "美白"
          ]
        }
      },
      {
        "_index" : "ecommerce",
        "_type" : "product",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "佳洁士牙膏",
          "desc" : "高效、好用",
          "price" : 25,
          "producer" : "高露洁生产厂家",
          "short" : "jjsyg",
          "tags" : [
            "防止蛀牙"
          ]
        }
      },
      {
        "_index" : "ecommerce",
        "_type" : "product",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "高露洁牙膏",
          "desc" : "高效、美白、好用",
          "price" : 33.5,
          "producer" : "高露洁生产厂家",
          "short" : "gljyg",
          "tags" : [
            "防止蛀牙",
            "美白"
          ]
        }
      }
    ]
  }
}

3、查询所有的商品,按照价格排序

GET /ecommerce/product/_search
{
  "query":{
    "match_all": {}
  },
  "sort":{
    "price":{
    "order":"asc"}}
}

3.1、错误信息

Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [price] in order to load field data by uninverting the inverted index. Note that this can use significant memory.

3.2、官方文档给的解决办法

https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

3.2.1、官方给的解决办法
PUT /ecommerce/product/_mapping
{
  "properties": {
    "price": { 
      "type": "text",
      "fielddata": true
    }
  }
}

事实证明官方给的方法没啥乱用只解决了一半的问题,并无卵用

3.2.2、include_type_name parameter问题
Types cannot be provided in put mapping requests, unless the include_type_name parameter is set to true.

错误提示告诉我们要加一个include_type_name参数。

4、加上以上参数的解决办法

PUT /ecommerce/product/_mapping?include_type_name=true
{
  "properties": {
    "price": { 
      "type": "text",
      "fielddata": true
    }
  }
}

按照这个错误提示,完美解决上面的问题。

你可能感兴趣的:(爬坑大法)