今天要比昨天吃苦的能力更强

Elasticsearch深入10

统计每个国家的喜欢每种爱好的员工有多少个,每个国家有多少个办公区一共有多少人统计喜欢那个爱好的有多少人

GET /company/rd_center/_search

{

  "size": 0,

  "aggs": {

    "group_by_country": {

      "terms": {

        "field": "country.keyword"

      },

      "aggs": {

        "group_by_child_employee": {

          "children": {

            "type": "employee"

          },

          "aggs": {

            "group_by_hobby": {

              "terms": {

                "field": "hobby.keyword"

              }}}}  }  } }}

祖孙三层数据关系建模以及搜索实战

父子关系,祖孙三层关系的数据建模,搜索

 

PUT /company

{

  "mappings": {

    "country": {},

    "rd_center": {

      "_parent": {

        "type": "country"

      }

    },

    "employee": {

      "_parent": {

        "type": "rd_center"

      }}}}

 

country -> rd_center -> employee,祖孙三层数据模型

POST /company/country/_bulk

{ "index": { "_id": "1" }}

{ "name": "中国" }

{ "index": { "_id": "2" }}

{ "name": "美国" }

 

POST /company/rd_center/_bulk

{ "index": { "_id": "1", "parent": "1" }}

{ "name": "北京研发总部" }

{ "index": { "_id": "2", "parent": "1" }}

{ "name": "上海研发中心" }

{ "index": { "_id": "3", "parent": "2" }}

{ "name": "硅谷人工智能实验室" }

 

PUT /company/employee/1?parent=1&routing=1

{

  "name":  "张三",

  "dob":   "1970-10-24",

  "hobby": "爬山"

}

 routing参数的讲解,必须跟grandparent相同,否则有问题

country,用的是自己的id去路由; rd_center,parent,用的是country的id去路由; employee,如果也是仅仅指定一个parent,那么用的是rd_center的id去路由,这就导致祖孙三层数据不会在一个shard上

孙子辈儿,要手动指定routing,指定为爷爷辈儿的数据的id

搜索有爬山爱好的员工所在的国家

GET /company/country/_search

{

  "query": {

    "has_child": {

      "type": "rd_center",

      "query": {

        "has_child": {

          "type": "employee",

          "query": {

            "match": {

              "hobby": "爬山"

            }}}}}}}

 基于completion suggest实现搜索提示

suggest,completion suggest,自动完成,搜索推荐,搜索提示 --> 自动完成,auto completion

auto completion

比如说我们在百度,搜索,你现在搜索“大话西游” -->

百度,自动给你提示,“大话西游电影”,“大话西游小说”, “大话西游手游”

不用你把所有你想要输入的文本都输入完,搜索引擎会自动提示你可能是你想要搜索的那个文本

PUT /news_website

{

  "mappings": {

    "news" : {

      "properties" : {

        "title" : {

          "type": "text",

          "analyzer": "ik_max_word",

          "fields": {

            "suggest" : {

              "type" : "completion",

              "analyzer": "ik_max_word"

            }

          }

        },

        "content": {

          "type": "text",

          "analyzer": "ik_max_word"

        }}}}}

 

completion,es实现的时候,是非常高性能的,会构建不是倒排索引,也不是正拍索引,就是纯的用于进行前缀搜索的一种特殊的数据结构,而且会全部放在内存中,所以auto completion进行的前缀搜索提示,性能是非常高的

大话西游

PUT /news_website/news/1

{

  "title": "大话西游电影",

  "content": "大话西游的电影时隔20年即将在2017年4月重映"

}

PUT /news_website/news/2

{

  "title": "大话西游小说",

  "content": "某知名网络小说作家已经完成了大话西游同名小说的出版"

}

PUT /news_website/news/3

{

  "title": "大话西游手游",

  "content": "网易游戏近日出品了大话西游经典IP的手游,正在火爆内测中"

}

GET /news_website/news/_search

{

  "suggest": {

    "my-suggest" : {

      "prefix" : "大话西游",

      "completion" : {

        "field" : "title.suggest"

      }

    }

  }

}

使用动态映射模板定制自己的映射策略

 高级的用法

比如说,我们本来没有某个type,或者没有某个field,但是希望在插入数据的时候,es自动为我们做一个识别,动态映射出这个type的mapping,包括每个field的数据类型,一般用的动态映射,dynamic mapping

这里有个问题,如果说,我们其实对dynamic mapping有一些自己独特的需求,比如说,es默认来说,如经过识别到一个数字,field: 10,默认是搞成这个field的数据类型是long,再比如说,如果我们弄了一个field : "10",默认就是text,还会带一个keyword的内置field。我们没法改变。

但是我们现在就是希望动态映射的时候,根据我们的需求去映射,而不是让es自己按照默认的规则去玩儿

dyanmic mapping template,动态映射模板

