Bootstrap

ElasticSearch7.4学习之基础操作(增删改查)

本教程基于kibana开发工具开发
1、创建索引(注意!es8之后type已被废弃,以下操作也会出警告)

POST movie?include_type_name=true
{
 "settings":{
   "number_of_shards":3,
   "number_of_replicas":1
 },
 "mappings":{
   "_doc":{
     "properties":{
       "name":{
         "type":"text"
       },
       "type":{
         "type":"keyword"
       },
       "country":{
         "type":"text"
       },
       "director":{
         "type":"text"
       },
       "date":{
         "type":"date"
       }
     }
   }
 }
}

2、插入数据(1代表id,不写自动设置为默认值)

POST movie/_doc/1
{
  "name":"Titanic",
  "type":"romance",
  "country":"America",
  "director":"James",
  "date":"1997-12-19"
}

3、修改数据(修改id为1的director)

POST movie/_doc/1
{
  "name":"Titanic",
  "type":"romance",
  "country":"America",
  "director":"James-Cameron",
  "date":"1997-12-19"
}

4、直接修改指定字段

POST movie/_update/2
{
  "doc": {
    "director":"Luc-Besson"
  }
}

5、删除数据(删除id为1的数据)

DELETE movie/_doc/1

6、查询所有数据

GET movie/_search

7、查询所有数据,并按照日期降序排列

GET movie/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "date": {
        "order": "desc"
      }
    }
  ]
}

8、模糊查询匹配name为“Titanic”的数据

GET movie/_search
{
  "query": {
    "match": {
      "name": "Titanic"
    }
  }
}

9、只匹配name="leon"的数据

#只匹配leon字段
GET movie/_search
{
  "query": {
    "match_phrase": {
      "name": "leon"
    }
  }
}

10、多个字段匹配查询(name和sex都包含女的数据)

GET people/_search
{
  "query": {
    "multi_match": {
      "query": "女",
      "fields": ["name","sex"]
    }
  }
}

11、字段级别查询(sex为男和女的数据)

GET people/_search
{
  "query": {
    "terms": {
      "sex": [
        "男",
        "女"
      ]
    }
  }
}

12、语法查询(查询name包含“小”的数据,text类型)

GET people/_search
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "小"
    }
  }
}

13、范围查询(查询date为1995-01-01~2000-01-01之间的数据)

GET movie/_search
{
  "query": {
    "range": {
      "date": {
        "gte": "1995-01-01",
        "lte": "2000-01-01"
      }
    }
  }
}

14、布尔查询(查询name为"leon"或type为“romance”的数据)

GET movie/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "leon"
          }
        },
        {
          "match": {
            "type": "romance"
          }
        }
      ]
    }
  }
}

15、布尔查询(查询name为“leon”并且type为“action”的数据)

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "leon"
          }
        },
        {
          "match": {
            "type": "action"
          }
        }
      ]
    }
  }
}

16、布尔查询(查询type不是“romance”的所有数据)

GET movie/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "type": "romance"
          }
        }
      ]
    }
  }
}

17、filter查询(查询date为1994-09-14的所有数据)

GET movie/_search
{
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "date": [
            "1994-09-14"
          ]
        }
      }
    }
  }
}

18、聚合查询(按type分组,注意!不能用text类型来做)

GET movie/_search
{
  "aggs": {
    "group_by_type": {
      "terms": {
        "field": "type"
      }
    }
  }
}

查询结果为:(只贴了最后聚合查询结果,结果显示 romance有两条数据,action有一条数据)

"aggregations" : {
    "group_by_type" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "romance",
          "doc_count" : 2
        },
        {
          "key" : "action",
          "doc_count" : 1
        }
      ]
    }
  }

19、多个聚合查询(按 type和 date分组)

GET movie/_search
{
  "aggs": {
    "group_by_type": {
      "terms": {
        "field": "type"
      }
    },
    "group_by_date":{
      "terms": {
        "field": "date"
      }
    }
  }
}

查询结果为:(只贴了最后聚合查询结果,按date分为三组,按type分为两组)

"aggregations" : {
    "group_by_date" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 772329600000,
          "key_as_string" : "1994-06-23T00:00:00.000Z",
          "doc_count" : 1
        },
        {
          "key" : 779500800000,
          "key_as_string" : "1994-09-14T00:00:00.000Z",
          "doc_count" : 1
        },
        {
          "key" : 882489600000,
          "key_as_string" : "1997-12-19T00:00:00.000Z",
          "doc_count" : 1
        }
      ]
    },
    "group_by_type" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "romance",
          "doc_count" : 2
        },
        {
          "key" : "action",
          "doc_count" : 1
        }
      ]
    }
  }

20、聚合查询(对date进行计算,数值类型日期类型支持)

GET movie/_search
{
  "aggs": {
    "grades_date":{
      "stats": {
        "field": "date"
      }
    }
  }
}

查询结果为:(只贴了最后聚合查询结果,列出最大值、最小值、平均值和总和)

"aggregations" : {
    "grades_date" : {
      "count" : 3,
      "min" : 7.723296E11,
      "max" : 8.824896E11,
      "avg" : 8.1144E11,
      "sum" : 2.43432E12,
      "min_as_string" : "1994-06-23T00:00:00.000Z",
      "max_as_string" : "1997-12-19T00:00:00.000Z",
      "avg_as_string" : "1995-09-18T16:00:00.000Z",
      "sum_as_string" : "2047-02-21T00:00:00.000Z"
    }
  }

21、聚合查询(找出date值最小的数据)

GET movie/_search
{
  "aggs": {
    "grades_date":{
      "min": {
        "field": "date"
      }
    }
  }
}

查询结果为:(只贴了最后聚合查询结果)

"aggregations" : {
    "grades_date" : {
      "value" : 7.723296E11,
      "value_as_string" : "1994-06-23T00:00:00.000Z"
    }
  }

22、固定score查询(查询score为1.2并且name包含"leon"的数据)

GET movie/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "match":{
          "name":"leon"
        }
      },
      "boost": 1.2
    }
  }
}

------更新
23、通过查询关键字进行更新(通过name=“Forrest Gump”的条件来更新director字段信息)

POST movie/_update_by_query
{
  "script":{
    "lang": "painless",
    "source": "ctx._source.director = params.director;",
    "params":{
      "director": "Robert-Zemeckis"
    }
  },
  "query":{
    "match":{
      "name": "Forrest Gump"
    }
  }
}

24、自定义字段类型创建索引
设置分片数为1

PUT weibo
{
  "settings": {"number_of_shards": 1}
}

其中address、message和user既支持text类型又支持keyword

PUT weibo/_mapping
{
  "properties":{
    "address": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above":256
        }
      }
    },
    "message":{
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above":500
        }
      }
    },
    "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
        }
      }
    }
  }
}

25、批量插入数据

POST _bulk
{ "index" : { "_index" : "weibo"  } }
{"user":"飞飞是大王","message":"今天天气真好!","city":"北京","country":"中国","province":"北京","uid":1,"address":"中国北京市朝阳区","location":{"lat":"39.970798","lon":"116.325747"}}
{ "index" : { "_index" : "weibo"  } }
{"user":"小不小图图","message":"好想喝奶茶~","city":"北京","country":"中国","province":"北京","uid":2,"address":"中国北京市朝阳区","location":{"lat":"39.9904313","lon":"116.412754"}}
{ "index" : { "_index" : "weibo"  } }
{"user":"勤劳的小蜜蜂","message":"又是充实的一天!","city":"北京","country":"中国","province":"北京","uid":3,"address":"中国北京市海淀区","location":{"lat":"39.893801","lon":"116.408986"}}
{ "index" : { "_index" : "weibo"  } }
{"user":"炸鸡狂热爱好者","message":"今天的炸鸡安排上了","city":"北京","country":"中国","province":"北京","uid":4,"address":"中国北京市东城区","location":{"lat":"39.718256","lon":"116.367910"}}
{ "index" : { "_index" : "weibo"  } }
{"user":"电影试探员","message":"李安的双子杀手超赞!","city":"北京","country":"中国","province":"北京","uid":5,"address":"中国北京市通州区","location":{"lat":"39.918256","lon":"116.467910"}}

26、 查询 朝外soho 5km范围内的weibo用户

GET weibo/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "address": "北京"
          }
        }
      ]
    }
  },
  "post_filter": {
    "geo_distance": {
      "distance": "5km",
      "location": {
        "lat": 39.920086,
        "lon": 116.454182
      }
    }
  }
}
;