ESrest基本操作

前排提示

本文整理自b站狂神说 Es课程

测试ik分词器是否生效

GET _analyze
{
  "analyzer": "ik_smart",
  "text":"今天天气不错"
}

put创建索引

PUT /test1/type1/1 
{
  "name":"测试1",
  "age":"18"
}

PUT /test1/_doc/1 
{
  "name":"测试1",
  "age":"18"
}

指定字段类型

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

修改索引

put覆盖索引

POST /test3/_doc/2/_update
{
  "doc":{
    "name":"修改测试3"
  }
}

删除索引

DELETE test3

查询索引

GET test3/_doc/2

GET test3/_doc/_search?q=name:测试

复杂查询

score 匹配度
_soutce 指定输出字段

GET test3/_doc/_search
{
  "query": {
    "match": {
      "name":"测试"
    }
  },
  "_source":["name"]
}

排序

GET test3/_doc/_search
{
  "query": {
    "match": {
      "name":"测试"
    }
  },
  "sort": [
    {
     "age": {
       "order":"desc"
     }
    }
    ]
}

分页查询

from 第几个开始
size 每页条数

GET test3/_doc/_search
{
  "query": {
    "match": {
      "name":"测试"
    }
  },
  "sort": [
    {
     "age": {
       "order":"desc"
     }
    }
    ],
    "from":0,
    "size" :2
}

布尔值查询 多条件查询

must = and 所有条件都要匹配
should = or 匹配一个就可以查询出来

must_not = not 不是的


GET test3/_doc/_search
{
  "query": {
    "bool": {
      "must" :[
        {
          "match": {
            "name":"测试"
          }
        },
        {
          "match" :{
            "age":18
          }
        }
        ]
    }
  }

}

过滤器

filter

GET test3/_doc/_search
{
  "query": {
    "bool": {
      "must" :[

        {
          "match" :{
            "name":"测试"
          }
        }
        ],
        "filter" : {
          "range": {
            "age" :{
              "gte":"20",
              "lte":"30"
            }
          }
        }
    }
  }

}-

精确查询

  • term 精确查找
  • match
    keyword 无法分割

高亮查询

  • highlight 设置高亮字段
GET test3/_search
{
  "query": {
          "match": {
             "name": "测试"
          }

  },
  "highlight" :{
    "fields" :{
      "name":{}
    }
  }
}

原文地址:https://www.cnblogs.com/ccubee/p/14254704.html