继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Elasticsearch系列之Query DSL

冯文议
关注TA
已关注
手记 82
粉丝 1.4万
获赞 707

1 前言

我们先通过阅读官方文档,了解一下什么是 Query DSL 。

1.1 Query DSL

Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries.

DSL是啥?
由Elasticsearch提供的一套完整的JSON格式的查询语句。

Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses:

Leaf query clauses
Leaf query clauses look for a particular value in a particular field, such as the match, term or range queries. These queries can be used by themselves.
**

Compound query clauses

Compound query clauses wrap other leaf or compound queries and are used to combine multiple queries in a logical fashion (such as the bool or dis_max query), or to alter their behaviour (such as the constant_score query).

两种查询类型:DSL和AST。
一种是根据查询关键字进行查询。
另一种就是符合查询。

Query clauses behave differently depending on whether they are used in query context or filter context.

由两种查询行为:
一种是你告诉我,你有多匹配。
另一种,通过筛选,剔除不满足条件的。

Allow expensive queries

Certain types of queries will generally execute slowly due to the way they are implemented, which can affect the stability of the cluster. Those queries can be categorised as follows:

根据行为的不同,查询速度也会有不同,而这些查询行为是非常缓慢的,可能还会影响集群的稳定性。

The execution of such queries can be prevented by setting the value of the search.allow_expensive_queries setting to false (defaults to true).

既然有这些问题,Elasticsearch也提供优化办法,就是禁用这些查询行为,默认是开启的。

1.2 官方文档

1.3 Elasticsearch-head

1.3.1 概览界面

图片描述

1.3.2 索引界面

图片描述

1.3.3 数据浏览界面

图片描述

1.3.4 基本查询界面

图片描述

1.3.5 复合查询界面

图片描述

1.4 测试介绍

1.4.1 接口地址

1.4.2 提交方法

POST

1.4.3参数格式

JSON

1.4.4 Content-Type

application/json; charset=UTF-8

1.4.5 测试工具

图片描述

1.4.6 测试索引

1.4.7 测试数据

手机信息数据源,共 1253670 条数据,是从京东抓取的,耗时一天
点击下载:phone-info.json

2 基础需求

2.1 排序

{
    "query": {
        "match_all": {}
    },
    "sort": {
        "createTimeStamp": {
            "order": "desc"
        }
    }
}

响应示例片段

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "RU8FM3MB3aBfAxkrRLZS",
                "_score": null,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "Redmi 红米7A 小米手机 老人机 磨砂黑 3+32G",
                    "ad": "【顺丰快递】【假一赔十】今日下单可送精美礼品",
                    "price": 619,
                    "imgUrl": "//img10.360buyimg.com/n7/jfs/t1/99921/22/11777/109099/5e3a8ec6E1af36352/a32b3017b2429c62.jpg",
                    "memory": "3GB运存",
                    "storage": "32GB",
                    "screen": "5.45英寸",
                    "createTimeStamp": 1594288850000,
                    "createTimeString": "2020-07-09 18:00:50,000"
                },
                "sort": [
                    1594288850000
                ]
            }
        ]
    }
}

2.1.1 升序

asc 或者 ASC

2.1.2 降序

desc 或者 DESC

2.2 分页

{
    "query": {
        "match_all": {}
    },
    "from": 1,
    "size": 1
}

响应示例

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "7j1PL3MB3aBfAxkrf0nz",
                "_score": 1.0,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "荣耀Play3 6.39英寸魅眼全视屏 4000mAh大电池 真4800万AI三摄 畅玩全网通 幻夜黑 全网通6G+64G",
                    "ad": "荣耀Play3 6.39英寸魅眼全视屏 4000mAh大电池 真4800万AI三摄 畅玩全网通 幻夜黑 全网通6G+64G",
                    "price": 999.0,
                    "imgUrl": "//img13.360buyimg.com/n7/jfs/t1/126334/20/1159/208111/5eba565cE6065f0bc/4184f6b681990d1d.png",
                    "memory": "6GB运存",
                    "storage": "64GB",
                    "screen": "6.39 英寸",
                    "createTimeStamp": 1594226606065,
                    "createTimeString": "2020-07-09 00:43:26,065"
                }
            }
        ]
    }
}

2.2.1 from

从第几个开始查询,最开始是0

2.2.2 size

要查几个结果

3 搜索关键字

