本文来记录下Elasticsearch各种文档操作
初始化文档数据
在进行各种文档操作之前,我们先进行初始化文档数据的工作
模糊查询文档
概述
match 模糊查询返回包含与搜索字词相似的字词的文档。
编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。这些更改可以包括:
操作 | 示例 |
---|---|
更改字符 | (box → fox) |
删除字符 | (black → lack) |
插入字符 | (sic → sick) |
转置两个相邻字符 | (act → cat) |
- 为了找到相似的术语,fuzzy 查询会在指定的编辑距离内创建一组搜索词的所有可能的变体或扩展。然后查询返回每个扩展的完全匹配。
- 通过 fuzziness 修改编辑距离。一般使用默认值 AUTO,根据术语的长度生成编辑距离。
示例1
在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:
服务器响应结果
{
"took": 22,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.2039728,
"hits": [
{
"_index": "person",
"_type": "_doc",
"_id": "1",
"_score": 1.2039728,
"_source": {
"name": "zhangsan",
"age": 20,
"sex": "男"
}
},
{
"_index": "person",
"_type": "_doc",
"_id": "2",
"_score": 1.0534762,
"_source": {
"name": "zhangsan1",
"age": 21,
"sex": "男"
}
},
{
"_index": "person",
"_type": "_doc",
"_id": "3",
"_score": 1.0534762,
"_source": {
"name": "zhangsan2",
"age": 22,
"sex": "男"
}
}
]
}
}
示例2
在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:
服务器响应结果
示例3
在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:
服务器响应结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.2039728,
"hits": [
{
"_index": "person",
"_type": "_doc",
"_id": "1",
"_score": 1.2039728,
"_source": {
"name": "zhangsan",
"age": 20,
"sex": "男"
}
},
{
"_index": "person",
"_type": "_doc",
"_id": "2",
"_score": 1.0534762,
"_source": {
"name": "zhangsan1",
"age": 21,
"sex": "男"
}
},
{
"_index": "person",
"_type": "_doc",
"_id": "3",
"_score": 1.0534762,
"_source": {
"name": "zhangsan2",
"age": 22,
"sex": "男"
}
}
]
}
}
单字段排序文档
sort 可以让我们按照不同的字段进行排序,并且通过 order 指定排序的方式。desc 降序,asc升序
在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:
服务器响应结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "person",
"_type": "_doc",
"_id": "3",
"_score": null,
"_source": {
"name": "zhangsan2",
"age": 22,
"sex": "男"
},
"sort": [
22
]
},
{
"_index": "person",
"_type": "_doc",
"_id": "2",
"_score": null,
"_source": {
"name": "zhangsan1",
"age": 21,
"sex": "男"
},
"sort": [
21
]
},
{
"_index": "person",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"name": "zhangsan",
"age": 20,
"sex": "男"
},
"sort": [
20
]
}
]
}
}
多字段排序文档
sort 可以让我们按照不同的字段进行排序,并且通过 order 指定排序的方式。desc 降序,asc升序。
假定我们想要结合使用 age 和 _score 进行查询,并且匹配的结果首先按照年龄排序,然后按照相关性得分排序。
在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:
{
"query": {
"fuzzy": {
"name": {
"value": "zhangsan"
}
}
},
"sort": [
{
"age": {
"order":"asc"
}
},
{
"_score": {
"order":"asc"
}
}
]
}
服务器响应结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "person",
"_type": "_doc",
"_id": "1",
"_score": 1.2039728,
"_source": {
"name": "zhangsan",
"age": 20,
"sex": "男"
},
"sort": [
20,
1.2039728
]
},
{
"_index": "person",
"_type": "_doc",
"_id": "2",
"_score": 1.0534762,
"_source": {
"name": "zhangsan1",
"age": 21,
"sex": "男"
},
"sort": [
21,
1.0534762
]
},
{
"_index": "person",
"_type": "_doc",
"_id": "3",
"_score": 1.0534762,
"_source": {
"name": "zhangsan2",
"age": 22,
"sex": "男"
},
"sort": [
22,
1.0534762
]
}
]
}
}
高亮查询文档
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮。
Elasticsearch 可以对查询内容中的关键字部分,进行标签和样式(高亮)的设置。
在使用 match 查询的同时,加上一个 highlight 属性:
属性 | 解释 |
---|---|
pre_tags | 前置标签 |
post_tags | 后置标签 |
fields | 需要高亮的字段 |
name | 这里声明 name字段需要高亮,后面可以为这个字段设置特有配置,也可以空 |
在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:
服务器响应结果
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.2039728,
"hits": [
{
"_index": "person",
"_type": "_doc",
"_id": "1",
"_score": 1.2039728,
"_source": {
"name": "zhangsan",
"age": 20,
"sex": "男"
},
"highlight": {
"name": [
"<font color='red'>zhangsan</font>"
]
}
}
]
}
}
分页查询文档
from :当前页的起始索引,默认从 0 开始。 from = (pageNum - 1) * size
size:每页显示多少条
在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:
服务器响应结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "person",
"_type": "_doc",
"_id": "4",
"_score": null,
"_source": {
"name": "lisi",
"age": 25,
"sex": "女"
},
"sort": [
25
]
},
{
"_index": "person",
"_type": "_doc",
"_id": "3",
"_score": null,
"_source": {
"name": "zhangsan2",
"age": 22,
"sex": "男"
},
"sort": [
22
]
}
]
}
}
完全匹配查询文档
match_phrase 会将检索关键词分词。match_phrase的分词结果必须在被检索字段的分词中都包含,默认情况下顺序必须相同且必须都是连续的。
在 apifox 中,向 ES 服务器发 POST 请求 :http://localhost:9200/person/_search,请求体内容为:
查询成功后,服务器响应结果文章来源:https://www.toymoban.com/news/detail-810830.html
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.2039728,
"hits": [
{
"_index": "person",
"_type": "_doc",
"_id": "1",
"_score": 1.2039728,
"_source": {
"name": "zhangsan",
"age": 20,
"sex": "男"
}
}
]
}
}
本文小结
本文记录了Elasticsearch一些常见的文档操作文章来源地址https://www.toymoban.com/news/detail-810830.html
到了这里,关于Elasticsearch各种高级文档操作2的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!