elasticsearch使用进阶

  1. head插件安装及其使用 安装流程: 安装nodejs
    安装grunt 安装head:https://github.com/mobz/elasticsearch-head npm run start http://localhost:9100 测试数据
POST /products/_bulk
{ "index": {"_id":1}}
  {"title":"iphone12 pro","price":8999,"created_at":"2020-10-23","description":"iPhone 12 Pro采用超瓷晶面板和亚光质感玻璃背板,搭配不锈钢边框,有银色、石墨色、金色、海蓝色四种颜色。宽度:71.5毫米,高度:146.7毫米,厚度:7.4毫米,重量:187克"}
{ "index": {"_id":2}}
  {"title":"iphone12","price":4999,"created_at":"2020-10-23","description":"iPhone 12 高度:146.7毫米;宽度:71.5毫米;厚度:7.4毫米;重量:162克(5.73盎司) [5]  。iPhone 12设计采用了离子玻璃,以及7000系列铝金属外壳。"}
{ "index": {"_id":3}}
  {"title":"iphone13","price":6000,"created_at":"2021-09-15","description":"iPhone 13屏幕采用6.1英寸OLED屏幕;高度约146.7毫米,宽度约71.5毫米,厚度约7.65毫米,重量约173克。"}
{ "index": {"_id":4}}
  {"title":"iphone13 pro","price":8999,"created_at":"2021-09-15","description":"iPhone 13Pro搭载A15 Bionic芯片,拥有四种配色,支持5G。有128G、256G、512G、1T可选,售价为999美元起。"}
{ "index": {"_id":5}}
  {"title":"iphone14 pro","price":8999,"created_at":"2021-09-15","description":"iPhone 13Pro搭载A16 Bionic芯片,拥有四种配色,支持5G。有128G、256G、512G、1T可选,售价为999美元起。"}
{ "index": {"_id":6}}
  {"title":"iphone14 pro max","price":9999,"created_at":"2021-09-15","description":"iPhone 14ProMax搭载A16 Bionic芯片,拥有四种配色,支持5G。有128G、256G、512G、1T可选,售价为999美元起。"}
{ "index": {"_id":7}}
  {"title":"iphone12","price":6499,"created_at":"2020-10-23","description":"iPhone 12 采用超瓷晶面板和亚光质感玻璃背板,搭配不锈钢边框,有银色、石墨色、金色、海蓝色四种颜色。宽度:71.5毫米,高:146.7毫米,厚度:7.4毫米,重量:187克"}

2. DSL之常见的Query

查询所有[match_all]

match_all关键字: 返回索引中的全部文档

GET /products/_search
{
  "query": {
    "match_all": {}
  }
}

###关键词查询(term) term 关键字: 用来使用关键词查询 Keyword integer double 不分词 text类型 会进行分词,默认是es标准分词器

GET /products/_search
{
 "query": {
   "term": {
     "price": {
       "value": 4999
     }
   }
 }
}

注意:

  1. 通过使用term查询得知ES中默认使用分词器为标准分词器(StandardAnalyzer),标准分词器对于英文单词分词,对于中文单字分词。
  2. 通过使用term查询得知,在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 这些类型不分词,只有text类型分词。

范围查询[range]

range 关键字: 用来指定查询指定范围内的文档

GET /products/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 1400,
        "lte": 9999
      }
    }
  }
}

###前缀查询[prefix] prefix 关键字: 用来检索含有指定前缀的关键词的相关文档

GET /products/_search
{
  "query": {
    "prefix": {
      "title": {
        "value": "ipho"
      }
    }
  }
}

通配符查询[wildcard]

wildcard 关键字: 通配符查询 ? 用来匹配一个任意字符 * 用来匹配多个任意字符

GET /products/_search
{
  "query": {
    "wildcard": {
      "description": {
        "value": "iphon*"
      }
    }
  }
}

###多id查询[ids] ids 关键字 : 值为数组类型,用来根据一组id获取多个对应的文档

GET /products/_search
{
  "query": {
    "ids": {
      "values": ["1","2"]
    }
  }
}

###模糊查询[fuzzy] fuzzy 关键字: 用来模糊查询含有指定关键字的文档

GET /products/_search
{
  "query": {
    "fuzzy": {
      "description": "iphooone"
    }
  }
}

注意: fuzzy 模糊查询 最大模糊错误 必须在0-2之间

  • 搜索关键词长度为 2 不允许存在模糊
  • 搜索关键词长度为3-5 允许一次模糊
  • 搜索关键词长度大于5 允许最大2模糊

###布尔查询[bool] bool 关键字: 用来组合多个条件实现复杂查询 must: 相当于&& 同时成立 should: 相当于|| 成立一个就行 must_not: 相当于! 不能满足任何一个

GET /products/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "price": {
            "value": 4999
          }
        }}
      ]
    }
  }
}

多字段查询[multi_match]

GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iphone13 毫",
      "fields": ["title","description"]
    }
  }
}

注意: 将query的字段类型进行分词,将分词之后词进行查询该字段 如果该字段不分词就会将查询条件作为整体进行查询 ###默认字段分词查询[query_string]

GET /products/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "屏幕真的非常不错"
    }
  }
}

注意: 查询字段分词就将查询条件分词查询 查询字段不分词将查询条件不分词查询 ###高亮查询[highlight] highlight 关键字: 可以让符合条件的文档中的关键词高亮

GET /products/_search
{
  "query": {
    "term": {
      "description": {
        "value": "iphone"
      }
    }
  },
  "highlight": {
    "fields": {
      "*":{}
    }
  }
}

自定义高亮html标签: 可以在highlight中使用pre_tags和post_tags

GET /products/_search
{
  "query": {
    "term": {
      "description": {
        "value": "iphone"
      }
    }
  },
  "highlight": {
    "post_tags": ["</span>"], 
    "pre_tags": ["<span style='color:red'>"],
    "fields": {
      "*":{}
    }
  }
}

多字段高亮 使用require_field_match开启多个字段高亮

GET /products/_search
{
  "query": {
    "term": {
      "description": {
        "value": "iphone"
      }
    }
  },
  "highlight": {
    "require_field_match": "false",
    "post_tags": ["</span>"], 
    "pre_tags": ["<span style='color:red'>"],
    "fields": {
      "*":{}
    }
  }
}

###返回指定条数[size] size 关键字: 指定查询结果中返回指定条数。 默认返回值10条

GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "size": 5
}

###分页查询[from] from 关键字: 用来指定起始返回位置,和size关键字连用可实现分页效果

GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "size": 5,
  "from": 0
}

###指定字段排序[sort]

GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

###返回指定字段[_source] _source 关键字: 是一个数组,在数组中用来指定展示那些字段

GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["title","description"]
}

###部分更新[_update]

POST /products/_update/1
{
    "doc": {
        "price": 8888
    }
}

###查询更新[_update_by_query]

POST /products/_update_by_query
{
    "query": {
        "match": {
            "_id": 1
        }
    },
    "script": {
        "source": "ctx._source.price = 8899"
    }
}

###批量操作[bulk]

POST _bulk
{"index":{"_index":"products", "_id":3}}
{"title":"iphone12 pro","price":89990,"created_at":"2020-10-23","description":"iPhone 12 Pro采用超瓷晶面板和亚光质感玻璃背板,搭配不锈钢边框,有银色、石墨色、金色、海蓝色四种颜色。宽度:71.5毫米,高度:146.7毫米,厚度:7.4毫米,重量:187克"}

###批量删除

POST _bulk
{"delete":{"_index":"products", "_id":3}}
{"delete":{"_index":"products", "_id":4}}

###批量创建

POST _bulk
{"create":{"_index":"products", "_id":3}} 
{"title":"iphone12 pro","price":8999,"created_at":"2020-10-23","description":"iPhone 12 Pro采用超瓷晶面板和亚光质感玻璃背板,搭配不锈钢边框,有银色、石墨色、金色、海蓝色四种颜色。宽度:71.5毫米,高度:146.7毫米,厚度:7.4毫米,重量:187克"}

主键重复会报错

###批量局部更新

POST _bulk
{"update":{"_index":"products","_id":4}}
{"doc": {"title":"iphone12 pro","price":8999}}

##3. 聚合查询

聚合:英文为Aggregation,是es除搜索功能外提供的针对es数据做统计分析的功能。 聚合有助于根据搜索查询提供聚合数据。聚合查询是数据库中重要的功能特性,ES作为搜索引擎兼数据库,同样提供了强大的聚合分析能力。它基于查询条件来对数据进行分桶、计算的方法。有点类似于 SQL 中的 group by 再加一些函数方法的操作。 注意事项:text类型是不支持聚合的。 测试数据 创建索引 index 和映射 mapping

