Request Body Search
-
将查询语句通过HTTP Request Body发送给ElasticSearch
-
Query DSL
POST /movies,404_idx/_search?ignore_unavailable=true
{
"profile": true,
"query": {
"match_all": {}
}
}
分页
POST /kibana_sample_data_ecommerce/_search
{
"from":10,
"size":20,//(此处查询20个结果)
"query":{
"match_all": {}
}
}
-
from 从0开始,默认返回10个结果
-
获取靠后的翻页成本较高
排序
POST kibana_sample_data_ecommerce/_search
{
"sort":[{"order_date":"desc"}],
"query":{
"match_all": {}
}
}
-
最好在"数字型"与"日期型"字段上排序
-
因为对于多值类型或分析过的字段排序,系统会选一个值,无法得知该值
_source filtering
#source filtering
POST kibana_sample_data_ecommerce/_search
{
"_source":["order_date","user*","category.keyword"],
"from":10,
"size":5,
"query":{
"match_all": {}
}
}
#返回结果
"hits" : [
{
"_index" : "kibana_sample_data_ecommerce",
"_type" : "_doc",
"_id" : "Ys9u0nABbRdUz8AoIfBO",
"_score" : 1.0,
"_source" : {
"order_date" : "2020-03-16T10:46:34+00:00",
"user" : "rabbia"
}
},
-
如果_source没有存储,那就只返回匹配的文档的元数据
-
_source支持使用通配符,_source["name","desc"]
脚本字段
GET kibana_sample_data_ecommerce/_search
{
"script_fields": {
"new_field": {
"script": {
"lang": "painless",
"source": "doc['order_date'].value+'hello'"
}
}
},
"query": {
"match_all": {}
}
}
#返回结果
"hits" : [
{
"_index" : "kibana_sample_data_ecommerce",
"_type" : "_doc",
"_id" : "WM9u0nABbRdUz8AoIfBN",
"_score" : 1.0,
"fields" : {
"new_field" : [
"2020-03-23T09:28:48.000Zhello"
]
}
},
- 用例:订单中有不同的汇率,需要结合汇率对订单价格进行排序
使用查询表达式 - Match
POST movies/_search
{
"query": {
"match": {
"title": "last christmas" #OR
}
}
}
POST movies/_search
{
"query": {
"match": {
"title": {
"query": "last christmas",
"operator": "and"
}
}
}
}
短语搜索 - Match Phrase
POST movies/_search
{
"query": {
"match_phrase": {
"title":{
"query": "one love",
"slop": 1,#允许一个空格字符
}
}
}
}
课程Demo
- 需要通过 Kibana 导入Sample Data的电商数据。具体参考“2.2节-Kibana的安装与界面快速浏览”
- 需导入Movie测试数据,具体参考“2.4-Logstash安装与导入数据”
#ignore_unavailable=true,可以忽略尝试访问不存在的索引“404_idx”导致的报错
#查询movies分页
POST /movies,404_idx/_search?ignore_unavailable=true
{
"profile": true,
"query": {
"match_all": {}
}
}
POST /kibana_sample_data_ecommerce/_search
{
"from":10,
"size":20,
"query":{
"match_all": {}
}
}
#对日期排序
POST kibana_sample_data_ecommerce/_search
{
"sort":[{"order_date":"desc"}],
"query":{
"match_all": {}
}
}
#source filtering
POST kibana_sample_data_ecommerce/_search
{
"_source":["order_date"],
"query":{
"match_all": {}
}
}
#脚本字段
GET kibana_sample_data_ecommerce/_search
{
"script_fields": {
"new_field": {
"script": {
"lang": "painless",
"source": "doc['order_date'].value+'hello'"
}
}
},
"query": {
"match_all": {}
}
}
POST movies/_search
{
"query": {
"match": {
"title": "last christmas"
}
}
}
POST movies/_search
{
"query": {
"match": {
"title": {
"query": "last christmas",
"operator": "and"
}
}
}
}
POST movies/_search
{
"query": {
"match_phrase": {
"title":{
"query": "one love"
}
}
}
}
POST movies/_search
{
"query": {
"match_phrase": {
"title":{
"query": "one love",
"slop": 1
}
}
}
}
相关阅读
网友评论