所有的操作根据RESETFUL风格

索引

PUT /索引名创建索引

image-20201117163832204

我们可以通过先前下载的可视化插件验证结果http://localhost:9100

image-20201117163935974

image-20201117164715076

image-20201117170357953

不过当前是什么都没有的。

我们可以给它在创建的时候指定字段的类型

  • 字符串类型:text keyword
  • 数值类型:long, integer, short, byte, double, float, half_float, scaled_float
  • 日期类型:date
  • 布尔值类型:boolean
  • 二进制类型 binary
  • 等等……

image-20201117170331080

image-20201117170425464

GET /索引名查询索引信息

image-20201117171141132

GET _cat/indices?v查询所有索引信息

image-20201117171305784

DELETE /索引名删除索引

image-20201117214541986

image-20201117214650508

成功删除!

文档

简单操作

put /索引名/类型(不写默认_doc)/id 添加数据 索引不存在自动创建,字段不存在也会自己创建智能选择类型(存在错误)。

image-20201117215628562

image-20201117215732047

image-20201117215754892

接着在添加几个数据

image-20201117220220636

image-20201117220237351

GET /索引名/类型/id获取数据

image-20201117220347626

POST(PUT)/索引/类型/ID{"字段":"值"...} 更新数据

image-20201117220628820

不过这种请求不推荐。因为它是将所有的字段数据都要进行修改,如果你有一个字段没写或者写错了就会发生为空或者更新错了的情况。

image-20201117220948490

image-20201117221248587

所以我们推荐使用POST /索引/类型/ID/_update{"doc":{"字段":"值"}} 更新数据

先将其复原成先前的样子image-20201117221500853

再更新。

image-20201117222210852

image-20201117222301826

复杂操作

条件查询GET /索引/类型/_search?q=字段名:字段值

image-20201117222659933

其中的score代表着匹配度,匹配度越高分值越高。

我们首先在插入一个数据

image-20201118092834280

匹配查询

查询字段值中匹配所给的值(包含)

GET /索引/类型/_search{"query":{"match":{"字段":"值"}}}

1
2
3
4
5
6
7
8
GET /索引/类型/_search
{
"query": {
"match": {
"字段1": "值"
}
}
}

image-20201118094022450

hits:包含索引和文档的信息,查询的结果总数,查询出来的具体的文档。数据中的东西都可以遍历出来。可以通过分数来判断谁更加符合结果。

结果过滤

例如select name, age from student

1
2
3
4
5
6
7
8
9
GET /索引/类型/_search
{
"query": {
"match": {
"字段1": "值"
}
},
"_source": ["字段1","字段2"...]
}

image-20201118095054682

排序操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GET /索引/类型/_search
{
"query": {
"match": {
"字段1": "值"
}
},
"sort": [
{
"字段": {
"order": "desc(asc)"
}
}
]
}

image-20201118095548824

由于指定了排序规则。所以score就没有用了为null。

分页查询

1
2
3
4
5
GET /索引/类型/_search
{
"from": 0,
"size": 1
}

from代表起始数据(数据下标从0开始)

size代表一页有多少条数据

image-20201118100639185

布尔查询

1
2
3
4
5
6
7
8
GET /索引/类型/_search
{
"query": {
"bool": {
"逻辑运算符"
}
}
}

image-20201118101236246

must(and),所有的条件都要符合 where id = 1 and name = xxx

image-20201118102258008

should(or),所有的条件都要符合 where id = 1 or name = xxx

image-20201118102435180

must_not(not)所有条件都不符合

image-20201118102705654

过滤器 filter

image-20201118102944927

  • gt 大于
  • gte 大于等于
  • lt 小于
  • lte 小于等于

image-20201118103152503

多条件匹配查询

匹配多个条件

1
2
3
4
5
6
7
8
GET /索引/类型/_search
{
"query": {
"match": {
"字段": "值1 值2 ..."
}
}
}

image-20201118103559277

精确查询

term查询是直接通过倒排索引指定的词条进程精确查找的!

关于分词:

  • term ,精确查询,对查询的值不分词,直接进倒排索引去匹配。
  • match,会使用分词器解析! (先分析文档,然后在通过分析的文档进行查询!)

两个类型 text keyword

为了区分这两个类型。我们首先创建一个索引,字段规则。

image-20201118104515613

添加数据

image-20201118104742702

我们首先使用不同的分词器来查看效果

image-20201118105115032

image-20201118105223570

image-20201118105836618

image-20201118105936831

term是代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇

match和term的区别是,match查询的时候,elasticsearch会根据你给定的字段提供合适的分析器,而term查询不会有分析器分析的过程。

match查询相当于模糊匹配,只包含其中一部分关键词就行

精确查询多个值

image-20201118134311466

高亮查询

1
2
3
4
5
6
7
8
9
10
11
12
13
GET /索引/类型/_search
{
"query": {
"match": {
"name": "kylin"
}
},
"highlight": {
"fields": {
"name":{}
}
}
}

image-20201118134906443

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GET /kylin/user/_search
{
"query": {
"match": {
"name": "kylin"
}
},
"highlight": {
"pre_tags": "<p class = 'key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name":{}
}
}
}

自定义高亮标签pre_tags,post_tags

image-20201118135208248

这些所有操作其实MySQL 也可以做,只是MySQL 效率比较低!