elasticsearch Request Body 與 Query DSL詳解

ghostwritten發表於2020-11-02

1. Request Body Search(query.match_all)

  • 將查詢語句通過 HTTP Request Body 傳送給 Elasticsearch
  • Query DSL
POST /movies,404_idx/_search?ignore_unavailable=true
{
  "profile": "true",
  "query": {
    "match_all": {}
  }
}
//返回全部

1.1 分頁

  • From 從 0 開始 預設返回 10 個結果
  • 獲取靠後的翻頁,成本較高
POST /kibana_sample_data_ecommerce/_search
{
  "from":10,
  "size":20,
  "query": {
    "match_all":{}
  }
}

1.2 排序

  • 最好在 “數字型” 與 “日期型” 欄位上排序
  • 因為對於多值型別或分析過的欄位排序,系統會選一個值,無法得知該值
POST /kibana_sample_data_ecommerce/_search
{
  "sort":[{"order_date":"desc"}],
  "from":10,
  "size":20,
  "query": {
    "match_all":{}
  }
}
//返回10個並按照日期排序

1.3 _source 過濾

  • 如果_source 沒有儲存,那就只返回匹配的文件後設資料
  • _source 支援使用萬用字元
  • _source[“name* “,”desc*”]
GET /kibana_sample_data_ecommerce/_search
{
  "_source": ["order_date","order_date","category.keyword"],
  "from":10,
  "size":20,
  "query": {
    "match_all":{}
  }
}
//返回_source含有"order_date","order_date","category.keyword"的值
例如
{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4675,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "kibana_sample_data_ecommerce",
        "_type" : "_doc",
        "_id" : "EFGvgnUBh8v6HnS077dk",
        "_score" : 1.0,
        "_source" : {
          "order_date" : "2020-10-30T16:29:17+00:00"
        }
      },

1.4 指令碼欄位

  • 能算出新的欄位
  • 用例:訂單中有不同的匯率,需要結合匯率對訂單價格進行排序
 #指令碼欄位
GET kibana_sample_data_ecommerce/_search
{
  "script_fields": {
    "new_field": {
      "script": {
        "lang": "painless",
        "source": "doc['order_date'].value+'hello'"
      }
    }
  },
  "query": {
    "match_all": {}
  }
}

1.5 使用查詢表示式 - Match(query.match)

POST movies/_search
{
  "query": {
    "match": {
      "title": "Last Christmas"  // 相當於 OR 可出現其中1個
    }
  }
}

POST movies/_search
{
  "query": {
    "match": {
      "title": {
        "query": "Last Christmas",
        "operator": "AND"             //相當於 and 只能兩個都出現
      }
    }
  }
}

1.6 短語搜尋 - Match Phrase(query.match_phrase)

POST movies/_search
{
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love"

      }
    }
  }
}
返回0

POST movies/_search
{
  "query": {
    "match_phrase": {
      "title":{
        "query": "one love",
        "slop":1 
      }
    }
  }
}
返回1個

1.7 query.query_string

  • 類似 URI Query – 把查詢條件放在 POST 裡面
//準備工作
  PUT /users/_doc/3
  {
    "name" : "Li Sunke",
    "about": "php,elasticsearch,redis,nginx,swoole"
  }

  PUT /users/_doc/4
  {
    "name" : "Qu Sunke",
    "about": "mysql,php"
  }
  //query_string
  POST /users/_search
  {
    "query": {
      "query_string": {
        "query": "Li AND Sunke",
        "default_field": "name"
      }
    }
  }
  //返回1個結果
  "hits" : [
        {
          "_index" : "users",
          "_type" : "_doc",
          "_id" : "3",
          "_score" : 0.87546873,
          "_source" : {
            "name" : "Li Sunke",
            "about" : "php,elasticsearch,redis,nginx,swoole"
          }
        }
      ]
  //query string 支援分組查詢多個欄位
  // 返回 doc_3
  POST /users/_search
  {
    "query": {
      "query_string": {
        "fields": ["name","about"],
        "query": "(Li AND Sunke) OR (php AND nginx)"
      }
    }
  }
  返回一個結果
  "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.9899296,
        "_source" : {
          "name" : "Li Sunke",
          "about" : "php,elasticsearch,redis,nginx,swoole"
        }
      }
    ]

1.8 query.simple_query_string

  • 類似 Query String , 但是會忽略錯誤的語法同時只支援部分查詢語句

  • 不支援 AND OR NOT , 但會當作字串處理

  • Term 之間預設的關係是 OR, 可以指定 Operator

  • 支援 部分邏輯

  • ·替代 AND

  • | 替代 OR

  • -替代 NOT

// Simple Query 預設的operator 是 Or
// AND 會當做一個字串,所以會 3個欄位OR查詢

POST /users/_search
{
"query": {
 "simple_query_string": {
   "query": "Li  AND Sunke", 
   "fields": ["name"],
 }
}
}
//返回兩個

POST /users/_search
{
"query": {
 "simple_query_string": {
   "query": "Li  AND Sunke", 
   "fields": ["name"],
   "default_operator": "AND"
 }
}
}
//返回0個



POST /users/_search
{
"query": {
"simple_query_string": {
"query": "Li Sunke",
"fields": ["name"],
"default_operator": "AND"
}
}
}
//返回1個

參考資料:
極客時間:Elasticsearch核心技術與實戰
相關閱讀:
初學elasticsearch入門
Elasticsearch本地安裝與簡單配置
docker-compose安裝elasticsearch叢集
Elasticsearch 7.X之文件、索引、REST API詳解
Elasticsearch節點,叢集,分片及副本詳解
Elasticsearch倒排索引介紹
elasticsearch Analyzer 進行分詞詳解
elasticsearch search API詳解
Elasticsearch URI Search 查詢方法詳解

相關文章