Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Mysql

  • PHP

  • Python

  • java

  • Go

  • 数据库

  • C#

  • ElasticSearch

    • ES快速开始
    • IK分词器
    • ES查询文档
      • 创建一个索引
        • 语法
        • 返回结果
        • 创建索引规则
      • 复杂搜索
        • 精确查询 match
        • 多条件查询 bool
        • 获取指定字段 _source
        • 排序 sort
        • 分页 from size
        • 高亮查询 highlight
  • Git

  • Gulp

  • Microsoft

  • Linux

  • 其它

  • 技术
  • ElasticSearch
mrcdh
2021-07-24
目录

ES查询文档

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)
GET localhost:9200/索引名称/类型名称/_search 查询所有数据
GET /索引名称 查询索引信息
GET /索引名称/类型名称/_search?q=name:mrcdh 根据条件查询文档

# 创建一个索引

# 语法

PUT /索引名/类型名/文档id
{请求体}
1
2
PUT /test1/type1/1
{
  "name": "mrcdh",
  "age": 18
}
1
2
3
4
5

# 返回结果

{
  "_index": "test1",
  "_type": "type1",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 创建索引规则

  • name 类型为 text
  • age 类型为 long
  • birthday 类型为 date
PUT /test2
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "aget": {
        "type": "long"
      },
      "birthday": {
        "type": "date"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 复杂搜索

# 精确查询 match

查询 name 等于 张三 的文档

GET /mrcdh/user/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  }
}
1
2
3
4
5
6
7
8

# 多条件查询 bool

通过 bool 来进行多条件查询

  • must: 所有条件都要符合,相当于 mysql 的 and
  • should: 只要有一个条件符合,相当于 mysql 的 or
  • must_not: 反向操作,相当于 mysql 的 not
  • filter: 使用 filter 进行数据过滤
GET /mrcdh/user/_search
{
  "query": {
    "bool":{
      "must": [
        {
          "match": {
            "name": "张三"
          }
        },
        {
          "match": {
            "age": 18
          }
        }
      ],
      "should": [
        {
          "match": {
            "name": "张三"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "name": "张三"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gt": 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# 获取指定字段 _source

使用 _source 来指定要获取的字段

GET /mrcdh/user/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  },
  "_source": ["name", "desc"]
}
1
2
3
4
5
6
7
8
9

# 排序 sort

GET /mrcdh/user/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  },
  "sort": [
    {
      "排序字段": {
        "order": "desc"
      }
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 分页 from size

  • from: 从第几条开始
  • size: 取几条数据
GET /mrcdh/user/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  },
  "from": 0,
  "size": 1
}
1
2
3
4
5
6
7
8
9
10

# 高亮查询 highlight

搜索的结果可以高亮显示

  • fields: 指定要高亮的字段
  • pre_tags: 自定义高亮包裹开始标签
  • post_tags: 自定义高亮包裹结束标签
GET /mrcdh/user/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color: red'>",
    "post_tags": "</p>",
    "fields": {
      "name": {}
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

返回

{
  "took": 50,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1.8132977,
    "hits": [
      {
        "_index": "mrcdh",
        "_type": "user",
        "_id": "2",
        "_score": 1.8132977,
        "_source": {
          "name": "张三",
          "aget": 3,
          "desc": "法外狂徒",
          "tags": ["打炮", "旅游", "渣男"]
        },
        "highlight": {
          "name": ["<em>张</em><em>三</em>"]
        }
      }
    ]
  }
}
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
#ElasticSearch
上次更新: 2023/09/22, 16:54:32
IK分词器
windows下生成SSH密钥

← IK分词器 windows下生成SSH密钥→

最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×