3.1 match_all

搜索的分值都是1.0

“max_score”: 1.0

{
    "query": {
        "match_all": {}
    }
}

响应示例片段

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "8z1PL3MB3aBfAxkrq0l2",
                "_score": 1,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "飞利浦E212A 翻盖按键超长待机 移动版双卡双待老人手机 学生备用功能机 飞利浦手机 深锖色(深蓝色)",
                    "ad": "咨询客服-领取优惠-下单立省-现货速发,15天价保,买贵补差价",
                    "price": 268,
                    "imgUrl": "//img10.360buyimg.com/n7/jfs/t1/24002/31/1438/337790/5c120bbeE6b9e6e12/befe00624e9a62a2.jpg",
                    "memory": "2GB以下运存",
                    "storage": "8GB以下",
                    "screen": "2.8英寸",
                    "createTimeStamp": 1594226617204,
                    "createTimeString": "2020-07-09 00:43:37,204"
                }
            }
        ]
    }
}

3.2 match_none

{
    "query": {
        "match_none": {}
    }
}

响应示例

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

3.3 match

指定字段,根据字段的关键字进行搜索

{
    "query": {
        "match": {
            "name": "小米"
        }
    }
}

响应示例片段

{
    "took": 20,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 6.0416183,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "sjwvL3MB3aBfAxkrLKVH",
                "_score": 6.0416183,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "小米(MI) 小米Max3 手机 黑色(现货) 全网通6GB+128GB",
                    "ad": "【京东物流、现货急速发】送:本店质保一年+运费险小米Play到手价1158",
                    "price": 1599,
                    "imgUrl": "//img12.360buyimg.com/n7/jfs/t1/105432/6/3249/74410/5dde4fd1E9c28713b/f2aea9f8bf1672a7.jpg",
                    "memory": "6GB运存",
                    "storage": "128GB",
                    "screen": "6.9英寸",
                    "createTimeStamp": 1594224487494,
                    "createTimeString": "2020-07-09 00:08:07,494"
                }
            }
        ]
    }
}

3.4 query_string

全文搜索

{
    "query": {
        "query_string": {
            "query": "小米"
        }
    }
}

响应示例片段

{
    "took": 43,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 11.336194,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "mTwuL3MB3aBfAxkr3qO_",
                "_score": 11.336194,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "小米9 Pro 5G 骁龙855Plus 30W无线闪充 8GB+256GB 钛银黑 双卡全网通 全面屏拍照智能新品游戏手机",
                    "ad": "小米,小米手机,小米9pro5g自营,小米9pro,小米5g版,小米9品质好物选小米,京东选购更省钱",
                    "price": 3799,
                    "imgUrl": "//img14.360buyimg.com/n7/jfs/t1/84314/2/11219/125539/5d89b1c9Ec781523e/cd769d2bd022de2a.jpg",
                    "memory": "8GB运存",
                    "storage": "256GB",
                    "screen": "6.39英寸",
                    "createTimeStamp": 1594224467645,
                    "createTimeString": "2020-07-09 00:07:47,645"
                }
            }
        ]
    }
}

3.4.1 and

{
    "query": {
        "query_string": {
            "query": "小米 and 红米"
        }
    }
}

响应示例片段

{
    "took": 50,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 21.420961,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "MjwtL3MB3aBfAxkrM5ii",
                "_score": 21.420961,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "Redmi K30 Pro 5G先锋 骁龙865旗舰处理器 弹出式超光感全面屏 索尼6400万高清四摄 4700mAh长续航 33W闪充 8GB+128GB 天际蓝 游戏智能手机【优享】",
                    "ad": "王一博同款k30pro,小米,xiaomi,小米手机,红米,redmi,红米手机,redmik30pro,k30pro,小米k30pro,小米5G,红米5G新品",
                    "price": 3399,
                    "imgUrl": "//img10.360buyimg.com/n7/jfs/t1/110595/15/10135/430668/5e79b705E69c06b1d/3d098ac4c305cf78.jpg",
                    "memory": "8GB运存",
                    "storage": "128GB",
                    "screen": "6.67英寸",
                    "createTimeStamp": 1594224358305,
                    "createTimeString": "2020-07-09 00:05:58,305"
                }
            }
        ]
    }
}

3.4.2 or

{
    "query": {
        "query_string": {
            "query": "小米 or 华为"
        }
    }
}

