Elasticsearch常用命令

获取健康值

GET /_cat/health

新增索引

PUT /test2
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "age":{
        "type": "long"
      },
      "birthday":{
        "type": "date"
      }
    }
  }
}

获取索引

GET /test2

获取索引状态

GET /_cat/indices?v

新增文档

PUT /test1/type1/1
{
  "name":"Stars",
  "link":"http://www.88sv.cn"
}

获取指定文档

GET /test1/type1/1

更新文档

POST /test3/_doc/1/_update
{
  "name":"张三1",
  "age":24,
  "birthday":"2021-08-10"
}

删除文档

DELETE /test1

按照name查询文档

GET /test1/user/_search?q=name:张三
GET /test1/user/_search
{
  "query":{
    "match": {
      "name": "张三"
    }
  }
}

查询字段过滤

GET /test1/user/_search
{
  "query":{
    "match": {
      "name": "张三"
    }
  },
  "_source":["name","desc"]
}

查询结果排序

GET /test1/user/_search
{
  "query":{
    "match": {
      "name": "张三"
    }
  },
  "_source":["desc","name"],
  "sort":[
    {
      "age":{
        "order":"desc"
      }
    }
  ]
}

查询结果分页

GET /test1/user/_search
{
  "query":{
    "match": {
      "name": "张三"
    }
  },
  "_source":["desc","name"],
  "sort":[
    {
      "age":{
        "order":"desc"
      }
    }
  ],
  "from":0,
  "size":10
}

多条件精确查询

  • must(相当于mysql中的and)
GET /test1/user/_search
{
  "query":{
    "bool": {
      "must": [
          {
            "match": {
              "name": "张三"
            }
          },{
            "match": {
              "age": 23
            }
          }
      ]
    }
  }
}
  • should(相当于mysql中的or)
GET /test1/user/_search
{
  "query":{
    "bool": {
      "should": [
          {
            "match": {
              "name": "张三"
            }
          },{
            "match": {
              "age": 23
            }
          }
      ]
    }
  }
}
  • must_not(相当于mysql中的not)
GET /test1/user/_search
{
  "query":{
    "bool": {
      "must_not": [
          {
            "match": {
              "age": 23
            }
          }
      ]
    }
  }
}
  • 范围筛选
  1. lt小于
  2. lte小于等于
  3. gt大于
  4. gte大于等于
GET /test1/user/_search
{
  "query":{
    "bool": {
      "must": [
          {
            "match": {
              "name": "张三"
            }
          }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 10,
              "lte": 30
            }
          }
        }
      ]
    }
  }
}
GET /test1/user/_search
{
  "query":{
    "match": {
      "tags": "男 技术"
    }
  }
}

关于分词:
term:直接精准查询
match:会使用分词器解析

GET test1/user/_search
{
  "query":{
    "match": {
      "name": "张三"
    }
  },
  "highlight":{
    
    "fields": {
      "name":{}
    }
  }
}

高亮显示

  • 默认高亮
GET test1/user/_search
{
  "query":{
    "match": {
      "name": "张三"
    }
  },
  "highlight":{
    "fields": {
      "name":{}
    }
  }
}
  • 自定义高亮
GET test1/user/_search
{
  "query":{
    "match": {
      "name": "张三"
    }
  },
  "highlight":{
    "pre_tags": "<span style='color:red;'>", 
    "post_tags": "</span>", 
    "fields": {
      "name":{}
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值