文章目录
多条件查询
- 多条件查询
- 范围查询
多条件查询会用到 “bool” , 代表条件的意思. 案例: 查询category是小米, price是1的数据 请求方式get 请求url: http://127.0.0.1:9200/shopping/_search
请求体 :
{
"query":{
"bool":{
"must":[
{
"match":{
"category":"小米"
}
},
{
"match":{
"price":1
}
}
]
}
}
}
最外层为query, 代表是查询, bool 代表是多条件查询. must代表是多个条件为且的关系, 里面为一个数组, 可以放多个查询的字段. 查询结果如下 :
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.210721,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "3",
"_score": 1.210721,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://xiaomi.com",
"price": 1
}
}
]
}
}
使用或查询, 要把must, 改成should. 先插入几个category为"华为" 的数据. 执行post请求, 请求url: http://127.0.0.1:9200/shopping/_doc
请求体:
{
"title": "华为手机",
"category": "华为",
"images": "http://huawei.com",
"price": 1999.00
}
响应:
{
"_index": "shopping",
"_type": "_doc",
"_id": "SptFPn4BwYTyfKb7AGLp",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 20,
"_primary_term": 2
}
再把price 改为6999 7999. 创建两个文档数据.
查询category是小米或华为的数据. get请求 请求体:
{
"query":{
"bool":{
"should":[
{
"match":{
"category":"小米"
}
},
{
"match":{
"category":"华为"
}
}
]
}
}
}
请求结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 1.6533571,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "SJtEPn4BwYTyfKb722JN",
"_score": 1.6533571,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://huawei.com",
"price": 6999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "SZtEPn4BwYTyfKb78GIE",
"_score": 1.6533571,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://huawei.com",
"price": 7999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "SptFPn4BwYTyfKb7AGLp",
"_score": 1.6533571,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://huawei.com",
"price": 1999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "3",
"_score": 1.1507283,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://xiaomi.com",
"price": 1
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "ex0KPX4BPkzBbPhZJmdM",
"_score": 1.1507283,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://xiaomi.com",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "fB0NPX4BPkzBbPhZNmcn",
"_score": 1.1507283,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://xiaomi.com",
"price": 3999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "2",
"_score": 1.1507283,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://xiaomi.com",
"price": 3999.00
}
}
]
}
}
可以看到把category是 小米和华为的都查询出来了.
范围查询查询价格大于6000的数据. 使用filter. filter 是在bool里面的, 与must, should是平级的. 请求方式 get 请求url : http://127.0.0.1:9200/shopping/_search
请求体:
{
"query":{
"bool":{
"should":[
{
"match":{
"category":"小米"
}
},
{
"match":{
"category":"华为"
}
}
],
"filter": {
"range": {
"price": {
"gt": 6000
}
}
}
}
}
}
请求结果 :
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.6533571,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "SJtEPn4BwYTyfKb722JN",
"_score": 1.6533571,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://huawei.com",
"price": 6999.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "SZtEPn4BwYTyfKb78GIE",
"_score": 1.6533571,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://huawei.com",
"price": 7999.00
}
}
]
}
}
可以看到结果只有price大于6000的数据.