Elasticsearch Essentials——Elasticsearch查询操作
程序员文章站
2022-07-09 18:49:30
...
Query string
查询可以将需要查询的条件组装在一起形成字符串,来进行复杂的数据查询。例如GET /oa/employee/_search?q=gender:male&sort=age:desc
,这串查询的结果就是以age为降序排序的所有的male员工。查询结果为
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": null,
"hits": [
{
"_index": "oa",
"_type": "employee",
"_id": "4",
"_score": null,
"_source": {
"name": "li zong rui",
"age": 37,
"gender": "male",
"hobby": [
"basketball"
],
"address": {
"province": "beijing",
"city": "chaoyangqu",
"county": "chaoyangqu",
"details": "chaoyanglu"
}
},
"sort": [
37
]
},
{
"_index": "oa",
"_type": "employee",
"_id": "1",
"_score": null,
"_source": {
"name": "wang kai",
"age": 34,
"gender": "male",
"hobby": [
"basketball",
"football"
],
"address": {
"province": "jiangsu",
"city": "yancheng",
"county": "xiangshui",
"details": "hepingjie"
}
},
"sort": [
34
]
},
{
"_index": "oa",
"_type": "employee",
"_id": "3",
"_score": null,
"_source": {
"name": "li si",
"age": 30,
"gender": "male",
"hobby": [
"basketball"
],
"address": {
"province": "shandong",
"city": "jinan",
"county": "lixiaqu",
"details": "yulanguanchang"
}
},
"sort": [
30
]
},
{
"_index": "oa",
"_type": "employee",
"_id": "5",
"_score": null,
"_source": {
"name": "jiang kang jian",
"age": 25,
"gender": "male",
"hobby": [
"basketball"
],
"address": {
"province": "jiangsu",
"city": "suzhou",
"county": "huqiuqu",
"details": "disanlu"
}
},
"sort": [
25
]
}
]
}
}
Query DSL
查询所有的员工
GET /oa/employee/_search
{
"query": {
"match_all": {}
}
}
改写query string
GET /oa/employee/_search
{
"query": {
"match": {
"gender": "male"
}
},
"sort": [
{
"age": "desc"
}
]
}
分页查询
GET /oa/employee/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
查询结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "oa",
"_type": "employee",
"_id": "5",
"_score": 1,
"_source": {
"name": "jiang kang jian",
"age": 25,
"gender": "male",
"hobby": [
"basketball"
],
"address": {
"province": "jiangsu",
"city": "suzhou",
"county": "huqiuqu",
"details": "disanlu"
}
}
},
{
"_index": "oa",
"_type": "employee",
"_id": "4",
"_score": 1,
"_source": {
"name": "li zong rui",
"age": 37,
"gender": "male",
"hobby": [
"basketball"
],
"address": {
"province": "beijing",
"city": "chaoyangqu",
"county": "chaoyangqu",
"details": "chaoyanglu"
}
}
}
]
}
}
全文检索
GET /oa/employee/_search
{
"query": {
"match": {
"name": "wang yuan yuan"
}
}
}
查询结果
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2.3527396,
"hits": [
{
"_index": "oa",
"_type": "employee",
"_id": "2",
"_score": 2.3527396,
"_source": {
"name": "wang yuan yuan",
"age": 29,
"gender": "female",
"hobby": [
"games",
"dancing"
],
"address": {
"province": "shandong",
"city": "jinan",
"county": "zhangqiu",
"details": "moalingshanlu"
}
}
},
{
"_index": "oa",
"_type": "employee",
"_id": "1",
"_score": 0.25811607,
"_source": {
"name": "wang kai",
"age": 34,
"gender": "male",
"hobby": [
"basketball",
"football"
],
"address": {
"province": "jiangsu",
"city": "yancheng",
"county": "xiangshui",
"details": "hepingjie"
}
}
}
]
}
}
Query phrase
GET /oa/employee/_search
{
"query": {
"match_phrase": {
"name": "wang yuan yuan"
}
}
}
查询结果
{
"took": 38,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.8299086,
"hits": [
{
"_index": "oa",
"_type": "employee",
"_id": "2",
"_score": 1.8299086,
"_source": {
"name": "wang yuan yuan",
"age": 29,
"gender": "female",
"hobby": [
"games",
"dancing"
],
"address": {
"province": "shandong",
"city": "jinan",
"county": "zhangqiu",
"details": "moalingshanlu"
}
}
}
]
}
}
总结
full_text query和phrase query的区别是:full_text query会把需要查询的数据拆分匹配,例如这里面搜索的是wang yuan yuan,es会把wang,yuan,yuan,wang yuan等一系列排列组合放进去搜索,匹配到wang yuan yuan的是分数最高的,一些其他的会根据匹配的依次降分,所以全文检索出来的数据会比phrase query出来的数据多。而phrase query就是对单个term的完全匹配。
上一篇: elasticsearch操作
推荐阅读
-
使用Python操作Elasticsearch数据索引的教程
-
ElasticStack学习(十):深入ElasticSearch搜索之QueryFiltering、多/单字符串的多字段查询
-
Elasticsearch 操作
-
Java中使用elasticsearch搜索引擎实现简单、修改等操作
-
Elasticsearch实现复合查询高亮结果功能
-
elasticsearch 请求体查询方式整理
-
Python插入Elasticsearch操作方法解析
-
SpringDataElasticsearch操作Elasticsearch创建索引库以及创建映射
-
ElasticSearch之CURL操作
-
关于Elasticsearch文档的描述以及如何操作文档的详细总结