elasticSearch语法

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 21:00   3471   0
PUT /employee
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

DELETE /employee

//非结构化方式新建索引
PUT /employee/_doc/1
{
  "name": "凯杰2",
  "age": 28
}

PUT /employee/_doc/1
{
  "name": "凯杰3"
}

//获取索引记录
GET /employee/_doc/1

//指定字段修改
POST /employee/_update/1
{
  "doc": {
    "name": "凯杰4"
  }
}

//强制指定创建,若已经存在,则失败
POST /employee/_create/2
{
  "name": "123",
  "age": 30
}

//删除某个文档
DELETE /employee/_doc/2

//删除全部文档
GET /employee/_search
//使用结构化方式创建索引
PUT /employee
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "name": {"type":"text"},
      "age": {"type":"integer"}
    }
  }
}

DELETE /employee


PUT /employee/_doc/1
{
  "name": "凯杰2",
  "age": 28
}

PUT /employee/_doc/2
{
  "name": "兄弟",
  "age": 35
}

PUT /employee/_doc/3
{
  "name": "兄妹",
  "age": 20
}

GET /employee/_search

//不带条件查询所有记录
GET /employee/_search
{
  "query":{
    "match_all": {}
  }
}

//分页查询
GET /employee/_search
{
  "query":{
    "match_all": {}
  },
  "from":0,
  "size":2
}

//带关键字条件的查询
GET /employee/_search
{
  "query":{
    "match": {"name":"兄长"}
  }
}

//带排序
GET /employee/_search
{
  "query":{
    "match": {"name":"兄长"}
  },
  "sort":[
    {"age":{"order":"desc"}}
  ]
}

//带filter,term不会分词,match会通过analyzer进行分词匹配
GET /employee/_search
{
  "query":{
    "bool":{
      "filter":[
        {"term":{"name":"兄长"}}
      ]
    }
  }
}

//带聚合
GET /employee/_search
{
  "query":{
    "match": {"name":"兄"}
  },
  "sort":[
    {"age":{"order":"desc"}}
  ],
  "aggs":{
    "group_by_age":{
      "terms": {
        "field": "age"
      }
    }
  }
}

//新建一个索引
PUT /movie/_doc/1
{
  "name":"Eating an apple a day & keeps the docter away."
}

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

//使用analyze api查看分词状态
GET /movie/_analyze
{
  "field":"name",
  "text":"Eating an apple a day & keeps the docter away."
}

GET /movie/_analyze
{
  "field":"name",
  "text":"eat"
}

//使用结构化的方式重新创建索引
PUT /movie/
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "name": {"type":"text","analyzer":"english"}
    }
  }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP