数据准备
DELETE /clayindex PUT /clayindex { "mappings": { "properties": { "name":{ "type": "keyword" }, "address":{ "type": "text" }, "age":{ "type": "integer" } } } } #插入数据 可以自己生成id // POST /clayindex/_doc { "name":"曹操", "address":"魏国", "age":18 } POST /clayindex/_doc { "name":"贾诩", "address":"魏国", "age":19 } POST /clayindex/_doc { "name":"诸葛亮", "address":"蜀国", "age":37 } POST /clayindex/_doc { "name":"关羽", "address":"蜀国", "age":35 } POST /clayindex/_doc { "name":"周瑜", "address":["吴国","蜀国"], "age":25 }
1、获取所有文档
GET /clayindex/_doc/_search { "query": { "match_all":{ } } }
2、分页查询,从第二条开始,查两条
GET /clayindex/_doc/_search { "query": { "match_all": {} }, "from": 1, "size": 2 }
这种分页方式如果进行深度分页,比如到100页,每页十条数据,它会从每个分片都查询出100*10条数据,假设有五个分片,就是5000条数据,然后在内存中进行排序,然后返回拍过序之后的集合中的第1000-1010条数据
3、指定查询出来的数据返回的字段
GET /clayindex/_doc/_search { "query": { "match_all": {} }, "_source": ["name","age"] }
返回的数据中返回name
和tages
字段。
4、address
字段中包含魏
GET /clayindex/_doc/_search { "query": { "match": { "address": "魏" } } }
返回的结果中元字段_score
有评分,说明使用query
会计算评分
5、address
字段中包含魏,并按照年龄升序排列
GET /clayindex/_doc/_search { "query": { "match": { "address": "魏" } }, "sort": [ { "age": { "order": "asc" } } ] }
6、年龄字段大于30
GET /clayindex/_doc/_search { "query": { "bool": { "filter": { "range": { "age": { "gte": 30 } } } } } }
返回的结果中元字段_score
字段等于0,没评分,说明使用filter
不会计算评分
7、 address
字段中包含魏,并且年龄字段大于10
GET /clayindex/_doc/_search { "query": { "bool": { "must": [ { "match": { "address": "魏" } } ], "filter": { "range": { "age": { "gte": 10 } } } } } }
8、查询address字段包含魏的文档的数量
GET /clayindex/_doc/_count { "query": { "match": { "address": "魏" } } }
9、排序
GET clayindex/_doc/_search { "query":{ "match": { "address": "魏国" } }, "_source":["name"], "sort":[ {"age":{"order":"desc"}}, {"name":{"order":"asc"}} ] }
10、分页查询
GET clayindex/_doc/_search { "query":{ "match": { "address": "魏国" } }, "_source":["name"], "sort":[ {"age":{"order":"desc"}}, {"name":{"order":"asc"}} ], "from":0, "size":10 }
11、布尔值查询
GET clayindex/_doc/_search { "query":{ "bool": { "must": [ { "match": { "address": "魏国" } },{ "match": { "name": "曹操" } } ] } }, "_source":["name"], "sort":[ {"age":{"order":"desc"}}, {"name":{"order":"asc"}} ], "from":0, "size":2 }
12、条件1 or 条件2
GET clayindex/_doc/_search { "query":{ "bool": { "should": [ { "match": { "address": "魏国" } },{ "match": { "name": "曹操" } } ] } }, "_source":["name"], "sort":[ {"age":{"order":"desc"}}, {"name":{"order":"asc"}} ], "from":0, "size":10 }
13、排除 MUST_NOT
#查询名字不是曹操的信息 GET clayindex/_doc/_search { "query":{ "bool": { "must_not": [ { "match": { "name": "曹操" } } ] } } }
14、数据过滤FILTER
gt 大于
gte 大于等于
lt 小于
lte 小于等于!
# 查询名字是曹操,然后年龄大于等于10 小于等于30
GET clayindex/_doc/_search { "query":{ "bool": { "must": [ { "match": { "name": "曹操" } } ], "filter": { "range": { "age": { "gte": 10, "lte": 20 } } } } } }
15、配备多个条件查询
GET clayindex/_doc/_search { "query":{ "match":{"address":"吴国 魏国 "} } }
16、高亮查询
GET clayindex/_doc/_search { "query":{ "match": { "name": "曹操" } }, "highlight":{ "fields": { "name": {} } } } GET clayindex/_doc/_search { "query":{ "match": { "address": "魏国" } }, "highlight":{ "fields": { "address": {} } } }
17、自定义高亮的风格
GET clayindex/_doc/_search { "query":{ "match": { "name": "曹操" } }, "highlight":{ "pre_tags": "<p class='gaoliang'>", "post_tags": "</p>", "fields": { "name": {} } } }
18、多个条件多个字段高亮查询
GET clayindex/_doc/_search { "query":{ "bool": { "must": [ { "match": { "address": "魏国" } },{ "match": { "name": "曹操" } } ] } }, "highlight":{ "pre_tags": "<p class='gaoliang'>", "post_tags": "</p>", "fields": [ {"name": {}}, {"address": {}} ] } }
gt 大于
2
gte 大于等于
3
lt 小于
4
lte 小于等于!
5
# 查询名字是曹操,然后年龄大于等于10 小于等于30