結構化查詢語句DSL的使用,bool,filter查詢等
新增資料集
curl -XPUT -H "Content-Type: application/json" 'localhost:9201/blog/article/7?pretty' -d '
{
"title": "elk搭建日志采集系統",
"content":"elk elasticsearch logstash kibana",
"PV":18
}'
什麼是query DSL
1、Domain Specific Language 領域特定語言
2、Elasticsearch提供了完整的查詢DSL,基于JSON定義查詢
3、用于構造複雜的查詢語句
url查詢(空格處理不當,會出問題)
curl -XPOST -H "Content-Type: application/json" 'http://localhost:9201/blog/article/_search' -d '{
"query" : {
"term" : { "title" : "elk" }
}
}'
建議使用postman工具
post方式送出,增加http頭資訊
body裡面選row格式,粘貼對應的dsl即可
bool查詢入門
{
"query": {
"bool": {
"must": [
{ "match": { "title": "elk" } }
],
"must_not": [
{ "match": { "title": "小D" } }
]
}
}
}
filter查詢入門(filtered文法已經在5.0版本後移除了,在2.0時候标記過期,改用filter )
參考位址:https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-filtered-query.html
{
"query": {
"bool": {
"filter": {
"range": {
"PV": {
"gt": 15
}
}
},
"must": {
"match": {
"title": "ELK"
}
}
}
}
}
總結:(官網參考 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html)
1、大部分filter的速度快于query的速度
2、filter不會計算相關度得分,且結果會有緩存,效率高
3、全文搜尋、評分排序,使用query
4、是非過濾,精确比對,使用filter