kibana/elasticsearch使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_34233802/article/details/79387793
--获取某一特定值的信息<REST Verb> /<Index>/<Type>/<ID>?<Rounting>=
GET /yinshang/poi/4628754?routing=B00155MPVY

—快捷查询无法查询中文字
GET /dp_pois/_search?q=city:”藤县”  


—查询中文字符串
GET /dp_pois/_search    {
    "query" : {
        "match" : {
            "city": "上海"
        }
    }
}

—两次运行结果顺序不一样
GET /yinshang/_search   
{
    "query" : {
        "match" : {
            "city": "上海"
        }
    }
}

—term的类型应为kewword
GET /yinshang/_search
{

  "query": {
    "bool": {
      "filter": {
        "term": { "city": "南京" }
      }
    }
  }
}
--terms 匹配多个数据
GET /yinshang/_search
{

  "query": {
    "bool": {
      "filter": {
        "terms": { "city": ["南京"] }
      }
    }
  }
}

—查找上海大于3星的商铺
GET /yinshang/_search   
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "city": "上海"
                }
            },
            "filter": {
                "range" : {
                    "stars" : { "gt" : 3 } 
                }
            }
        }
    }
}

match 完全模糊匹配     match_phrase短语模糊匹配 keyword类型数据只能全匹配


— 创建文档
第一种方法使用 op_type 查询 -字符串参数:
PUT /website/blog/123?op_type=create
{ ... }
第二种方法是在 URL 末端使用 /_create :
PUT /website/blog/123/_create
{ ... }

DELETE /website/blog/123

如果每页展示 5 条结果,可以用下面方式请求得到 13 页的结果:
GET /_search?size=5
GET /_search?size=5&from=5
GET /_search?size=5&from=10

被允许的操作符如下:
gt:大于
gte:大于等于
lt:小于
lte:小于等于


——————————
查询点评poi商铺数量
GET /dp_pois/_search
{
    "query" : {
        "match" : {
            "name": "华联"
        }
    }
}

--查询多个index的方法
GET /amap_pois,amap_pois4,amap_pois5/_search
{
    "query" : {
        "match" : {
            "name": "华联"
        }
    }
}

—分组并计算平均评分
GET /yinshang/_search
{
    "size" : 0,
    "aggs" : { 
        "t_name" : { 
            "terms" : { 
              "field" : "type_name"
            },
            "aggs": { 
            "avg_price": { 
               "avg": {
                  "field": "stars" 
               }
            }
         }
        }
    }
}


—多个分组 类型、城市
GET /yinshang/_search
{
    "size" : 0,
    "aggs" : { 
        "type_name" : { 
            "terms" : { 
              "field" : "type_name"
            },
            "aggs": {
            "city" : { 
              "terms" : { 
                "field" : "city"
              }

            }}
        }
    }
}

—地理范围过滤,无该方法,filtered已不再使用
GET /yinshang/_search
{
  "query": {
    "filtered": {
      "filter": {
        "geo_distance": {
          "distance": "1km", 
          "position": { 
            "lat":  31.25575572,
            "lon": 121.4214928
          }
        }
      }
    }
  }
}
可改为如下形式
GET /yinshang/_search
{
  "query": {
    "bool": {
      "filter": {
        "geo_distance": {
          "distance": "1km", 
          "position": { 
            "lat":  31.25575572,
            "lon": 121.4214928
          }
        }
      }
    }
  }
}



—泉州,stars>3
GET /yinshang/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "city": "泉州"
                }
            },
            "filter": {
                "range" : {
                    "stars" : { "gt" : 3 } 
                }
            }
        }
    }
}

—泉州三星级美食
GET /yinshang/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "city": "泉州"

                }
            },
            "should": [
            { "match": { "type_name": "美食"}}],
            "filter": {
                "range" : {
                    "stars" : { "gt" : 3 } 
                }
            }
        }
    }
}

or

GET /yinshang/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "city": "上海"

                }
            },
            "should": [
            { "match": { "type_name": "美食"}},
            {"range" : {
                    "stars" : { "gt" : 4 } }
                }]
        }
    }
}