响应示例片段

{
    "took": 53,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 11.336194,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "mTwuL3MB3aBfAxkr3qO_",
                "_score": 11.336194,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "小米9 Pro 5G 骁龙855Plus 30W无线闪充 8GB+256GB 钛银黑 双卡全网通 全面屏拍照智能新品游戏手机",
                    "ad": "小米,小米手机,小米9pro5g自营,小米9pro,小米5g版,小米9品质好物选小米,京东选购更省钱",
                    "price": 3799,
                    "imgUrl": "//img14.360buyimg.com/n7/jfs/t1/84314/2/11219/125539/5d89b1c9Ec781523e/cd769d2bd022de2a.jpg",
                    "memory": "8GB运存",
                    "storage": "256GB",
                    "screen": "6.39英寸",
                    "createTimeStamp": 1594224467645,
                    "createTimeString": "2020-07-09 00:07:47,645"
                }
            },
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "yDwuL3MB3aBfAxkrv6LX",
                "_score": 11.323852,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "华为 Mate20 X 全网通智能手机 幻影银(6+128)",
                    "ad": "送:原装耳机+小钢炮蓝牙音箱+钢化膜+保护壳!华为p30、华为p30Pro华为mate20Pro",
                    "price": 3588,
                    "imgUrl": "//img14.360buyimg.com/n7/jfs/t24652/329/2253404779/295465/198e1b99/5bc7f1ecNc23ec763.jpg",
                    "memory": "6GB运存",
                    "storage": "128GB",
                    "screen": "7.2英寸",
                    "createTimeStamp": 1594224459734,
                    "createTimeString": "2020-07-09 00:07:39,734"
                }
            }
        ]
    }
}

3.5 match_phrase

如果我们并不想那样进行分割,那我们换一个关键字 match_phrase

{
    "query": {
        "match_phrase": {
            "name": "小米红米"
        }
    }
}

响应示例片段

{
    "took": 42,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1336,
            "relation": "eq"
        },
        "max_score": 10.831314,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "GjwvL3MB3aBfAxkr06nd",
                "_score": 10.831314,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "小米红米Note8pro 手机 电光灰 8G+128G",
                    "ad": "小金刚品质保证,6400万超广角四摄,3D四曲面玻璃机身!",
                    "price": 1599,
                    "imgUrl": "//img11.360buyimg.com/n7/jfs/t1/74883/5/13866/102693/5db3b32eEe950fcbd/62ae450227788db6.jpg",
                    "memory": "6GB运存",
                    "storage": "128GB",
                    "screen": "6.53英寸",
                    "createTimeStamp": 1594224530395,
                    "createTimeString": "2020-07-09 00:08:50,395"
                }
            }
        ]
    }
}

3.6 term

主要用于数字匹配。

举例:查询价格为1999的手机

{
    "query": {
        "term": {
            "price": 1999
        }
    }
}

响应示例片段

{
    "took": 15,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "_z1PL3MB3aBfAxkrrEkR",
                "_score": 1,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "华为 nova5i手机 后置AI四摄 全面屏 前置2400万高清 全网通双卡双待4G 幻夜黑 全网通8+128",
                    "ad": "华为(HUAWEI) nova 5i",
                    "price": 1999,
                    "imgUrl": "//img11.360buyimg.com/n7/jfs/t1/72502/14/4150/92863/5d2561a5Ed843b841/74dece57677911e3.jpg",
                    "storage": "128GB",
                    "createTimeStamp": 1594226617360,
                    "createTimeString": "2020-07-09 00:43:37,360"
                }
            }
        ]
    }
}

3.7 range

范围查询

举例:查询价格在1000-2000之间的手机

{
    "query": {
        "range": {
            "price": {
                "gte": 1000,
                "lte": 2000
            }
        }
    }
}

响应示例片段

{
    "took": 31,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "8j1PL3MB3aBfAxkrq0lp",
                "_score": 1,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "小米 Redmi 红米K30 极速版 5G手机 深海微光 6GB+128GB",
                    "ad": "5G双模,120Hz流速屏,骁龙768G,前置挖孔双摄,索尼6400万后置四摄,30W快充!K30i新品",
                    "price": 1599,
                    "imgUrl": "//img12.360buyimg.com/n7/jfs/t1/111819/3/6275/81916/5eba1a81Ee5a49643/cb43bd5ca9139ded.jpg",
                    "memory": "6GB运存",
                    "storage": "128GB",
                    "screen": "6.67英寸",
                    "createTimeStamp": 1594226617192,
                    "createTimeString": "2020-07-09 00:43:37,192"
                }
            }
        ]
    }
}

