0
点赞
收藏
分享

微信扫一扫

es 7.x http 条件查询 分页查询 查询排序

做个橙梦 2022-01-09 阅读 32

文章目录

条件查询

请求url上的条件查询

get请求, 在url上进行条件查询,
例如查询category有小米的数据.
url如下 :
http://127.0.0.1:9200/shopping/_search?q=category:小米
查询结果如下:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 0.09037233,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ex0KPX4BPkzBbPhZJmdM",
                "_score": 0.09037233,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "fB0NPX4BPkzBbPhZNmcn",
                "_score": 0.09037233,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.09037233,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.09037233,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            }
        ]
    }
}

此种查询方式 , 由于查询条件是在url路径上, 中文容易乱码, 因此不推荐使用, 一般是把查询条件放在请求体中.

请求体查询数据

请求方式为get
请求url:
http://127.0.0.1:9200/shopping/_search
请求体:

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

查询结果如下 :

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 0.18074466,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ex0KPX4BPkzBbPhZJmdM",
                "_score": 0.18074466,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "fB0NPX4BPkzBbPhZNmcn",
                "_score": 0.18074466,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.18074466,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.18074466,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            }
        ]
    }
}

全量查询

请求方式为get
请求url:
http://127.0.0.1:9200/shopping/_search
请求体:

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

查询结果如下:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ex0KPX4BPkzBbPhZJmdM",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "fB0NPX4BPkzBbPhZNmcn",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            }
        ]
    }
}

分页查询

请求方式为get,
请求url: http://127.0.0.1:9200/shopping/_search
请求体: 代表查询从第一页查询,每页2条数据

{
    "query":{
        "match_all":{
          
        }
    },
    "from":  0,
    "size":  2
}

查询结果 :

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ex0KPX4BPkzBbPhZJmdM",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "fB0NPX4BPkzBbPhZNmcn",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            }
        ]
    }
}

from = (页数-1) * size 每页条数
查第二页的数据 from = (2-1) *2 = 2
查询结果如下:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            }
        ]
    }
}

对查询结果指定字段

_source 对指定的字段进行过滤, 例如只想在查询结果中要title字段.
查询请求体内容如下 :

{
    "query":{
        "match_all":{
          
        }
    },
    "from":  2,
    "size":  2,
    "_source": ["title"]
}

查询结果如下:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机"
                }
            }
        ]
    }
}

可以看到查询的结果字段只有title了.

查询排序

先把id为3的数据, 价格改为1.
执行post请求, 进行局部更新操作.
请求url
http://127.0.0.1:9200/shopping/_update/3
请求体: 只对price价格进行更新

{
    "doc": {
        "price": 1
    }
}

再执行对价格进行升序查询排序,
get请求,
请求url:
http://127.0.0.1:9200/shopping/_search
请求体:

{
    "query":{
        "match_all":{
          
        }
    },
    "from":  0,
    "size":  2,
    "sort": {
        "price":{
            "order": "asc"
        }
    }
}

请求结果如下 :

{
    "took": 211,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 1
                },
                "sort": [
                    1.0
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ex0KPX4BPkzBbPhZJmdM",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                },
                "sort": [
                    3999.0
                ]
            }
        ]
    }
}

可以看到price为1的在查询结果的第一个.

举报

相关推荐

0 条评论