基础命令语句
- 使用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 | json复制代码// 创建一个空的索引 |
查询
简单查询
1 | json复制代码GET test1/type1/_search |
结果:
1 | json复制代码{ |
结果过滤
在原本的查询中添加 _source即可进行字段的筛选
1 | json复制代码GET test1/type1/_search |
结果:
1 | json复制代码"hits" : [ |
结果排序
通过sort 来排序
1 | json复制代码GET test1/type1/_search |
1 | json复制代码"hits" : [ |
分页查询
1 | json复制代码GET test1/type1/_search |
多条件查询 | Bool 查询
must(and) 所有条件都必须符合
查询名字包含遇见且年龄为19的
1 | json复制代码GET test1/type1/_search |
should (or)
查询年龄20 or 19的人
1 | json复制代码GET test1/type1/_search |
msut_not 等价于not
查询既不是19也不是20 岁的人
1 | json复制代码GET test1/type1/_search |
过滤器 filter
查询年龄在【10,20】,且名称包含遇见的人
1 | json复制代码GET test1/type1/_search |
多匹配查询 tags
查询标签中包含男or 技术的人
1 | json复制代码GET test1/type1/_search |
精准查询term
通过倒排索引进行查询
原始数据 | 倒排索引 | ||
---|---|---|---|
博客id | 标签 | 标签 | 博客id |
1 | windows | windows | 1,2,3 |
2 | windows | linux | 3,4 |
3 | windows,linux | ||
4 | liunx |
- term 直接精准匹配
- match 使用分词器(先分析文档,在查找)
- text :会被分词器拆分
- keyword 被当成一个整体不会被分词器拆分
1 | json复制代码GET test1/type1/_search |
高亮查询
1 | json复制代码GET test1/type1/_search |
1 | json复制代码"hits" : [ |
我们也可以自定义高亮标签
1 | json复制代码GET test1/type1/_search |
1 | json复制代码"highlight" : { |
本文转载自: 掘金