PUT /fruit
{
  "mappings": {
    "properties": {
      "title":{
        "type": "keyword"
      },
      "price":{
        "type":"double"
      },
      "description":{
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
POST /fruit/_bulk
{"index":{}}
{"title":"面包","price":19.9,"description":"小面包非常好吃"}
{"index":{}}
{"title":"旺仔牛奶","price":29.9,"description":"非常好喝"}
{"index":{}}
{"title":"豆本豆","price":19.9,"description":"豆本豆非常好吃"}
{"index":{}}
{"title":"小馒头","price":19.9,"description":"小馒头非常好吃"}
{"index":{}}
{"title":"大辣片","price":39.9,"description":"大辣片非常好吃"}
{"index":{}}
{"title":"王老吉","price":9.9,"description":"透心凉非常好喝"}
{"index":{}}
{"title":"小浣熊","price":19.9,"description":"童年的味道"}
{"index":{}}
{"title":"海苔","price":19.9,"description":"海的味道"}

###根据某个字段进行分组 统计数量 比如统计“好吃” 好评的数量

GET /fruit/_search
{
  "query": {
    "term": {
      "description": {
        "value": "好吃"
      }
    }
  }, 
  "aggs": {
    "price_group": {
      "terms": {
        "field": "price"
      }
    }
  }
}

###求最大值

GET /fruit/_search
{
  "aggs": {
    "price_max": {
      "max": {
        "field": "price"
      }
    }
  }
}

###求最小值

GET /fruit/_search
{
  "aggs": {
    "price_min": {
      "min": {
        "field": "price"
      }
    }
  }
}

###求平均值

GET /fruit/_search { "aggs": { "price_agv": { "avg": { "field": "price" } } } }

###求和

GET /fruit/_search
{
  "aggs": {
    "price_sum": {
      "sum": {
        "field": "price"
      }
    }
  }
}

准备案例数据 Mappings

PUT /cars
{
    "mappings": {
        "properties": {
            "price": {
                "type": "long"
            },
            "color": {
                "type": "keyword"
            },
            "brand": {
                "type": "keyword"
            },
            "model": {
                "type": "keyword"
            },
            "sold_date": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            },
            "remark": {
                "type": "text",
                "analyzer": "ik_max_word"
            }
        }
    }
}

bluck插入数据

POST /cars/_bulk
{"index":{}}
{"price":258000,"color":"金色","brand":"大众","model":"大众迈腾","sold_date":"2021-10-22","remark":"大众中档车"}
{"index":{}}
{"price":123000,"color":"金色","brand":"大众","model":"大众速腾","sold_date":"2021-11-05","remark":"大众神车"}
{"index":{}}
{"price":239800,"color":"白色","brand":"标志","model":"标志508","sold_date":"2021-05-18","remark":"标志品牌全球上市车型"}
{"index":{}}
{"price":148800,"color":"白色","brand":"标志","model":"标志408","sold_date":"2021-07-02","remark":"比较大的紧凑型车"}
{"index":{}}
{"price":1998000,"color":"黑色","brand":"大众","model":"大众辉腾","sold_date":"2021-08-19","remark":"大众最让人肝疼的车"}
{"index":{}}
{"price":218000,"color":"红色","brand":"奥迪","model":"奥迪A4","sold_date":"2021-11-06","remark":"小资车型"}
{"index":{}}
{"price":489000,"color":"黑色","brand":"奥迪","model":"奥迪A6","sold_date":"2022-01-01","remark":"政府专用?"}
{"index":{}}
{"price":1899000,"color":"黑色","brand":"奥迪","model":"奥迪A8","sold_date":"2022-02-12","remark":"很贵的大A6。。。"}

###统计某个维度平均价格

GET cars/_search
{
    "size":0,
    "aggs": {
        "group_by_color": {
            "terms": {
                "field": "color",
                "order": {
                    "avg_by_price": "asc"
                }
            },
            "aggs": {
                "avg_by_price": {
                    "avg": {
                        "field": "price"
                    }
                }
            }
        }
    }
}

设置size为0,不返回原始数据

###统计某个维度最大和最小价格、总价

```json
GET cars/_search
{
  "aggs": {
    "group_by_color": {
      "terms": {
        "field": "color"
      },
      "aggs": {
        "max_price": {
          "max": {
            "field": "price"
          }
        },
        "min_price": {
          "min": {
            "field": "price"
          }
        },
        "sum_price": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

###根据某个维度统计销售(月季年)

GET cars/_search
{
   "size" : 0,
   "aggs": {
      "sales": {
         "date_histogram": {
            "field": "sold_date",
            "calendar_interval": "month", //按月划分 year quarter day  34d
            "format": "yyyy-MM-dd",
            "min_doc_count" : 0,
            "extended_bounds" : {    //时间限制
                "min" : "2021-01-01",
                "max" : "2023-12-31"
            }
         },
         "aggs": {
            "per_brand_sum": {
               "terms": {
                  "field": "brand"
               },
               "aggs": {
                  "sum_price": {
                     "sum": { "field": "price" } 
                  }
               }
            },
            "total_sum": {
               "sum": { "field": "price" } 
            }
         }
      }
   }
}

打 赏