GET /yinshang/_search
{
    "query" : {
                "multi_match" : {
                    "query": "3",
                    "fields":   [ "stars" ]
                }   
    }
}

—查询多个条件
GET /yinshang/_search
{

 "query": {

  "bool": {"must": [
            { "match": { "type_name": "美食"}},
            { "match": { "city": "上海"}}
          ]}
  }
}

—多fields查找字段内容
GET /yinshang/_search
{
  "query": {
    "multi_match": {
      "query": “上海 购物",
      "fields":   [ "name", "addr","city" ]
    }
  }
}

—-地理范围内poi
GET /yinshang/_search
{
  "query": {
"bool" : {
    "must" : {
        "match_all" : {}
    },
    "filter" : {
        "geo_distance" : {
            "distance" : "200km",
            "position" : {
                "lat" : 24.999, 
                "lon" : 118.999
            }
        }
    }
}}
}

longitude,latitude 经度,纬度
经度东西半球的坐标范围是 -180 到 180
纬度南北半球的坐标范围是 -90 到 90

—指定地理范围内的poi
GET /yinshang/_search?size=0
{
    "aggs" : {
        "rings" : {
            "geo_distance" : {
                "field" : "position",
                "origin" : "24.999, 118.999",
                "unit" : "km", 
                "ranges" : [
                    { "to" : 100 },
                    { "from" : 100, "to" : 300 },
                    { "from" : 300 }
                ]
            }
        }
    }
}


—上海地区不同类别poi的数量
GET /yinshang/_search?size=0
{
    "query" : {
        "match" : {
            "city" : "上海"
        }
    },
    "aggs" : {
        "type_name" : {
            "terms" : {
              "field" : "type_name"
            }
        }
    }
}

—不同城市家乐福数量
GET /yinshang/_search?size=0
{
    "query" : {
        "match" : {
            "name" : "家乐福"
        }
    },
    "aggs" : {
        "city" : {
            "terms" : {
              "field" : "city"
            }
        }
    }
}


—minimum_should_match 匹配度
GET /dp_pois/_search
{
    "query" : {
        "match" : {
            "name": {"query": "家乐福", "minimum_should_match": "100%"}

        }
    }
}

—match more
GET /yinshang/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "city": "北京" }},
        { "match": { "type_name": "丽人"   }},
        { "match": { "area": "海淀区"  }}
      ],
      "minimum_should_match": 3
    }
  }
}

使用bool过滤器来合并多个过滤器以实现and,or和not逻辑。bool查询也做了类似的事,但有一个显著的不同。分值计算(Score Calculation)
bool查询通过将匹配的must和should语句的_score相加,然后除以must和should语句的总数来得到相关度分值_score。
must_not语句不会影响分值;它们唯一的目的是将不需要的文档排除在外
—多字段分值https://www.elastic.co/guide/cn/elasticsearch/guide/current/_boosting_query_clauses.html
—dis_max 即分离 最大化查询(Disjunction Max Query)
{
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "Brown fox" }},
                { "match": { "body":  "Brown fox" }}
            ]
        }
    }
}
—指定点附近地铁的数量
GET /yinshang/_search
{
  "query": {
    "bool": {
      "must": {
        "match": { 
          "addr": "地铁"
        }
       }
      }
    },
    "aggs" : {
        "rings" : {
            "geo_distance" : {
                "field" : "position",
                "origin" : "31.254822, 121.440661",
                "unit" : "km", 
                "ranges" : [
                    { "to" : 1 }
                ]
            }
        }
    },
    "post_filter": { 
    "geo_distance": {
      "distance":   "1km",
      "position": {
        "lat":      31.254822,
        "lon":     121.440661
      }
    }
    },
    "size": 10
}
—查询city包含 南京 并且name包含 美食 的地址      ,查询结果再按city名分组
GET /dp_pois/_search
{
  "size" : 0, 
  "query": { 
    "bool": {
      "must": [
        { "match": { "city":     "南京" }},
        { "match": { "name": "美食"          }}
      ]
    }
  },
  "aggs": {
    "users": {
      "terms": {
        "field":   "city",      
        "order": { "top_score": "desc" } 
      },
      "aggs": {
        "top_score": { "max":      { "script":  "_score"           }}, 
        "blogposts": { "top_hits": { "_source": "addr", "size": 5 }}  
      }
    }
  }
}

