ElasticSearch基本操作指令-增删改查

发布日期: 2021-01-18 14:49:28 作者: Stephen 评论: 0

字符串

text

会被分词器解析

Request:

GET _analyze
{
  "analyzer": "standard",
  "text": "四大天王"
}

Response:

{
  "tokens" : [
    {
      "token" : "四",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "大",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "天",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "王",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    }
  ]
}

keyword

不会被分词器解析

Request:

GET _analyze
{
  "analyzer": "keyword",
  "text": "香港四大天王刘德华"
}

Response:

{
  "tokens" : [
    {
      "token" : "香港四大天王刘德华",
      "start_offset" : 0,
      "end_offset" : 9,
      "type" : "word",
      "position" : 0
    }
  ]
}

示例

创建索引

PUT test2
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "desc": {
        "type": "keyword"
      }
    }
  }
}

插入数据

PUT test2/_doc/1
{
  "name": "不老男神刘天王",
  "desc": "香港四大天王刘德华"
}

PUT test2/_doc/2
{
  "name": "气质女神高圆圆",
  "desc": "中国四大女神高圆圆"
}

查询

GET test2/_search
{
  "query": {
    "match": {
      "name": "男神"
    }
  }
}

Result:可以查询到两条数据

GET test2/_search
{
  "query": {
    "match": {
      "desc": "中国四大女神高圆圆"
    }
  }
}

Result:精确匹配,可以查询到一条数据

GET test2/_search
{
  "query": {
    "match": {
      "desc": "高圆圆"
    }
  }
}

Result:查询不到数据,由于 desckeyword ,不会被分词器解析,需精确匹配查询

插入

# 新增/覆盖数据
PUT test1/_doc/1
{
  "name": "Stephen",
  "age": 28,
  "desc": "随便写点什么吧,我就是那么任性!",
  "tags": ["java", "阳光", "帅气"]
}

修改

# 修改数据
POST test1/_update/1
{
  "doc": {
    "birthday": "2002-01-24"
  }
}

等价于MySQL:

update test1 set `birthday` = '2002-01-24' where `id` = 1;

删除

删除数据

# 删除数据
DELETE test1/_doc/1

删除索引

# 删除索引
DELETE test1

查询

term:精确查询,效率更高match:会使用分词器解析(先分析文档,然后再通过分析的文档进行查询)

搜索及排序

# 搜索及排序
GET test1/_search
{
  "query": {
    "match": {
      "name": "四大"
    }
  },
  "_source": ["name", "desc"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

等价于MySQL:

select `name`, `desc` from test1 where `name` like "%四大%" order by `age` asc;

分页

# 分页查询
GET test1/_search
{
  "from": 0,
  "size": 20
}

等价于MySQL:

select * from test1 offset 0 limit 20;

多条件查询:and

# 布尔值多条件精确查询
GET test1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "四大"
          }
        },
        {
          "match": {
            "age": "32"
          }
        }
      ]
    }
  }
}

等价于MySQL:

select * from test1 where `name` like "%四大%" and `age` = 32;

注意

多条件查询:or

GET test1/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "天王"
          }
        },
        {
          "match": {
            "age": 32
          }
        }
      ]
    }
  }
}

等价于MySQL:

select * from test1 where `name` like "%天王%" or `age` = 32;

注意

多条件查询:not

# not
GET test1/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "age": 52
          }
        }
      ]
    }
  }
}

等价于MySQL:

select * from test1 where `age` != 52;

过滤器:filter

# filter过滤器
GET test1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "四大"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 40,
              "lte": 60
            }
          }
        }
      ]
    }
  }
}

等价于MySQL:

select * from test1 where `name` like "%四大%" and `age` between 40 and 60;

range:区间查询

  • gte:大于等于( >= )

  • lte:小于等于( <= )

  • gt:大于 ( > )

  • lt:小于( < )

  • eq:等于( = )

匹配多个条件

匹配 tags 标签中包含 活着 帅气 的文档

# 匹配多个条件
GET test1/_search
{
  "query": {
    "match": {
      "tags": "男 帅气"
    }
  }
}

高亮查询

# 高亮查询
GET test1/_search
{
  "query": {
    "match": {
      "name": "四大天王"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

查询出的结果默认会自动加上 em 标签

自定义高亮标签

# 自定义高亮标签
GET test1/_search
{
  "query": {
    "match": {
      "name": "四大天王"
    }
  },
  "highlight": {
    "pre_tags": "<span class='key' style='color: red'>",
    "post_tags": "</span>",
    "fields": {
      "name": {}
    }
  }
}

查询出的结果会自动加上自定义的 span 标签

快来抢沙发