Elasticsearch基础命令使用

    科技2022-07-15  141

    注:7.0之后,type默认都是_doc

    创建文档

    elasticsearch创建文档有两种方式,1是POST,2是PUT,post是让elasticsearch自动生成id,而通过put,我们可以自己指定id

    自动生成id

    POST my_index/_doc { "username": "pibigsrar" "age": 12 }

    自定义id

    指定id为1(如果id已经存在,则直接报错)

    PUT my_index/_doc/1?op_type=create { "username": "pibigsrar" "age": 12 }

    更新

    删除并更新

    它会先删除原先id为1的文档,并把新的文档设置进去

    PUT my_index/_doc/1 { "username": "pibigsrar" }

    只更新

    你可以通过这个新增一个字段或修改某个字段的值

    POST my_index/_update/1 { "doc": { "username": "test" "sex": 1 } }

    删除

    DELETE my_index/_doc/1

    读取

    GET

    根据id获取

    GET my_index/_doc/1

    bulk

    批量执行操作,操作可以是查询,创建,删除,搜索等

    POST _bulk { "index" : { "_index" : "test", "_id" : "1" } } { "field1" : "value1" } { "delete" : { "_index" : "test", "_id" : "2" } } { "create" : { "_index" : "test2", "_id" : "3" } } { "field1" : "value3" } { "update" : {"_id" : "1", "_index" : "test"} } { "doc" : {"field2" : "value2"} }

    mget

    批量获取

    GET my_index/_mget { "docs" : [ { "_id" : "1" }, { "_id" : "2" } ] }

    不同的index

    GET /_mget { "docs" : [ { "_index" : "my_index", "_id" : "1" }, { "_index" : "test", "_id" : "2" } ] }

    msearch

    批量搜索

    POST my_index/_msearch {} {"query" : {"match_all" : {}},"size":1} {"index" : "kibana_sample_data_flights"} {"query" : {"match_all" : {}},"size":2}

    去重

    根据contentId 去重

    "aggs": { "distinct_contentId": { "cardinality": { "field": "contentId", } } }

    分组

    根据contentId分组count

    "aggs": { "label_group": { "terms": { "field": "contentId" } } }
    Processed: 0.014, SQL: 8