—创建index
PUT /customer?pretty
PUT /customer/_doc/1?pretty
{
  "name": "John Doe"
}
GET /customer/_doc/1?pretty
DELETE /customer?pretty
<REST Verb> /<Index>/<Type>/<ID>
POST /customer/_doc/1/_update?pretty
{
  "doc": { "name": "Jane Doe" }
}
POST /customer/_doc/1/_update?pretty
{
  "doc": { "name": "Jane Doe", "age": 20 }
}
POST /customer/_doc/1/_update?pretty
{
  "script" : "ctx._source.age += 5"
}
DELETE /customer/_doc/2?pretty

—范围 排序
GET /_search
{
  "query": { "match_all": {} },
  "sort": { "stars": { "order": "desc" }},
  "from": 10, 
  "size": 10
}
—指定source中字段
GET /_search
{
  "query": { "match_all": {} },
  "_source": ["name", "addr"]
}
—查找词语
GET /_search
{
  "query": { "match_phrase": { "addr": "上海" } }
}


—must同时满足
GET /_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "addr": "上海" } },
        { "match": { "addr": "" } }
      ]
    }
  }
}
—满足一个
GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "addr": "上海" } },
        { "match": { "addr": "" } }
      ]
    }
  }
}
—分组
GET /_search
{
  "size": 0,
  "aggs": {
    "group_by_city": {
      "terms": {
        "field": "city"
      }
    }
  }
}

—每个城市美食的数量及平均评分
GET /_search
{
  "query": { "bool": {
    "must": {
                "term" : {
                    "type_name": "美食"
                }},

    "filter": {
                "range" : {
                    "stars" : { "gt" : 0 } 
                }
            }
    }
  } ,
  "size": 0,
  "aggs": {
    "group_by_city": {
      "terms": {
        "field": "city"

      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "stars"
          }
        }
      }
    }
  }

}
—指定点附近的美食排序
POST /_search
{
   "query" : {
      "term" : { "type_name": "美食" }
   },
   "sort" : [
        {
            "_geo_distance" : {
                "position" : [113.3624924, 22.94712592],
                "order" : "asc",
                "unit" : "km",
                "mode" : "min",
                "distance_type" : "arc"
            }
        }
    ]
}

—南京玄武区不同种类的商铺的数量,以及南京所有区分别的商铺数量
GET /_search
{
  "query": {
    "bool": {
      "filter": {
        "term": { "city": "南京" } 
      }
    }
  },
  "aggs": {
    "colors": {
      "terms": { "field": "area" } 
    },
    "color_red": {
      "filter": {
        "term": { "area": "玄武区" } 
      },
      "aggs": {
        "models": {
          "terms": { "field": "type_name" } 
        }
      }
    }
  },
  "post_filter": { 
    "term": { "area": "玄武区" }
  }
}


1、分城市/行政区级别的poi按类别统计数量
GET /_search?size=0
{
  "aggs" : {
    "city" : {
      "terms" : {
         "field" : "city"

      },
      "aggs" : {
        "type_names" : {
          "terms" : {
            "field" : "type_name"

          }
        }
      }
    }
  }
}
上述语法无法应用到dp_pois上,应terms参数类型应为keyworld
2、综合超市数量、地理分布(例如家乐福,沃尔玛、大润发、欧尚、联华、华联)
GET /yinshang/_search
{   "size": 10, 
    "query" : {
        "match_phrase" : {
            "name" : "家乐福"
        }
    }    ,
    "aggs" : {
        "city" : {
            "terms" : {
              "field" : "city"
            },
            "aggs" : {
              "area" : {
              "terms" : {
                "field" : "area"
              }}
            }
        }
    }
}


