Elasticsearch语法

首先在Linux中启动docker,并启动es以及相关容器
#创建索引

PUT index_01
{
  
}

#查看索引

GET _cat/indices

#创建索引并设置分片副本,索引名称不能以下划线开头,不能用大写,不能以关键字命名

PUT index_02
{
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
  }
}

#删除缩影

DELETE index_02

#分词器

GET _analyze
{
  "text": "北京大兴区中科电商电商谷",
  "analyzer": "ik_max_word"
}

#创建索引和type

PUT index_user
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "user":{
      "properties": {
        "name":{
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "age":{
          "type": "integer"
        },
        "sex":{
          "type": "integer"
        },
        "address":{
          "type": "text",
          "analyzer": "ik_max_word"
        }
      }
    }
  }
}

#根据索引和mapping查询数据

GET index_user/user/_search
{
  
}

#POST方式新增数据

POST /index_user/user/
{
  "name":"test1",
  "age":20,
  "sex":0,
  "address":"北京大兴"
}

#PUT方式新增数据,必须加上ID

PUT /index_user/user/1
{
  "name":"小明",
  "age":20,
  "sex":0,
  "address":"北京大兴2"
}

#删除document 最后面是id

DELETE index_user/user/6gw6j3QBGLI-1RrOIdKR

#修改数据

PUT index_user/user/1
{
  "name":"小明modify",
  "age":20,
  "sex":0,
  "address":"北京大兴"
}

#数据查询-请求体查询-过滤查询(精准查询,不评分,bool+filter)

GET index_user/user/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "age": 20
        }
      }
    }
  }
}

#数据查询-请求体查询-字句查询(精准查询,不评分,bool+filter)
#gte大于等于 lt小于

GET index_user/user/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 20,
            "lt": 22
          }
        }
      }
    }
  }
}

#数据查询-请求体查询-匹配度查询(全文检索查询,进行打分,常用match)

GET index_user/user/_search
{
  "query": {
    "match": {
      "address": "北京"
    }
  }
}

#数据查询-组合查询-should

GET index_user/user/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "age": "20"
        }
      },
      "should": [
        {
          "match": {
            "name": "小明"
          }
        }
      ]
    }
  }
}

#数据查询-组合查询-must(分页)

GET index_user/user/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "age": 20
        }
      },
      "must": [
        {
          "term": {
            "name": {
              "value": "小明"
            }
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 20
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值