GET _search
{
"query": {
"match_all": {}
}
}
/获取所有的索引/
GET /_cat/indices
/检查es健康状况/
GET /_cat/health
/创建索引为twitter(要先在索引模式中创建索引模式,并与之匹配);type为_doc,id为1 的一个索引/
POST twitter/_doc/1
{
"user":"QCX",
"uid":1,
"city":"beijing",
"province":"Beijing",
"country":"China"
}
/查询数据 通过id搜索/
GET twitter/_doc/1
/put 更新数据/
PUT twitter/_doc/1
{
"user":"乔晨曦",
"uid":1,
"city":"北京",
"province":"北京",
"country":"中国",
"location":{
"lat":"29.884661",
"lon":"111.335210"
}
}
/通过查询 更新数据,API:_update_by_query/
POST twitter/_update_by_query
{
"script":{
"source":"ctx._source.city=params.city;ctx._source.province=params.province;",
"lang":"painless",
"params":{
"city":"上海",
"province":"上海"
}
},
"query":{
"match":{
"user":"乔晨曦"
}
}
}
/删除 index twitter/
DELETE twitter
/批量插入数据 _bulk接口 针对twitter这个index操作/
POST _bulk
{"index":{"_index":"twitter"}}
{"user":"双榆树-张三","message":"今天天气不错 出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}}
{"index":{"_index":"twitter"}}
{"user":"东城区-老刘","message":"出发,下一站云南","uid":3,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.904313","lon":"116.412757"}}
{"index":{"_index":"twitter"}}
{"user":"东城区-李四","message":"happy birthday","uid":4,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.893801","lon":"116.408986"}}
{"index":{"_index":"twitter"}}
{"user":"朝阳区区-老贾","message":"123 gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}}
{"index":{"_index":"twitter"}}
{"user":"朝阳区-老王","message":"Happy Birthday My Friend!","uid":6,"age":50,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}}
{"index":{"_index":"twitter"}}
{"user":"红桥-老吴","message":"好朋友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}}
GET twitter/_search
/类似数据库中的表 里面的type:text:这个字段可以用于全文索引,keywords:表示 这个字段可以用于聚合查询或者统计分析,text比keywords占用更多存储空间;这个mapping是自动发生的 我们叫做dynamic mapping。动态mapping有一个问题:有些字段不会正确的mapping,比如location字段没有mapping到geo位置信息。解决方法就是重新配置这个mapping/
GET twitter/_mapping
DELETE twitter
/设置 twitter 这个索引的基本信息 有一个分片/
PUT twitter
{
"settings":{"number_of_shards": 1}
}
/用 _mapping这个接口对twitter的mapping重新定义/
PUT twitter/_mapping
{
"properties" : {
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"city" : {
"type" : "keyword"
},
"country" : {
"type" : "keyword"
},
"location" : {
"type" : "geo_point"
},
"province" : {
"type" : "keyword"
},
"uid" : {
"type" : "long"
},
"user" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
/查询数据 用接口_search/
GET twitter/_search
{
“query”:{
“match”:{
“city”:“上海”
}
}
}
/使用bool表达式,使用了must条件,必须满足里面的两个条件,北京的年龄是30的;must 等同于sql中的and条件/
GET twitter/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"city":"北京"
}
},
{
"match":{
"age":"30"
}
}
]
}
}
}
/使用bool表达式,使用了must_not条件,查询不在北京的/
GET twitter/_search
{
"query":{
"bool":{
"must_not":[
{
"match":{
"city":"北京"
}
}
]
}
}
}
/使用bool表达式,使用了should条件,满足里面的两个条件之一即可,北京的或者上海的;should 等同于sql中的or条件/
GET twitter/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"city":"北京"
}
},
{
"match":{
"city":"上海"
}
}
]
}
}
}
/使用_count接口查询满足条件的数量/
GET twitter/_count
{
"query":{
"bool":{
"should":[
{
"match":{
"city":"北京"
}
},
{
"match":{
"city":"上海"
}
}
]
}
}
}
/基于地理位置的查询;
朝外soho 39.920086,116.454182
查询距离朝外soho 5km范围内的
上面query是查询条件
下面post_filter对查询结果过滤,按照地理位置过滤/
GET twitter/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"address":"北京"
}
}
]
}
},
"post_filter": {
"geo_distance": {
"distance": "5km",
"location": {
"lat": 39.920086,
"lon": 116.454182
}
}
}
}
/查询距离朝外soho 5km范围内的 按距离排序/
GET twitter/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"address":"北京"
}
}
]
}
},
"post_filter": {
"geo_distance": {
"distance": "5km",
"location": {
"lat": 39.920086,
"lon": 116.454182
}
}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 39.920086,
"lon": 116.454182
},
"order": "asc",
"unit": "km"
}
}
]
}
/范围查询 大于等于30岁,小于等于40岁之间的 降序排/
GET twitter/_search
{
"query":{
"range":{
"age":{
"gte":30,
"lte":40
}
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
/查询除了 包含happy 和 birthday 的记录/
GET twitter/_search
{
"query":{
"match":{
"message":"happy birthday"
}
}
}
/注意大小写的自动处理 这里没有区分大小写
Happy birthday 按照一个词组查询/
GET twitter/_search
{
"query":{
"match_phrase":{
"message":"Happy birthday"
}
}
}
/对结果进行高亮展示 highlight,查询结果多出一个highlight属性,返回一个带有标签的字符串/
GET twitter/_search
{
"query":{
"match_phrase":{
"message":"Happy birthday"
}
},
"highlight": {
"fields": {
"message": {}
}
}
}
/聚合操作 aggs,range 操作/
/size:0 不会把命中内容展示出来/
GET twitter/_search
{
"size":10,
"aggs":{
"age":{
"range":{
"field":"age",
"ranges":[
{
"from":20,
"to":30
},
{
"from":30,
"to":40
},
{
"from":40,
"to":50
}
]
}
}
}
}
/聚合操作 aggs,term 操作 类似sql的group by,按照某个字段分组运算/
GET twitter/_search
{
"query":{
"match":{
"message":"happy birthday"
}
},
"size":0,
"aggs":{
"city":{
"terms":{
"field":"city",
"size":10
}
}
}
}
/_analyze:分词器,分析器;分词过滤操作 再形成倒排索引
一个分词器由三个部分组成:
1、char filter 对字段进行过滤
2、tokenizer 形成分词器,讲一句话分成各个短语 各个
3、filter 过滤器 对分开的词进行过滤
在对index做mapping时是可以指定分词器的,默认分词器时standard
下面测试_analyzer分词器/
/standard 只能用空格做分词/
GET twitter/_analyze
{
"text":["Happy Birthday"],
"analyzer":"standard"
}
/analyzer simple/
GET twitter/_analyze
{
"text":["Happy.Birthday"],
"analyzer":"simple"
}
/自己设定tokenizer 和 filter来实现分词
如果一开始没有指定filter,只指定了tokenizer,把词组分成了Happy 和 Birthday 两个词,但是没有转为小写,这时候指定filter 把两个词都转为小写/
GET twitter/_analyze
{
"text":["Happy Birthday"],
"tokenizer":"standard",
"filter":["lowercase"]
}