GET /_search
{   "size": 10, 
    "query" : {
      "bool": {
        "must": [
         {"match_phrase" : {"name" : "家乐福"}},
        {"match_phrase" : {"type_name": "购物>超市/便利店"}}
        ]

    }},
    "aggs" : {
        "city" : {
            "terms" : {
              "field" : "city"
            },
            "aggs" : {
              "area" : {
              "terms" : {
                "field" : "area"
              }}
            }
        }
    },
    "post_filter": { 
    "term": { "area": "浦东新区" }
  }
}


    1   以点评数据为主
    2   通过关键字“家乐福”以及poi类型,找出全国所有的家乐福超市(约300家),具体结果可能需要手动去把最终的列表整理出来,poi信息的准确性可以和高德poi交叉验证
GET /_search
{   "size": 1000, 
    "query" : {
      "bool": {
        "must": [
          {"match_phrase": {"name": ""}},
         {"match_phrase" : {"name" : "家乐福"}},
        {"match_phrase" : {"type_name": "购物>超市/便利店"}}
        ]

    }},
    "aggs" : {
        "city" : {
            "terms" : {
              "field" : "city"
            },
            "aggs" : {
              "area" : {
              "terms" : {
                "field" : "area"
              }}
            }
        }
    }
}
因elastic无法对符号进行匹配,总数为436家,比实际值大
    3   搜索每一个家乐福周边三公里内的poi列表
GET /_search
{ 
  "query": {
    "bool": {
      "filter": {
        "geo_distance": {
          "distance":      "3km",
          "distance_type": "plane", 
          "position": {
            "lat":  31.3028178,
            "lon": 121.4187999
          }
        }
      }
    }
  }
}


    1   * 在某个特定的地理范围内(多边形),找出所有特定类型(例如餐饮),名称包含关键字(例如麦当劳)的poi,返回满足条件的poi列表,包含poi的基本信息(名称,经纬度坐标,地址,id)
GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "position" : {
                          "points": [ 
                              [121.335041,30.755042],
                              [121.335464,30.75513],
                              [121.335888,30.755213],
                              [121.33598,30.755229],
                              [121.336071,30.755213],
                              [121.33613,30.755146],
                              [121.336594,30.754063],
                              [121.335738,30.753756],
                              [121.334816,30.753431],
                              [121.334172,30.753226],
                              [121.333574,30.754501],
                              [121.335041,30.755042]
                            ]

                    }
                }
            }
        }
    }
}


    2   * 指定城市(例如上海),返回所有名称包含关键字(例如万达广场)的商场列表,包含商场的基本信息(名称,地址,经纬度坐标边界, id)
GET /_search
{ 
  "query": {
    "bool": {
      "must": [
        {"match_phrase": {"name": "万达广场"}},
        {"match_phrase": { "_type": "mall" }}
      ], 
      "filter": {
        "term": { "city": "上海" }
      }
    }
  }
}

    3   * 指定某个商场(从接口#2中返回),以该商场的经纬度坐标边界为地理范围,按照接口#1的条件做进一步查询
GET /_search
{
    "query": {
        "bool" : {
            "must" : {
                "match" : {"name" : "麦当劳"}
            },
            "filter" : {
                "geo_polygon" : {
                    "position" : {
                          "points": [ 
                          [121.513433,31.299647],
                          [121.513283,31.299743],
                          [121.513088,31.300281],
                          [121.512564,31.301219],
                          [121.511901,31.302248],
                          [121.511898,31.302326],
                          [121.335738,30.753756],
                          [121.334816,30.753431],
                          [121.334172,30.753226],
                          [121.333574,30.754501],
                          [121.335041,30.755042]
                            ]

                    }
                }
            }
        }
    }
}
    4   * 指定某个品牌关键字(例如麦当劳),返回所有包含该品牌的商场列表,包含商场的基本信息(名称,地址,经纬度坐标边界, id)
    GET /_search
{
  "query": {
    "has_child": {
      "type": "poi",
          "query": {
            "bool" : {
                "must" : {
                    "match" : {"name" : {"query": "麦当劳", "minimum_should_match": "100%"}}
                },
                "filter" : {
                    "geo_polygon" : {
                        "position" : {
                              "points": [ 
                              [121.085376,31.410612], [121.960162,31.436394], [121.995868,30.823947], [121.008472,30.803897], [121.085376,31.410612]
                                ]

                        }
                    }
                }
            }
        }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/sinat_34233802/article/details/79387793