ElasticSearch基本介紹(四)搜索(Query、Filter、Aggregation)

準(zhǔn)備


導(dǎo)入測試用數(shù)據(jù)集

# 批量插入測試數(shù)據(jù)
POST /test_index/_bulk
{"index":{"_id":1}}
{"name":"zs","realname":"張三","age":22,"birthday":"2018-12-27","salary":1000.0,"address":"北京市昌平區(qū)沙陽路55號"}
{"index":{"_id":2}}
{"name":"ls","realname":"李四","age":24,"birthday":"2017-10-20","salary":5000.0,"address":"北京市朝陽區(qū)三里屯街道21號"}
{"index":{"_id":3}}
{"name":"ww","realname":"王五","age":28,"birthday":"2016-03-15","salary":4300.0,"address":"北京市海淀區(qū)中關(guān)村大街新中關(guān)商城2樓511室"}
{"index":{"_id":4}}
{"name":"zl","realname":"趙六","age":30,"birthday":"2003-04-19","salary":12300.0,"address":"北京市海淀區(qū)中關(guān)村軟件園9號樓211室"}
{"index":{"_id":5}}
{"name":"tq","realname":"田七","age":35,"birthday":"2001-08-11","salary":1403.0,"address":"北京市海淀區(qū)西二旗地鐵輝煌國際大廈負(fù)一樓"}

查詢(Query)


1. 查看所有并按照年齡降序排列

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query":{
    "match_all":{}  # 查詢所有
  },
  "sort":{
    "age":"desc"  # 按年齡倒序排列
  }
}

2. 查詢第2頁的用戶(每頁顯示2條)

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query":{
    "match_all":{}  # 查詢所有
  },
  "sort":{
    "age":"asc"   # 按年齡倒序排列
   },
  "from":2,         # 從(nowPage-1)*pageSize檢索
  "size":2          # 查 pageSize條
}

3. 查詢address在海淀區(qū)的所有用戶,并高亮

基于全文檢索的查詢(分析檢索關(guān)鍵詞 匹配索引庫 返回結(jié)果)

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query": {
    "match": {  #   match查詢會(huì)分詞 如:海淀區(qū) 會(huì)分詞為 海 | 淀 | 區(qū)
      "address":"海淀區(qū)"
    }
  },
  "highlight": {
    "fields": { #  需要高亮的字段列表
      "address": {}     
    }
  }
}

4. 查詢name是zs關(guān)鍵字的用戶

基于Term詞元查詢

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query":{
    "term": {
      "name": {
        "value": "zs"
      }
    }
  }
}

5. 查詢年齡在20~30歲之間的用戶

基于范圍查詢

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,   # 大于等于  大于用 gt
        "lte": 30    # 小于等于  小于用 lt
      }
    }
  }
}

6. 查詢真實(shí)姓名以張開頭的用戶

基于前綴(prefix)查詢

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query": {
    "prefix": {
      "realname": {
        "value": "李"
      }
    }
  }
}

7. 查詢名字以s結(jié)尾的用戶

基于通配符(wildcard)的查詢

  • ?匹配一個(gè)字符
  • *匹配0~n個(gè)字符

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "*s"
      }
    }
  }
}

8. 查詢id為1,2,3的用戶

基于Ids的查詢

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query": {
    "ids": {
      "values": [1,2,3]
    }
  }
}

9. 模糊查詢r(jià)ealname中包含張關(guān)鍵字的用戶

基于Fuzzy的查詢

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query": {
    "fuzzy": {
      "realname": {"value": "張"}
    }
  }
}

10. 查詢age在15-30歲之間并且name必須通配z*

基于Boolean的查詢(多條件查詢)

  • must:查詢結(jié)果必須符合該查詢條件(列表)。
  • should:類似于or的查詢條件。
  • must_not:查詢結(jié)果必須不符合查詢條件(列表)。

DSL實(shí)現(xiàn):

GET /test_index/_search
{
  "query": {
    "bool": {
      "must": [    #年齡在15~30歲之間并且必須名字通配z*
        {
          "range": {
            "age": {
              "gte": 15,
              "lte": 30
            }
          }
        },
        {
          "wildcard": {
            "name": {
              "value": "z*"
            }
          }
        }
      ],
      "must_not": [  # 正則查詢 name必須不能以s結(jié)尾
        {
          "regexp": {
            "name": ".*s"
          }
        }
      ] 
    }
  }
}

過濾器(Filter)


準(zhǔn)確來說,ES中的查詢操作分為兩種:查詢(query)和過濾(filter)。

查詢即是之前提到的query查詢,查詢默認(rèn)會(huì)計(jì)算每個(gè)返回文檔的得分,然后根據(jù)得分排序。

而過濾(filter)只會(huì)篩選出符合的文檔,并不計(jì)算得分,且它可以緩存文檔。

所以,單從性能考慮,過濾比查詢更快。

換句話說,過濾適合在大范圍篩選數(shù)據(jù),而查詢則適合精確匹配數(shù)據(jù)。

一般應(yīng)用時(shí),應(yīng)先使用過濾操作過濾數(shù)據(jù),然后使用查詢匹配數(shù)據(jù)。

過濾器使用

GET /test_index/_search
{
   "query":{
      "bool": {
        "must": [
          {"match_all": {}}
        ],
        "filter": {     # 過濾年齡大于等于25歲的用戶
          "range": {
            "age": {
              "gte": 25
            }
          }
        }
      }
   }
}

注意: 過濾查詢運(yùn)行時(shí)先執(zhí)行過濾語句,后執(zhí)行普通查詢

過濾器的類型

1. term、terms Filter

term、terms的含義與查詢時(shí)一致。term用于精確匹配,terms用于多詞條匹配

GET /test_index/_search
{
   "query":{
      "bool": {
        "must": [
          {"match_all": {}}
        ],
        "filter": {
          "terms": {
            "name": [
              "zs",
              "ls"
            ]
          }
        }
      }
   }
}
2. range filter
3. exists filter

exists過濾指定字段沒有值的文檔

GET /test_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": {    # 排除salary為null的結(jié)果
        "exists": {
          "field": "salary"
        }
      }
    }
  },
  "sort": [
    {
      "_id": {
        "order": "asc"
      }
    }
  ]
}

相反操作(查詢出salary為null的結(jié)果)

GET /test_index/_search
{ 
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "salary"
          }
        }
      ]
    }
  }
}
4. ids filter

需要過濾出若干指定_id的文檔,可使用標(biāo)識符過濾器(ids)

GET /test_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "address": "昌平區(qū)"
          }
        }
      ],
      "filter": {
        "ids": {   # id 過濾器
          "values": [
            1,
            2,
            3
          ]
        }
      }
    }
  }
}
5. 其余使用方式可查閱官網(wǎng)

聚合(Aggregations)


官方文檔 6.x search-aggregations

聚合提供了功能可以分組并統(tǒng)計(jì)你的數(shù)據(jù)。

理解聚合最簡單的方式就是可以把它粗略的看做SQLGROUP BY操作和SQL聚合函數(shù)。

ES中常用的聚合:

  • metric(度量)聚合:度量類型聚合主要針對的number類型的數(shù)據(jù),需要ES做比較多的計(jì)算工作,類似于關(guān)系型數(shù)據(jù)庫的組函數(shù)操作
  • bucketing(桶)聚合:劃分不同的“桶”,將數(shù)據(jù)分配到不同的“桶”里。非常類似sql中的group語句的含義,類似于關(guān)系型數(shù)據(jù)庫的分組操作
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容