Elasticsearch 第三篇:查询详解
从第一篇开始,我用的ES版本就是7.8.0的,与低版本略有不同,不同点可以参考官方介绍,最大的不同就是抛弃 type 这一概念,为了方便测试,首先建立一个学生成绩的索引库(在建立的同时,规定字段类型,并指定IK中文分词)
PUT http://localhost:9200/db_student
{ "mappings": { "properties": { "class": { "type": "integer", "store": true, "index":true }, "chinese": { "type": "integer", "store": true, "index":true }, "english": { "type": "integer", "store": true, "index":true }, "math": { "type": "integer", "store": true, "index":true }, "name": { "type": "text", "store": true, "index":true }, "school": { "type": "text", "store": true, "index":true, "analyzer":"ik_max_word" } } } }
为了方便测试,需要先插入测试数据,如下,一共插入8条记录
PUT http://localhost:9200/db_student/_doc/1 { "chinese": 80, "class": 10, "english": 90, "math": 100, "name": "Vincent", "school": "华南理工大学" } PUT http://localhost:9200/db_student/_doc/2 { "chinese": 80, "class": 11, "english": 85, "math": 90, "name": "Kitty", "school": "华南理工大学" } PUT http://localhost:9200/db_student/_doc/3 { "chinese": 90, "class": 12, "english": 65, "math": 70, "name": "Thomas", "school": "华南师范大学" } PUT http://localhost:9200/db_student/_doc/4 { "chinese": 70, "class": 12, "english": 85, "math": 85, "name": "Lucy", "school": "华南师范大学" } PUT http://localhost:9200/db_student/_doc/5 { "chinese": 60, "class": 12, "english": 95, "math": 95, "name": "Lily", "school": "华南农业大学" } PUT http://localhost:9200/db_student/_doc/6 { "chinese": 87, "class": 12, "english": 55, "math": 98, "name": "Coco", "school": "华南农业大学" } PUT http://localhost:9200/db_student/_doc/7 { "chinese": 88, "class": 12, "english": 65, "math": 85, "name": "Allen", "school": "中山大学" } PUT http://localhost:9200/db_student/_doc/8 {
"chinese": 77, "class": 12, "english": 45, "math": 89, "name": "Zack", "school": "中山大学" }
打开 Kibana 可以看到已经插入的数据,如下
数据已经插入,现在可以来实现文档的复杂查询了,Elasticsearch 的检索 API 格式如下:
GET /索引库名/_search { "query": { "查询类型": { "查询条件": "查询条件值" } } }
查询类型包括 match_all,match (应用于类型为text的字段检索),term(等于或者词条包含),range(范围查询),fuzzy,bool 等等,下面举例说明。
1、查询所有索引库、所有文档
GET http://localhost:9200/_search
{
"query": {
"match_all": {}
}
}
2、查询索引库 db_student 所有文档
GET http://localhost:9200/db_student/_search
{
"query": {
"match_all": {}
}
}
3、根据文档编号 id=1 来获取文档
GET http://localhost:9200/db_student/_doc/1
4、查询 class=10 的学生
注意:term 在这里相当于 = 的逻辑,但是如果是字符串,还可以是包含的逻辑。
GET http://localhost:9200/db_student/_search { "query": {
"term":{
"class":10
}
} }
5、And 逻辑查询,如查询 class=10 并且 name=vincent 的文档
GET http://localhost:9200/db_student/_search { "query": { "bool":{ "must":[ {"term":{"name":"vincent"}}, {"term":{"class":10}} ] } } }
6、模糊查询,例如,查询 school 包含 “华南” 的文档
GET http://localhost:9200/db_student/_search { "query": { "match":{"school":"华南"} } }
也可以是 term
GET http://localhost:9200/db_student/_search { "query": { "term":{"school":"华南"} } }
7、范围查询,查询 english 大于等于90,小于等于100的文档
注意:from、to 都是闭包的,逻辑上是大于等于、小于等于,
gte 和 lte 也是一样的逻辑。
GET http://localhost:9200/db_student/_search { "query": {
"range":{
"english": {"from": 90, "to": 99}
} } }
还可以查大于、小于的逻辑,例如查询 english 大于90的文档
注意:gt 表示大于, lt 表示小于 ,这两者都不包含等于
GET http://localhost:9200/db_student/_search { "query": {
"range": {
"english": { "gt": 90 }
} } }
8、高亮显示,例如 name 高亮
GET http://localhost:9200/db_student/_search { "query": { "term":{"name":"vincent"} }, "highlight":{ "pre_tags" : "<a class='red'>", "post_tags" : "</a>", "fields" : { "name" : {} } } }
查询结果是:
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.7917595, "hits": [ { "_index": "db_student", "_type": "_doc", "_id": "1", "_score": 1.7917595, "_source": { "chinese": 80, "class": 10, "english": 90, "math": 100, "name": "Vincent", "school": "华南理工大学" }, "highlight": { "name": [ "<a class='red'>Vincent</a>" ] } } ] } }
9、分页和排序,先按照 english 倒序,再按 math 升序,每页3条记录,取第一页
GET http://localhost:9200/db_student/_search { "query": { "match_all":{} }, "from": 0, "size": 3, "sort":{ "english" : {"order" : "desc"}, "math": {"order" : "asc"} } }
查询结果是
{ "took": 0, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 8, "relation": "eq" }, "max_score": null, "hits": [ { "_index": "db_student", "_type": "_doc", "_id": "5", "_score": null, "_source": { "chinese": 60, "class": 12, "english": 95, "math": 95, "name": "Lily", "school": "华南农业大学" }, "sort": [ 95, 95 ] }, { "_index": "db_student", "_type": "_doc", "_id": "1", "_score": null, "_source": { "chinese": 80, "class": 10, "english": 90, "math": 100, "name": "Vincent", "school": "华南理工大学" }, "sort": [ 90, 100 ] }, { "_index": "db_student", "_type": "_doc", "_id": "4", "_score": null, "_source": { "chinese": 70, "class": 12, "english": 85, "math": 85, "name": "Lucy", "school": "华南师范大学" }, "sort": [ 85, 85 ] } ] } }
更多查询,还可以参考这篇博文 https://www.cnblogs.com/chenhuabin/p/13983303.html
聚合查询、统计查询等更加复杂的查询, 后面的文章将会补上