ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ## **测试数据准备:官方样例数据accounts.json** 在开始探索之前,我们先下载官方提供的样例数据集,导入到我们的集群中。 官方样例数据集下载地址:[右键下载](https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true) 百度云盘链接: https://pan.baidu.com/s/15wtt3olKf06KxugXSqMq2w 提取码: vse4 将下载的accounts.json 上传到当前ES服务器目录中,执行以下命令 ``` curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json" ``` 执行成功后,查询索引 ``` root@ubuntu:/home/guanfuchang# curl -X GET "localhost:9200/_cat/indices?v" health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open bank TadPjNB3T9G4qDm5j8SVwA 5 1 1000 0 506.1kb 506.1kb yellow open customer H4FuIykjRIeAbPij8yLRug 5 1 4 0 14.2kb 14.2kb ``` 发现已经新增了索引`bank`,其中有1000个document。 ## **搜索API `_search `** 我们有两种方式进行搜索: - 在请求URL中传参 ``` curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty" ``` - 在请求BODY中传参 ``` curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ] } ' ``` 通常,我们会选择在BODY中使用JSON格式进行传参。 上面两种方式,查询的结果是一样的。**查询关键字**为*,代表所有值。**排序**是根据account_number升序,**默认是返回10条数据**。 返回格式如下: ![](https://box.kancloud.cn/42b102ff6f9d32ab6e018b39fb38ba7c_640x838.png) ### **查询语句介绍** #### **基本参数选项:query、from、size、sort、_source** ``` GET /bank/_search { "query": {"match_all": {}}, "from": 10, "size": 2, "sort": [ {"balance": { "order": "desc" } } ], "_source":["account_number","balance"] } ``` **query**:指定查询条件,这里使用` { "match_all": {} }`表示查询条件匹配所有记录 **from**:表示从第n条匹配记录开始取值,默认为0 **size**:表示匹配条数,默认10 **sort**:表示排序,这里使用`{ "balance": { "order": "desc" }}`,表示按balance降序排序,这里也可以写成`[{ "balance": "desc" }]` **_source**:表示查询字段,这里使用` ["account_number", "balance"] `表示返回结果中,只需要返回"account_number", "balance"两个字段即可。默认返回所有字段。 上面的查询结果如下: ![](https://box.kancloud.cn/e0011bac6579703788f52950a27e17ab_443x858.png) #### **查询匹配条件** 上面例子中,我们在选项query中,使用了` { "match_all": {} }`表示查询条件匹配所有记录,下面以一系列的例子介绍各种匹配条件 ##### **match查询**:查询 account_number=20 的document ``` GET /bank/_search { "query": { "match": {"account_number": 20} } } ``` 查询结果如下: ![](https://box.kancloud.cn/ba6b7bc0ff162aa6b760dc4c0244deef_457x710.png) ##### **match**查询:查询 address 中包含 “mill” 的document ``` GET /bank/_search { "query": { "match": {"address": "mill"} } } ``` ##### **match**查询:查询 address 中包含 “mill” 或者 “lane” 的document ``` GET /bank/_search { "query": { "match": {"address": "mill lan"} } } ``` ##### **match_phrase**查询:查询 address 中包含短语 “mill lane” 的document ``` GET /bank/_search { "query": { "match_phrase": {"address": "mill lane"} } } ``` ##### **bool and**关系查询:查询 address 中同时包含 “mill” 和 “lane” 的document ``` GET /bank/_search { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } } ``` ##### **bool or** 关系查询:查询 address 中包含 “mill” 或者 “lane” 的document ``` GET /bank/_search { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } } ``` ##### **bool not**关系查询:查询 address 中即不存在 “mill” 也不存在 “lane” 的document ``` GET /bank/_search { "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } } ``` ##### **bool 组合**查询:查询 age=40,state!="ID" 的document ``` GET /bank/_search { "query": { "bool": { "must": [ { "match": { "age": 40 } } ], "must_not": [ { "match": { "state": "ID" } } ] } } } ``` ##### **bool filter**查询:查询 20000<=balance<=30000 的document ``` GET /bank/_search { "query": { "bool": { "filter":{ "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } } ```