ElasticSearch操作命令

Chrome安装Sense插件执行curl请求

查询ES中所有索引

curl -XPOST "http://localhost:9200/_search" -d'
ElasticSearch操作命令_第1张图片
image.png

CRUD

创建索引和文档

索引的名称为“movies”,类型名称(“movie”)和id(“1”),pretty美化输出json

curl -XPUT "http://localhost:9200/movies/movie/1?pretty" -d'
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972
}'

更新文档

curl -XPUT "http://localhost:9200/movies/movie/1?pretty" -d'
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972,
    "genres": ["Crime", "Drama"]
}'

或者

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/movies/movie/1/_update?pretty' -d '
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972,
    "genres": ["Crime", "Drama"]
}'

根据ID获取文档

curl -XGET "http://localhost:9200/movies/movie/1?pretty" -d''

删除文档

curl -XDELETE "http://localhost:9200/movies/movie/1" -d''

批处理(_bulk端点)

curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/movies/movie/_bulk?pretty" -d '{"index":{"_id":"2"}}{"title":"The Godfather"}{"index":{"_id":"3"}} {"title":"ABCD"} '

搜索(_search端点)

  • 简单字符串查询
curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
    "query": {
        "query_string": {
            "query": "Drama"
        }
    }
}'
  • 指定搜索的字段
curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d'
{
    "query": {
        "query_string": {
            "query": "Crime",
            "fields": ["genres"]
        }
    }
}'
  • 过滤
curl -XPOST "http://localhost:9200/_search" -d'
{
    "query": {
        "filtered": {
            "query": {
                "query_string": {
                    "query": "drama"
                }
            },
            "filter": {
                "term": { "year": 1962 }
            }
        }
    }
}'

匹配查询

match_all匹配一切,如果没有指定size的值,则默认返回前10个文档,from指定返回从哪个文档开始

curl -XPOST "http://localhost:9200/_search" -d'
{
    "query": {
        "filtered": {
            "query": {
                "match_all": {
                }
            },
            "filter": {
                "term": { "year": 1962 }
            }
        }
    },
    //"sort" : [{ "排序字段" : { "order" : "desc"}},
    "from":10,
    "size": 1
}'

当文档title字段满足godfather godmother abcd中任意一个,则匹配成功

operator:用来控制match查询匹配词条的逻辑条件,默认值是or,如果设置为and,表示查询满足所有条件;

minimum_should_match:当operator参数设置为or时,该参数用来控制应该匹配的分词的最少数量;

curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
    "query": {
        "match": {
            "title": "Godfather GodMother ABCD",
            "operator":"or",
            "minimum_should_match":2
        }
    }
}'

查询title是The Godfather且1972年的记录

bool参数, 除了must,还有should(或),must_not(不包括),filter

curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
    "query": {
    "bool": {
      "must": [
        { "match": { "title": "The Godfather" ,"operator":"and"} },  //或者用match_phase
        { "match": { "year": 1972 } }
      ]
    }
  }
}'

聚合查询

curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
    "size": 0, //表示查询多少条文档,聚合只需就和结果,输出文档可以设置为0条
    "aggs": {
        "price_of_sum": { //自行取名作为结果集
            "sum": {   //min max terms:分组聚合
                "field": "price"
            }
        }
    }
}'

查询price范围在(-无穷,10)、[10, 20)、[20, +无穷)这3个区间的price总和

curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
   "aggs":{
        "my_agg_name":{
            "range":{
                "field":"price",
                "ranges":[
                    {"to":10},
                    {"from":10,"to":20},
                    {"from":20}
                ]
            },
            "aggs":{
                "my_agg_name_child":{
                    "sum":{"field":"price"}
                }
            }
        }
    }
}'

你可能感兴趣的:(ElasticSearch操作命令)