前言
查询操作是Elasticsearch最核心的模块之一。Elasticsearch能够达到数据的实时搜索,而且性能非常稳定,能很方便地用于对大量数据进行搜索和分析。这些都体现了Elasticsearch强大的搜索能力,因此关于Elasticsearch的查询知识的相关学习就显得非常重要。
本小节袁庭新老师就带领大家来学习Elasticsearch基本查询操作。我们从以下六个模块来学习Elasticsearch的基本查询功能。
- 查询所有match_all
- 匹配查询match
- 词条匹配term
- 布尔组合bool
- 范围查询range
- 模糊查询fuzzy
一. 查询所有match_all
1.语法结构
基本语法:
GET /索引库名/_search
{
"query": {
"查询类型": {
"查询条件": "查询条件值"
}
}
}
这里的query代表一个查询对象,里面可以有不同的查询属性。
- 查询类型:例如match_all、match、term、range等。
- 查询条件:查询条件会根据类型的不同,写法也有差异,后面详细讲解。
2.match_all查询
2.1 演示案例
演示示例:
GET /yx/_search
{
"query": {
"match_all": {}
}
}
语法说明:
| 属性 | 描述 |
|---|---|
| query | 代表查询对象 |
| match_all | 代表查询所有 |
响应结果:
{
"took": 65,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "yx",
"_type": "goods",
"_id": "5",
"_score": 1,
"_source": {
"title": "小米电视4A",
"images": "http://images.com",
"price": 3999
}
},
{
"_index": "yx",
"_type": "goods",
"_id": "4",
"_score": 1,
"_source": {
"title": "Apple手机",
"images": "http://image.yx.com/12479122.jpg",
"price": 6899
}
},
{
"_index": "yx",
"_type": "goods",
"_id": "lNC7KYUB35ub5htYEZMU",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "http://image.yx.com/12479122.jpg",
"price": 2699
}
},
{
"_index": "yx",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "IPhone手机",
"images": "http://image.yx.com/12479122.jpg",
"price": 6299,
"stock": 200,
"saleable": true,
"subTitle": "IPhone 15 Pro"
}
},
{
"_index": "yx",
"_type": "goods",
"_id": "1",
"_score": 1,
"_source": {
"title": "大米手机",
"images": "http://image.yx.com/12479122.jpg",
"price": 2899
}
}
]
}
}
上述代码运行后输出结果见下:
2.2 属性介绍
查询所有的属性介绍:
| 属性名 | 描述 |
|---|---|
| took | 查询花费时间,单位是毫秒 |
| time_out | 是否超时 |
| _shards | 分片信息 |
| hits | 搜索结果总览对象 |
搜索结果总览对象属性介绍:
| 属性名 | 描述 |
|---|---|
| total | 搜索到的总条数 |
| max_score | 所有结果中文档得分的最高分 |
| hits | 搜索结果的文档对象数组,每个元素是一条搜索到的文档信息 |
搜索结果的文档对象数组属性介绍:
| 属性名 | 描述 |
|---|---|
| _index | 索引库 |
| _type | 文档类型 |
| _id | 文档id |
| _score | 文档得分。使用Elasticsearch时,对于查询出的文档无疑会有文档相似度之别。而理想的排序是和查询条件相关性越高排序越靠前,而这个排序的依据就是_score |
| _source | 文档的源数据 |
二. match匹配查询
我们先加入一条数据,便于测试:
PUT /yx/goods/3
{
"title": "小米电视4A",
"images": "http://image.yx.com/12479122.jpg",
"price": 3899.00
}
现在,索引库中有3部手机,1台电视。
1.OR关系
match类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是OR的关系。
GET /yx/_search
{
"query": {
"match": {
"title": "小米电视"
}
}
}
在上面的案例中,不仅会查询到“电视”,而且与“小米”相关的都会查询到,多个词之间是OR的关系。
2.AND关系
某些情况下,我们需要更精确查找:比如在电商平台精确搜索商品时,我们希望这个关系(查询条件切分词之后的关系)变成AND(既要满足你,又要满足我),可以这样做:
GET /yx/_search
{
"query": {
"match": {
"title": {
"query": "小米电视",
"operator": "and"
}
}
}
}
本例中,只有同时包含“小米”和“电视”的词条才会被搜索到。
三. term词条匹配
term查询被用于精确值匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串,keyword类型的字符串。
效果类似于:SELECT * FROM tableName WHERE colName='value';的SQL语句。
GET /yx/_search
{
"query": {
"term": {
"price": 2699.00
}
}
}
响应结果:
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "yx",
"_type": "goods",
"_id": "lNC7KYUB35ub5htYEZMU",
"_score": 1,
"_source": {
"title": "小米手机",
"images": "http://image.yx.com/12479122.jpg",
"price": 2699
}
}
]
}
}
上述代码运行后输出结果见下:
四. bool布尔组合
bool把各种其它查询通过must(与)、must_not(非)、should(或)的方式进行组合。
GET /yx/_search
{
"query": {
"bool": {
"must": {
"match": {
"title": "大米"
}
},
"must_not": {
"match": {
"title": "电视"
}
},
"should": {
"match": {
"title": "手机"
}
}
}
}
}
响应结果:
{
"took": 28,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "yx",
"_type": "goods",
"_id": "1",
"_score": 0.5753642,
"_source": {
"title": "大米手机",
"images": "http://image.yx.com/12479122.jpg",
"price": 2899
}
}
]
}
}
上述代码运行后输出结果见下:
五. range范围查询
range查询找出那些落在指定区间内的数字或者时间。
GET /yx/_search
{
"query": {
"range": {
"price": {
"gte": 3000,
"lte": 4000
}
}
}
}
响应结果:
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "yx",
"_type": "goods",
"_id": "5",
"_score": 1,
"_source": {
"title": "小米电视4A",
"images": "http://images.com",
"price": 3999
}
}
]
}
}
上述代码运行后输出结果见下:
range查询允许以下字符:
| 操作符 | 说明 |
|---|---|
| gt | 大于 |
| gte | 大于等于 |
| lt | 小于 |
| lte | 小于等于 |
六. fuzzy模糊查询
fuzzy查询是term查询的模糊等价,很少直接使用它。
我们新增一个商品:
POST /yx/goods/4
{
"title": "Apple手机",
"images": "http://image.yx.com/12479122.jpg",
"price": 6899.00
}
响应结果:
{
"_index": "yx",
"_type": "goods",
"_id": "4",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 9,
"_primary_term": 4
}
上述代码运行后输出结果见下:
fuzzy查询是term查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2。
GET /yx/_search
{
"query": {
"fuzzy": {
"title": "Apple"
}
}
}
上面的查询,也能查询到“Apple手机”这条数据。
七. 结语
本小节主要给小伙伴介绍了Elasticsearch的六大基本查询语句的操作。分别介绍了:查询所有match_all、匹配查询match、词条匹配term、布尔组合bool、范围查询range和模糊查询fuzzy等。这一块儿的内容很重要,需要大家反复练习进行掌握。好了这一小节我们就说到这里,下一小节将带领大家来学习Elasticsearch高级查询相关的内容。
今天的内容就分享到这里吧。关注「袁庭新」,干货天天都不断!