我们自己预先定义一个模板,然后插入数据的时候,相关的field,如果能够根据我们预先定义的规则,匹配上某个我们预定义的模板,那么就会根据我们的模板来进行mapping,决定这个Field的数据类型

0、默认的动态映射的效果咋样

DELETE /my_index

PUT /my_index/my_type/1

{

  "test_string": "hello world",

  "test_number": 10

}

es的自动的默认的,动态映射是咋样的。。。

GET /my_index/_mapping/my_type

 

{

  "my_index": {

    "mappings": {

      "my_type": {

        "properties": {

          "test_number": {

            "type": "long"

          },

          "test_string": {

            "type": "text",

            "fields": {

              "keyword": {

                "type": "keyword",

                "ignore_above": 256

              }}}}}}}}

 

这个就是es的默认的动态映射规则,可能就不是我们想要的。。。

我们比如说,现在想要的效果是啥。。。

test_number,如果是个数字,我们希望默认就是integer类型的

test_string,如果是字符串,我们希望默认是个text,这个没问题,但是内置的field名字,叫做raw,不叫座keyword,类型还是keyword,保留500个字符

 

1、根据类型匹配映射模板

动态映射模板,有两种方式,第一种,是根据新加入的field的默认的数据类型,来进行匹配,匹配上某个预定义的模板;第二种,是根据新加入的field的名字,去匹配预定义的名字,或者去匹配一个预定义的通配符,然后匹配上某个预定义的模板

PUT my_index

{

  "mappings": {

    "my_type": {

      "dynamic_templates": [

        {

          "integers": {

            "match_mapping_type": "long",

            "mapping": {

              "type": "integer"

            }

          }

        },

        {

          "strings": {

            "match_mapping_type": "string",

            "mapping": {

              "type": "text",

              "fields": {

                "raw": {

                  "type": "keyword",

                  "ignore_above": 500

                }}}}}] }}}

 

PUT /my_index/my_type/1

{

  "test_long": 1,

  "test_string": "hello world"

}

 

