系列文章目录
文章目录
- 系列文章目录
- 前言
- 一、基本查询
- 1.1 查询所有(match_all)
- 1.2 匹配查询(match)
- 1、插入预制数据
- 2、match关键字是or匹配
- 3、match关键字 + operator指定 是 and 匹配
- 4、or和and之间选择
- 1.3 多字段查询(multi_match)
- 1.4 精准值匹配(term)
- 1.5 多词条精确匹配(terms)
- 二、返回结果中仅返回需要在字段
- 2.1 通过_source关键字实现
- 2.2 通过includes和excludes实现
- 三、高级查询
- 3.1 布尔组合(bool)
- 3.2 范围查询(range)
- 3.3 模糊查询(fuzzy)
- 四、对查询结果行记录过滤(filter)
- 4.1 条件查询中进行过滤
- 4.2 无查询条件,直接过滤
- 五、排序
- 5.1 单字段排序
- 5.2 多字段排序
- 总结
前言
ES数据查询,我们从5个方面来讲讲解:
1、基本查询(类似mysql中的where field1 = xxx and filed2 like “%xxx%”)
2、_source
过滤/结果过滤(类似mysql中的 select field1,field2 from table-name)
3、高级查询(类似mysql中的where field1 = xxx and filed2 like “%xxx%”)
4、对查询结果行记录过滤(filter) (类似mysql中的where field1 = xxx and filed2 like “%xxx%”)
5、排序(类似mysql中的 order by field desc/asc)
小结:基本查询 match bool range fuzzy,然后过滤 filter,然后 _source 排序,最后排序 order 。
本文所有的es操作语句:https://www.syjshare.com/res/5W547A7Z
一、基本查询
基本语法
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
这里的query代表一个查询对象,里面可以有不同的查询属性
- 查询类型:
- 例如:
match_all
,match
,term
,range
等等
- 查询条件:查询条件会根据类型的不同,写法也有差异,后面详细讲解
数据预制
1.1 查询所有(match_all)
示例:
GET /myindex/_search
{
"query":{
"match_all": {}
}
}
-
query
:代表查询对象 -
match_all
:代表查询所有
结果:
- took:查询花费时间,单位是毫秒
- time_out:是否超时
- _shards:分片信息
- hits:搜索结果总览对象
- total:搜索到的总条数
- max_score:所有结果中文档得分的最高分
- hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
- _index:索引库
- _type:文档类型
- _id:文档id
- _score:文档得分
- _source:文档的源数据
1.2 匹配查询(match)
1、插入预制数据
我们先加入一条数据,便于测试:
PUT /myindex/goods/3
{
"title":"小米电视4A",
"price":3899.00
}
GET /myindex/_search
{
"query":{
"match_all": {}
}
}
现在,索引库中有2部手机,1台电视:
2、match关键字是or匹配
match
类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系
GET /myindex/_search
{
"query":{
"match":{
"title":"小米电视"
}
}
}
或者
GET /myindex/_search
{
"query":{
"match":{
"title":{
"query": "小米电视" }
}
}
}
或者
GET /myindex/_search
{
"query":{
"match":{
"title":{
"query": "小米电视",
"operator": "or"
}
}
}
}
结果:
在上面的案例中,小米电视 四个字中任何一个字,只要有一个字匹配上了,就可以匹配到,所以说,多个词之间是or
的关系。
3、match关键字 + operator指定 是 and 匹配
某些情况下,我们需要更精确查找,我们希望这个关系变成and
,可以这样做:
GET /myindex/_search
{
"query":{
"match": {
"title": {
"query": "小米电视",
"operator": "and"
}
}
}
}
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_score": 0.5753642,
"_source": {
"title": "小米电视4A",
"price": 3899
}
}
]
}
}
本例中,只有同时包含小米电视
的词条才会被搜索到。
4、or和and之间选择
在 or
与 and
间二选一有点过于非黑即白。 如果用户给定的条件分词后有 5 个查询词项,想查找只包含其中 4 个词的文档,该如何处理?将 operator 操作符参数设置成 and
只会将此文档排除。
有时候这正是我们期望的,但在全文搜索的大多数应用场景下,我们既想包含那些可能相关的文档,同时又排除那些不太相关的。换句话说,我们想要处于中间某种结果。
match
查询支持 minimum_should_match
最小匹配参数, 这让我们可以指定必须匹配的词项数用来表示一个文档是否相关。我们可以将其设置为某个具体数字,更常用的做法是将其设置为一个百分数
,因为我们无法控制用户搜索时输入的单词数量:
GET /myindex/_search
{
"query":{
"match":{
"title":{
"query":"小米曲面电视",
"minimum_should_match": "75%"
}
}
}
}
本例中,搜索语句可以分为3个词,如果使用and关系,需要同时满足3个词才会被搜索到。这里我们采用最小品牌数:75%,那么也就是说只要匹配到总词条数量的75%即可,这里3*75% 约等于2。所以只要包含2个词条就算满足条件了。
结果:
1.3 多字段查询(multi_match)
multi_match
与match
类似,不同的是它可以在多个字段中查询
GET /myindex/_search
{
"query":{
"multi_match": {
"query": "小米",
"fields": [ "title", "price" ]
}
}
}
本例中,我们会在title字段和price字段中查询小米
这个词
1.4 精准值匹配(term)
term
查询被用于精确值匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串
GET /myindex/_search
{
"query":{
"term":{
"price":2699.00
}
}
}
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"title": "小米手机",
"price": 2699
}
}
]
}
}
1.5 多词条精确匹配(terms)
terms
查询和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件:
GET /myindex/_search
{
"query":{
"terms":{
"price":[2699.00,3899.00]
}
}
}
结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex",
"_type" : "goods",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"title" : "小米手机",
"price" : 2699
}
},
{
"_index" : "myindex",
"_type" : "goods",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"title" : "小米电视4A",
"price" : 3899.0
}
}
]
}
}
再来示范一个,如下:
GET /myindex/_search
{
"query":{
"terms":{
"price":[2699.00,2899.00,3899.00]
}
}
}
结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "2",
"_score": 1,
"_source": {
"title": "大米手机",
"price": 2899
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"title": "小米手机",
"price": 2699
}
},
{
"_index": "heima",
"_type": "goods",
"_id": "3",
"_score": 1,
"_source": {
"title": "小米电视4A",
"price": 3899
}
}
]
}
}
二、返回结果中仅返回需要在字段
默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source
的所有字段都返回。
如果我们只想获取其中的部分字段,我们可以添加_source
的过滤
2.1 通过_source关键字实现
示例:
GET /myindex/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 2699
}
}
}
返回的结果:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "r9c1KGMBIhaxtY5rlRKv",
"_score": 1,
"_source": {
"price": 2699,
"title": "小米手机"
}
}
]
}
}
2.2 通过includes和excludes实现
我们也可以通过:
- includes:来指定想要显示的字段
- excludes:来指定不想要显示的字段
二者都是可选的。
示例:
GET /myindex/_search
{
"_source": {
"includes":["title"]
},
"query": {
"term": {
"price": 2699
}
}
}
与下面的结果将是一样的:
GET /myindex/_search
{
"_source": {
"excludes": ["price"]
},
"query": {
"term": {
"price": 2699
}
}
}
三、高级查询
3.1 布尔组合(bool)
bool
把各种其它查询通过must
(与)、must_not
(非)、should
(或)的方式进行组合
GET /myindex/_search
{
"query":{
"bool":{
"must": { "match": { "title": "大米" }},
"must_not": { "match": { "title": "电视" }},
"should": { "match": { "title": "手机" }}
}
}
}
结果:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "heima",
"_type": "goods",
"_id": "2",
"_score": 0.5753642,
"_source": {
"title": "大米手机",
"price": 2899
}
}
]
}
}
3.2 范围查询(range)
range
查询找出那些落在指定区间内的数字或者时间
GET /myindex/_search
{
"query":{
"range": {
"price": {
"gte": 1000.0,
"lt": 2800.00
}
}
}
}
range
查询允许以下字符:
操作符 | 说明 |
gt | 大于 |
gte | 大于等于 |
lt | 小于 |
lte | 小于等于 |
3.3 模糊查询(fuzzy)
我们新增一个商品:
POST /myindex/goods/4
{
"title":"apple手机",
"price":6899.00
}
fuzzy
查询是 term
查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2:
GET /myindex/_search
{
"query": {
"fuzzy": {
"title": "appla"
}
}
}
上面的查询,也能查询到apple手机
我们可以通过fuzziness
来指定允许的编辑距离:
GET /myindex/_search
{
"query": {
"fuzzy": {
"title": {
"value":"appla",
"fuzziness":1
}
}
}
}
四、对查询结果行记录过滤(filter)
4.1 条件查询中进行过滤
所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用filter
方式:
GET /myindex/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手机" }},
"filter":{
"range":{"price":{"gt":2000.00,"lt":3800.00}}
}
}
}
}
注意:filter
中还可以再次进行bool
组合条件过滤。
小结:匹配分词 “小米手机” 的输出结果,然后再次按照 price 进行筛选,最后输出。
4.2 无查询条件,直接过滤
如果一次查询只有过滤,没有查询条件,不希望进行评分,我们可以使用constant_score
取代只有 filter 语句的 bool 查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。
GET /myindex/_search
{
"query":{
"constant_score": {
"filter": {
"range":{"price":{"gt":2000.00,"lt":3000.00}}
}
}
}
五、排序
5.1 单字段排序
sort
可以让我们按照不同的字段进行排序,并且通过order
指定排序的方式
GET /myindex/_search
{
"query": {
"match": {
"title": "小米手机"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
小结:小米手机 分词查询,然后order desc排序。
5.2 多字段排序
假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序:
GET /_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手机" }}
}
},
"sort": [
{ "price": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}
GET /myindex/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手机" }},
"filter":{
"range":{"price":{"gt":2000.00,"lt":3000.00}}
}
}
},
"sort": [
{ "price": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}
总结
ES数据查询,我们从5个方面来讲讲解:
1、基本查询(类似mysql中的where field1 = xxx and filed2 like “%xxx%”)
2、_source
过滤/结果过滤(类似mysql中的 select field1,field2 from table-name)
3、高级查询(类似mysql中的where field1 = xxx and filed2 like “%xxx%”)
4、对查询结果行记录过滤(filter) (类似mysql中的where field1 = xxx and filed2 like “%xxx%”)
5、排序(类似mysql中的 order by field desc/asc)
小结:基本查询 match ,然后 bool range fuzzy,再然后过滤 filter,再然后 _source 排序,最后排序 order 。