Elasticsearch基本操作

1. 条件查询

1.1 直接在路径中添加额外参数

EX:查询数据中,“sex”为“女”的数据。

打开postman,新建GET请求:

 点击send,查看结果:

{
    "took": 74,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.87546873,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1004",
                "_score": 0.87546873,
                "_source": {
                    "name": "王五",
                    "age": 17,
                    "sex": "女",
                    "tel": "13254485621"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1003",
                "_score": 0.87546873,
                "_source": {
                    "name": "李凤",
                    "age": 26,
                    "sex": "女",
                    "tel": "19854458751"
                }
            }
        ]
    }
}

问题:在请求路径中添加额外参数,相对麻烦,且容易在出现中文时,出现乱码的可能性。

解决:通过请求体来传参-->匹配查询

1.2 匹配查询(match)

打开postman,新建GET请求:

 点击send,查看结果:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.87546873,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1004",
                "_score": 0.87546873,
                "_source": {
                    "name": "王五",
                    "age": 17,
                    "sex": "女",
                    "tel": "13254485621"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1003",
                "_score": 0.87546873,
                "_source": {
                    "name": "李凤",
                    "age": 26,
                    "sex": "女",
                    "tel": "19854458751"
                }
            }
        ]
    }
}

这边的“match”是模糊查询,ex:查询“name”中有“张”的数据。

结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.3862942,
                "_source": {
                    "name": "张三",
                    "age": 14,
                    "sex": "男",
                    "tel": "12255864789"
                }
            }
        ]
    }
}

问:假如查询person索引下的所有文档,请求体该如何写?

==>全量查询

1.3 全量查询

打开postman,新建GET请求:

 点击send,查看结果:

{
    "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": "person",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.0,
                "_source": {
                    "name": "张三",
                    "age": 14,
                    "sex": "男",
                    "tel": "12255864789"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1006",
                "_score": 1.0,
                "_source": {
                    "name": "李四",
                    "age": 28,
                    "sex": "男",
                    "tel": "15655889721"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.0,
                "_source": {
                    "name": "王五",
                    "age": 17,
                    "sex": "女",
                    "tel": "13254485621"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1003",
                "_score": 1.0,
                "_source": {
                    "name": "李凤",
                    "age": 26,
                    "sex": "女",
                    "tel": "19854458751"
                }
            }
        ]
    }
}

问:当索引下的数据过多时,如何实现分页查看呢?

-->分页查询

2. 分页查询(from|size)

打开postman,新建GET请求:

说明:from:起始页【起始页 = (页码-1)* 每页数据条数】

           size:每页数据条数

点击send,查看结果:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.0,
                "_source": {
                    "name": "张三",
                    "age": 14,
                    "sex": "男",
                    "tel": "12255864789"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1006",
                "_score": 1.0,
                "_source": {
                    "name": "李四",
                    "age": 28,
                    "sex": "男",
                    "tel": "15655889721"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.0,
                "_source": {
                    "name": "王五",
                    "age": 17,
                    "sex": "女",
                    "tel": "13254485621"
                }
            }
        ]
    }
}

问:现虚对所查询到的数据,根据某一属性,进行排序。

-->查询排序

3. 查询排序(sort)

打开postman,新建GET请求:

  • desc:降序   
  • asc:升序

 点击send,查看结果:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1006",
                "_score": null,
                "_source": {
                    "name": "李四",
                    "age": 28,
                    "sex": "男",
                    "tel": "15655889721"
                },
                "sort": [
                    28
                ]
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1003",
                "_score": null,
                "_source": {
                    "name": "李凤",
                    "age": 26,
                    "sex": "女",
                    "tel": "19854458751"
                },
                "sort": [
                    26
                ]
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1004",
                "_score": null,
                "_source": {
                    "name": "王五",
                    "age": 17,
                    "sex": "女",
                    "tel": "13254485621"
                },
                "sort": [
                    17
                ]
            }
        ]
    }
}

问:假若只想查看其中指定的一些属性

3.1 查看指定属性

打开postman,新建GET请求:

点击send,查看结果: 

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1006",
                "_score": null,
                "_source": {
                    "sex": "男",
                    "name": "李四",
                    "age": 28
                },
                "sort": [
                    28
                ]
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1003",
                "_score": null,
                "_source": {
                    "sex": "女",
                    "name": "李凤",
                    "age": 26
                },
                "sort": [
                    26
                ]
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1004",
                "_score": null,
                "_source": {
                    "sex": "女",
                    "name": "王五",
                    "age": 17
                },
                "sort": [
                    17
                ]
            }
        ]
    }
}

4. 多条件查询

4.1 must【同时满足条件A和条件B】

查询person索引下,年龄为17,并且性别为女的数据。

打开postman,新建GET请求:

 点击send,查看结果:

{
    "took": 19,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.8754687,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.8754687,
                "_source": {
                    "name": "王五",
                    "age": 17,
                    "sex": "女",
                    "tel": "13254485621"
                }
            }
        ]
    }
}

4.2 should【满足条件A和B其中一个】

查询person索引下,年龄为28,或者性别为女的数据。

打开postman,新建GET请求:

 点击send,查看结果:

{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1006",
                "_score": 1.0,
                "_source": {
                    "name": "李四",
                    "age": 28,
                    "sex": "男",
                    "tel": "15655889721"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1004",
                "_score": 0.87546873,
                "_source": {
                    "name": "王五",
                    "age": 17,
                    "sex": "女",
                    "tel": "13254485621"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1003",
                "_score": 0.87546873,
                "_source": {
                    "name": "李凤",
                    "age": 26,
                    "sex": "女",
                    "tel": "19854458751"
                }
            }
        ]
    }
}

5. 范围查询

查询person索引下,年龄大于17,小于22的数据。

  • gt:大于
  • gte:大于等于
  • lt:小于
  • lte:小于等于

打开postman,新建GET请求:

 点击send,查看结果:

{
    "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": "person",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.0,
                "_source": {
                    "name": "张三",
                    "age": 14,
                    "sex": "男",
                    "tel": "12255864789"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1006",
                "_score": 1.0,
                "_source": {
                    "name": "李四",
                    "age": 28,
                    "sex": "男",
                    "tel": "15655889721"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.0,
                "_source": {
                    "name": "王五",
                    "age": 17,
                    "sex": "女",
                    "tel": "13254485621"
                }
            },
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1003",
                "_score": 1.0,
                "_source": {
                    "name": "李凤",
                    "age": 26,
                    "sex": "女",
                    "tel": "19854458751"
                }
            }
        ]
    }
}

查询person索引下,性别为男,年龄小于27的数据。

打开postman,新建GET请求:

 点击send,查看结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.53899646,
        "hits": [
            {
                "_index": "person",
                "_type": "_doc",
                "_id": "1005",
                "_score": 0.53899646,
                "_source": {
                    "name": "张三",
                    "age": 14,
                    "sex": "男",
                    "tel": "12255864789"
                }
            }
        ]
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值