ES-基础查询 基础命令语句 查询

基础命令语句

  • 使用PUT 进行更新时需要将所有属性都抄一遍,否则会被置空
  • 而通过POST /索引名/类型名/文档id/_update 只需填写要修改的属性即可,灵活性更高
    method url 描述
    PUT localhost:9200/索引名/类型名/文档id 创建文档(指定id)
    POST localhost:9200/索引名/类型名 创建文档(随机文档id)
    POST localhost:9200/索引名/类型名/文档id/_update 修改文档
    DELETE localhost:9200/索引名/类型名/文档id 删除文档
    GET localhost:9200/索引名/类型名/文档id 通过id查询文档
    POST localhost:9200/索引名/类型名/_search 查询所有数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
json复制代码// 创建一个空的索引
PUT /test2
{
"mappings": {
"properties": {
"name": {"type": "text"},
"age": { "type": "integer"},
"birthday": {"type": "date"}
}
}
}
//创建一个索引test1 类型type1 id=1
PUT /test1/type1/1
{
// 往索引中插入数据
"name": "xxxxx",
"age": 18
}
// 获取索引信息
GET test1

查询

简单查询

1
2
3
4
5
6
7
8
9
json复制代码GET test1/type1/_search
{
// 查询的参数体
"query":{
"match": { //查询的结果为模糊匹配
"name": "遇见" //查询的条件
}
}
}

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
json复制代码{
"took" : 901,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {// 查询的结果都放在hits里了
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.26706278,
"hits" : [
{
"_index" : "test1",
"_type" : "type1",
"_id" : "2",
"_score" : 0.26706278, //匹配度越大,数值越大
"_source" : {
"name" : "遇见_line",
"age" : 20
}
},
{
"_index" : "test1",
"_type" : "type1",
"_id" : "3",
"_score" : 0.26706278,
"_source" : {
"name" : "遇见_line3",
"age" : 20
}
},
{
"_index" : "test1",
"_type" : "type1",
"_id" : "1",
"_score" : 0.26706278,
"_source" : {
"name" : "遇见_line1",
"age" : 20
}
}
]
}
}

结果过滤

在原本的查询中添加 _source即可进行字段的筛选

1
2
3
4
5
6
7
8
9
json复制代码GET test1/type1/_search
{
"query":{
"match": {
"name": "遇见"
}
},
"_source" : ["name"]
}

结果:

1
2
3
4
5
6
7
8
9
10
11
json复制代码"hits" : [
{
"_index" : "test1",
"_type" : "type1",
"_id" : "2",
"_score" : 0.26706278,
"_source" : {
"name" : "遇见_line"
// age 没有了
}
}

结果排序

通过sort 来排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
json复制代码GET test1/type1/_search
{
"query":{
"match": {
"name": "遇见"
}
},
"_source" : ["name"],
"sort":[
{
"age":{
"order": "desc" //"asc" 升序, desc降序
}
}
]

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
json复制代码"hits" : [
{
"_index" : "test1",
"_type" : "type1",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "遇见_line3"
},
"sort" : [
21
]
},
{
"_index" : "test1",
"_type" : "type1",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "遇见_line2"
},
"sort" : [
20
]
},
{
"_index" : "test1",
"_type" : "type1",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "遇见_line1"
},
"sort" : [
19
]
}
]
}

分页查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
json复制代码GET test1/type1/_search
{
"query":{
"match": {
"name": "遇见"
}
},
"_source" : ["name"],
"sort":[
{
"age":{
"order": "desc"
}
}
],
"from":0, //从第0条数据开始 ,数据下标从0开始
"size":2 // 每页显示2条数据
}

多条件查询 | Bool 查询

must(and) 所有条件都必须符合

查询名字包含遇见且年龄为19的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
json复制代码GET test1/type1/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "遇见"
}
},
{
"match": {
"age": 19
}
}
]
}
}
}

should (or)

查询年龄20 or 19的人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
json复制代码GET test1/type1/_search
{
"query":{
"bool": {
"should": [
{
"match": {
"age": "20"
}
},
{
"match": {
"age": 19
}
}
]
}
}
}

msut_not 等价于not

查询既不是19也不是20 岁的人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
json复制代码GET test1/type1/_search
{
"query":{
"bool": {
"must_not": [
{
"match": {
"age": "20"
}
},
{
"match": {
"age": 19
}
}
]
}
}
}

过滤器 filter

查询年龄在【10,20】,且名称包含遇见的人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
json复制代码GET test1/type1/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "遇见"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 10, //gt> gte>=
"lte": 20
}
}
}
]
}
}
}

多匹配查询 tags

查询标签中包含男or 技术的人

1
2
3
4
5
6
7
json复制代码GET test1/type1/_search
{
"query":{
"match": {
"tags": "男 技术" // 空格表示or
}
}

精准查询term

通过倒排索引进行查询

原始数据 倒排索引
博客id 标签 标签 博客id
1 windows windows 1,2,3
2 windows linux 3,4
3 windows,linux
4 liunx
  • term 直接精准匹配
  • match 使用分词器(先分析文档,在查找)
  • text :会被分词器拆分
  • keyword 被当成一个整体不会被分词器拆分
1
2
3
4
5
6
7
8
json复制代码GET test1/type1/_search
{
"query":{
"term": {
"name": "遇见_line1"
}
}
}

高亮查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
json复制代码GET test1/type1/_search
{
"query":{
"match": {
"name": "遇见"
}
},
// 选择name字段高亮
"highlight" : {
"fields":{
"name":{}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
json复制代码"hits" : [
{
"_index" : "test1",
"_type" : "type1",
"_id" : "3",
"_score" : 0.17402273,
"_source" : {
"name" : "遇见_line3",
"age" : 21,
"tags" : [
"直男",
"技术宅",
"内向"
]
},
"highlight" : {
"name" : [
// 被em标签包裹的就是高亮部分
"<em>遇</em><em>见</em>_line3"
]
}
},

我们也可以自定义高亮标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
json复制代码GET test1/type1/_search
{
"query":{
"match": {
"name": "遇见"
}
},
"highlight" : {
"pre_tags": "<p color='red'>",
"post_tags": "</p>",
"fields":{
"name":{}
}
}
}
1
2
3
4
5
6
json复制代码"highlight" : {
"name" : [
"<p color='red'>遇</p><p color='red'>见</p>_line3"
]
}
}

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%