{

  "my_index": {

    "mappings": {

      "my_type": {

        "dynamic_templates": [

          {

            "integers": {

              "match_mapping_type": "long",

              "mapping": {

                "type": "integer"

              }

            }

          },

          {

            "strings": {

              "match_mapping_type": "string",

              "mapping": {

                "fields": {

                  "raw": {

                    "ignore_above": 500,

                    "type": "keyword"

                  }},

                "type": "text"

              }}}],

        "properties": {

          "test_number": {

            "type": "integer"

          },

          "test_string": {

            "type": "text",

            "fields": {

              "raw": {

                "type": "keyword",

                "ignore_above": 500

              }}}}}}}

 

2、根据字段名配映射模板

PUT /my_index

{

  "mappings": {

    "my_type": {

      "dynamic_templates": [

        {

          "string_as_integer": {

            "match_mapping_type": "string",

            "match": "long_*",

            "unmatch": "*_text",

            "mapping": {

              "type": "integer"

            }

          }}] }}}

举个例子,field : "10",把类似这种field,弄成long型

{

  "my_index": {

    "mappings": {

      "my_type": {

        "dynamic_templates": [

          {

            "string_as_integer": {

              "match": "long_*",

              "unmatch": "*_text",

              "match_mapping_type": "string",

              "mapping": {

                "type": "integer"

              }}}],

        "properties": {

          "long_field": {

            "type": "integer"

          },

          "long_field_text": {

            "type": "text",

            "fields": {

              "keyword": {

                "type": "keyword",

                "ignore_above": 256

              }}}}}}}}

 

场景,有些时候,dynamic mapping + template,每天有一堆日志,每天有一堆数据

这些数据,每天的数据都放一个新的type中,每天的数据都会哗哗的往新的tye中写入,此时你就可以定义一个模板,搞一个脚本,每天都预先生成一个新type的模板,里面讲你的各个Field都匹配到一个你预定义的模板中去,就好了

学习使用geo point地理位置数据类型

这一讲开始,后面会跟着几讲内容,将地理位置相关的知识给大家讲解一下

主要是es支持基于地理位置的搜索,和聚合分析的

举个例子,比如说,我们后面就会给大家演示一下,你现在如果说做了一个酒店o2o app,让你的用户在任何地方,都可以根据当前所在的位置,找到自己身边的符合条件的一些酒店,那么此时就完全可以使用es来实现,非常合适

我现在在上海某个大厦附近,我要搜索到距离我2公里以内的5星级的带游泳池的一个酒店s,用es就完全可以实现类似这样的基于地理位置的搜索引擎

1、建立geo_point类型的mapping

 

第一个地理位置的数据类型,就是geo_point,geo_point,说白了,就是一个地理位置坐标点,包含了一个经度,一个维度,经纬度,就可以唯一定位一个地球上的坐标

PUT /my_index

{

  "mappings": {

    "my_type": {

      "properties": {

        "location": {

          "type": "geo_point"

        }}}}}

 

2、写入geo_point的3种方法

PUT my_index/my_type/1

{

  "text": "Geo-point as an object",

  "location": {

    "lat": 41.12,

    "lon": -71.34

  }

}latitude:维度

longitude:经度

我们这里就不用去关心,这些坐标到底代表什么地方,其实都是我自己随便写的,只要能够作为课程,给大家演示清楚就可以了,自己去找一些提供地理位置的一些公司,供应商,api,百度地图,也是提供各个地方的经纬度的

不建议用下面两种语法

PUT my_index/my_type/2

{

  "text": "Geo-point as a string",

  "location": "41.12,-71.34"

}

 

PUT my_index/my_type/4

{

  "text": "Geo-point as an array",

  "location": [ -71.34, 41.12 ]

}

 

3、根据地理位置进行查询

 

最最简单的,根据地理位置查询一些点,比如说,下面geo_bounding_box查询,查询某个矩形的地理位置范围内的坐标点

 

GET /my_index/my_type/_search

{

  "query": {

    "geo_bounding_box": {

      "location": {

        "top_left": {

          "lat": 42,

          "lon": -72

        },

        "bottom_right": {

          "lat": 40,

          "lon": -74

        }}}}}

比如41.12,-71.34就是一个酒店,然后我们现在搜索的是从42,-72(代表了大厦A)和40,-74(代表了马路B)作为矩形的范围,在这个范围内的酒店,是什么

搜索案例以及搜索指定区域内的酒店

稍微真实点的案例,酒店o2o app作为一个背景,用各种各样的方式,去搜索你当前所在的地理位置附近的酒店

 

搜索指定区域范围内的酒店,比如说,我们可以在搜索的时候,指定两个地点,就要在东方明珠大厦和上海路组成的矩阵的范围内,搜索我想要的酒店

 

PUT /hotel_app

{

    "mappings": {

        "hotels": {

            "properties": {

                "pin": {

                    "properties": {

                        "location": {

                            "type": "geo_point"

                        }}}}}}}

 

PUT /hotel_app/hotels/1

{

    "name": "喜来登大酒店",

    "pin" : {

        "location" : {

            "lat" : 40.12,

            "lon" : -71.34

        }}}

 

GET /hotel_app/hotels/_search

{

  "query": {

    "bool": {

      "must": [

        {"match_all": {}}

      ],

      "filter": {

        "geo_bounding_box": {

          "pin.location": {

            "top_left" : {

                "lat" : 40.73,

                "lon" : -74.1

            },

            "bottom_right" : {

                "lat" : 40.01,

                "lon" : -71.12

            }}}}}}}

 

GET /hotel_app/hotels/_search

{

  "query": {

    "bool": {

      "must": [

        {

          "match_all": {}

        }

      ],

      "filter": {

        "geo_polygon": {

          "pin.location": {

            "points": [

              {"lat" : 40.73, "lon" : -74.1},

              {"lat" : 40.01, "lon" : -71.12},

              {"lat" : 50.56, "lon" : -90.58}

            ] }}}}}}

 

我们现在要指定东方明珠大厦,上海路,上海博物馆,这三个地区组成的多边形的范围内,我要搜索这里面的酒店

 

 

实战搜索距离当前位置一定范围内的酒店

 酒店o2o app,作为案例背景

 比如说,现在用户,所在的位置,是个地理位置的坐标,我是知道我的坐标的,app是知道的,android,地理位置api,都可以拿到当前手机app的经纬度

 我说,我现在就要搜索出,举例我200m,或者1公里内的酒店

 GET /hotel_app/hotels/_search

{

  "query": {

    "bool": {

      "must": [

        {

          "match_all": {}

        }

      ],

      "filter": {

        "geo_distance": {

          "distance": "200km",

          "pin.location": {

            "lat": 40,

            "lon": -70

          }}}}}}

统计当前位置每个距离范围内有多少家酒店

基于地理位置进行聚合分析

我的需求就是,统计一下,举例我当前坐标的几个范围内的酒店的数量,比如说举例我0~100m有几个酒店,100m~300m有几个酒店,300m以上有几个酒店

一般来说,做酒店app,一般来说,我们是不是会有一个地图,用户可以在地图上直接查看和搜索酒店,此时就可以显示出来举例你当前的位置,几个举例范围内,有多少家酒店,让用户知道,心里清楚,用户体验就比较好

GET /hotel_app/hotels/_search

{

  "size": 0,

  "aggs": {

    "agg_by_distance_range": {

      "geo_distance": {

        "field": "pin.location",

        "origin": {

          "lat": 40,

          "lon": -70

        },

        "unit": "mi",

        "ranges": [

          {

            "to": 100

          },

          {

            "from": 100,

            "to": 300

          },

          {

            "from": 300

          }] }}}}

posted @ 2019-06-24 15:09  Java野生程序猿  阅读(245)  评论(0编辑  收藏  举报