原创

ElasticSearch基础语法

ElasticSearch基础语法

  • 1.删除索引
DELETE /test
{

}
  • 2.创建索引
    PUT /test
    {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 0
    }
    }
    
DELETE /employee
PUT /employee
{
  "settings": {
    "number_of_shards": 1
    , "number_of_replicas": 0
  }
}
  • 非结构化方式新建索引
PUT /employee/_doc/1
{
  "name":"凯杰2",
  "age":25
}
  • PUT 是全量更新;无则新建,有则更新。
    PUT /employee/_doc/1
    {
    "name":"凯杰3"
    }
    
  • 获取索引记录
    GET /employee/_doc/1
    
  • 指定字段修改
    POST /employee/_update/1
    {
    "doc": {
      "name":"凯杰4"
    }
    }
    
  • 强制指定创建,若已存在,则失败(按id判断)
    POST /employee/_create/4
    {
    "name":"兄长2",
    "age":31
    }
    
  • 删除某个文档
    DELETE /employee/_doc/1
    
  • 查询全部文档(默认含有分页)
    GET /employee/_search
    
  • 使用结构化的方式创建索引
    DELETE /employee
    
    PUT /employee
    {
    "settings": {
      "number_of_shards": 1
      , "number_of_replicas": 0
    },
    "mappings": {
      "properties": {
        "name":{"type":"text"},
        "age":{"type": "integer"}
      }
    }
    }
    
PUT /employee/_doc/1
{
  "name":"凯杰",
  "age":30
}
POST /employee/_create/2
{
  "name":"123",
  "age":"字符串测试", //可以防止脏数据
  "gender":"男" //不在mappers中的字段自动生成类型
}
  • 不带条件的查询所有记录(默认按_id倒序排序)
    GET /employee/_search
    {
    "query": {
      "match_all": {}
    }
    }
    
  • 分页查询
    GET /employee/_search
    {
    "query": {
      "match_all": {}
    },
    "from": 0,
    "size": 1
    }
    
  • 带关键字条件的查询
GET /employee/_search
{
  "query": {
    "match": {
      "name": "兄"
    }
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}
  • 带filter(相当于where)

    term精确(不分词)

    //
    GET /employee/_search
    {
    "query": {
      "bool": {
        "filter": [
            {
              "term":
                {
                  "age":30
                }
            }
          ]
      }
    }
    }
    

    match模糊(分词)

    GET /employee/_search
    {
    "query": {
      "bool": {
        "filter": [
            {
              "match":
                {
                  "name":"兄"
                }
            }
          ]
      }
    }
    }
    

带聚合

GET /employee/_search
{
  "query": {
    "match": {
      "name": "兄"
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "aggs": {
    "group_by_age": {  //aggs聚合中设置的group_by_tags参数用于返回,即结果返回在此字段上,可以随意设置。
      "terms": {
        "field": "age",
        "size": 10
      }
    }
  }
}
正文到此结束
本文目录