3.7.1 gt

大于

3.7.2 lt

小于

3.7.3 gte

大于等于

3.7.4 lte

小于等于

3.8 filter

在查询过程中,只判断文档是否满足条件,只有Yes或者No。

举例:查询价格为1999的手机

{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "price": 1999
                }
            }
        }
    }
}

响应示例片段

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 0.0,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "_z1PL3MB3aBfAxkrrEkR",
                "_score": 0.0,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "华为 nova5i手机 后置AI四摄 全面屏 前置2400万高清 全网通双卡双待4G 幻夜黑 全网通8+128",
                    "ad": "华为(HUAWEI) nova 5i",
                    "price": 1999.0,
                    "imgUrl": "//img11.360buyimg.com/n7/jfs/t1/72502/14/4150/92863/5d2561a5Ed843b841/74dece57677911e3.jpg",
                    "storage": "128GB",
                    "createTimeStamp": 1594226617360,
                    "createTimeString": "2020-07-09 00:43:37,360"
                }
            }
        ]
    }
}

3.9 multi_match

3.9.1 query

要查询的关键字

3.9.2 fileds

要查询的字段

3.9.3 type

best_fields

最佳字段。best_fields 类型是默认值,可以不指定。

most_fields

多数字段。多数字段匹配成功的得分之和,字段匹配越多,得分越高

cross_fields

cross_fields指的是一个唯一标识,跨域了多个字段,比如人的标识是名字,一个建筑的标识是地址,姓名跨域散落在多个field中,比如first_name和last_name,一个地址也可以散落在多个字段中,比如country,province,city中。

3.9.4 operator

operator使用and,是要返回两者同时匹配成功的结果。

3.9.5 tie_breaker

tie_breaker是将其它匹配的查询子句考虑进来也是可能的。通过指定tie_breaker参数将其它每个匹配的子句的分值乘以tie_breaker,以达到取得最佳匹配查询子句的_score。

示例:

{
    "query": {
        "multi_match": {
            "query": "一加",
            "fields": [
                "name",
                "ad"
            ],
            "type": "cross_fields",
            "operator": "and"
        }
    }
}

响应示例片段:

{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 83,
            "relation": "eq"
        },
        "max_score": 16.650242,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "OjwtL3MB3aBfAxkrDpfE",
                "_score": 16.650242,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "一加 OnePlus 8 5G旗舰 90Hz高清柔性屏 骁龙865 180g轻薄手感 12GB+256GB 青空 超清超广角拍照游戏手机",
                    "ad": "一加 8",
                    "price": 4599.0,
                    "imgUrl": "//img11.360buyimg.com/n7/jfs/t1/120981/4/3752/79431/5ed5aba3Ead96f265/23e94e5f9fa168d7.jpg",
                    "memory": "12GB运存",
                    "storage": "256GB",
                    "createTimeStamp": 1594224348866,
                    "createTimeString": "2020-07-09 00:05:48,866"
                }
            }
        ]
    }
}

要想了解更多,可以看官方文档:Elasticsearch权威指南:multi_match 查询

4 复合查询

4.1 constant_score

固定分数查询。支持filter查询,不支持match查询。

举例,查询分数为10的小米手机

{
    "query":{
        "constant_score":{
            "filter":{
                "match":{
                    "name":"小米"
                }
            },
            "boost":10
        }
        
    }
}

响应示例片段

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10000,
            "relation": "gte"
        },
        "max_score": 10,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "8j1PL3MB3aBfAxkrq0lp",
                "_score": 10,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "小米 Redmi 红米K30 极速版 5G手机 深海微光 6GB+128GB",
                    "ad": "5G双模,120Hz流速屏,骁龙768G,前置挖孔双摄,索尼6400万后置四摄,30W快充!K30i新品",
                    "price": 1599,
                    "imgUrl": "//img12.360buyimg.com/n7/jfs/t1/111819/3/6275/81916/5eba1a81Ee5a49643/cb43bd5ca9139ded.jpg",
                    "memory": "6GB运存",
                    "storage": "128GB",
                    "screen": "6.67英寸",
                    "createTimeStamp": 1594226617192,
                    "createTimeString": "2020-07-09 00:43:37,192"
                }
            }
        ]
    }
}

4.2 bool

主要与其他关键字组合使用。

4.3 should

or的关系

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "name": "咨询"
                    }
                },
                {
                    "match": {
                        "name": "实惠"
                    }
                }
            ]
        }
    }
}

响应示例片段

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 104,
            "relation": "eq"
        },
        "max_score": 49.01896,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "AjwuL3MB3aBfAxkroaJ_",
                "_score": 49.01896,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "华为HUAWEI nova 6 SE [下单-200,享碎屏险服务,分期0首付,咨询享实惠]手机 绮境森林(咨询省20) 8GB+128GB",
                    "ad": "咨询客服享优惠,赠钢化膜+手机壳+碎屏险+数据线+挂绳nova65G火热优惠中,详情猛戳",
                    "price": 1999.0,
                    "imgUrl": "//img12.360buyimg.com/n7/jfs/t1/87035/6/16675/136341/5e7df172Ec4e4ffd9/4944e5128f4cab11.jpg",
                    "memory": "8GB运存",
                    "storage": "128GB",
                    "screen": "6.4英寸",
                    "createTimeStamp": 1594224451966,
                    "createTimeString": "2020-07-09 00:07:31,966"
                }
            }
        ]
    }
}

4.4 must

and的关系。

示例

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": "咨询"
                    }
                },
                {
                    "match": {
                        "name": "实惠"
                    }
                }
            ]
        }
    }
}

响应示例片段

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 11,
            "relation": "eq"
        },
        "max_score": 49.01896,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "AjwuL3MB3aBfAxkroaJ_",
                "_score": 49.01896,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "华为HUAWEI nova 6 SE [下单-200,享碎屏险服务,分期0首付,咨询享实惠]手机 绮境森林(咨询省20) 8GB+128GB",
                    "ad": "咨询客服享优惠,赠钢化膜+手机壳+碎屏险+数据线+挂绳nova65G火热优惠中,详情猛戳",
                    "price": 1999.0,
                    "imgUrl": "//img12.360buyimg.com/n7/jfs/t1/87035/6/16675/136341/5e7df172Ec4e4ffd9/4944e5128f4cab11.jpg",
                    "memory": "8GB运存",
                    "storage": "128GB",
                    "screen": "6.4英寸",
                    "createTimeStamp": 1594224451966,
                    "createTimeString": "2020-07-09 00:07:31,966"
                }
            }
        ]
    }
}

4.5 must_not

{
    "query": {
        "bool": {
            "must_not": {
                "query_string": {
                    "query": "小米 or 华为 or 苹果 or 荣耀 or 红米 or 手机 or vivo or 三星 or iPhone or 一加"
                }
            }
        }
    }
}

响应示例片段:

{
    "took": 268,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 11,
            "relation": "eq"
        },
        "max_score": 0.0,
        "hits": [
            {
                "_index": "spring-boot-elasticsearch-sample-phone",
                "_type": "_doc",
                "_id": "ZTwtL3MB3aBfAxkrFJcJ",
                "_score": 0.0,
                "_source": {
                    "_class": "com.fengwenyi.spring_boot_elasticsearch_sample.entity.PhoneEntity",
                    "name": "金立 Gionee K6 8+128GB 梦幻蓝 4350mAh大电池 6.2英寸水滴屏 微Q8开 全网通4G 双卡双待",
                    "ad": "【评价享好礼】金立K6,4350mAh大电池|6.2英寸水滴屏|8+128GB大内存|评价返E卡!金立K3低至699元!",
                    "price": 899.0,
                    "imgUrl": "//img10.360buyimg.com/n7/jfs/t1/107655/18/18582/180279/5ec226c4Ebd488e23/7cbd62adba1590b4.jpg",
                    "memory": "8GB运存",
                    "storage": "128GB",
                    "screen": "6.2英寸",
                    "createTimeStamp": 1594224350216,
                    "createTimeString": "2020-07-09 00:05:50,216"
                }
            }
        ]
    }
}

Blog Link

Erwin Feng Bloghttps://www.yuque.com/fengwenyi/pvaq8o
博客长期更新,感兴趣,就点点关注,支持一下吧!

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP