ElasticSearch之QueryDSL查询语法
目錄
創(chuàng)建索引:
插入測試數(shù)據(jù)
QueryDSL用法
查詢所有
排序
查詢n條數(shù)據(jù)
分頁查詢
查詢結(jié)果返回指定列
關(guān)鍵字term查詢
范圍查詢(range)
前綴查詢(prefix)
通配符查詢(wildcard)
根據(jù)多個id查詢
模糊查詢(fuzzy)
高亮查詢
自定義高亮標(biāo)簽
創(chuàng)建索引:
請求頭 put 192.168.0.101:9200/log_index/請求體:{"mappings":{"log_type":{"properties":{"name":{"type":"keyword"},"age":{"type":"integer"},"birthday":{"type": "date"}}}} }插入測試數(shù)據(jù)
請求頭 PUT 192.168.0.101:9200/log_index/log_type/_bulk請求體:{"index":{}}{"name":"小黑","age":23,"birthday":"2012-12-12"}{"index":{}}{"name":"王小黑","age":24,"birthday":"2012-12-13"}{"index":{}}{"name":"張小五","age":8,"birthday":"2012-12-14"}{"index":{}}{"name":"win7","age":9,"birthday":"2012-12-15"}{"index":{}}{"name":"梅超風(fēng)","age":43,"birthday":"2012-12-16"}{"index":{}}{"name":"張無忌","age":59,"birthday":"2012-12-17"}QueryDSL用法
查詢所有
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"match_all":{}}}注:match_all 而不是match-all中間是下劃線
排序
說明:多字段排序,先按照出生日期降序排列,如果出生日期相同則按照年齡排序
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"match_all":{}},"sort":{"birthday":"desc","age":"asc"}}或者
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"match_all":{ }},"sort": [{"birthday":{"order":"desc" }},{"age":{"order":"asc"}}] }兩種寫法結(jié)果一樣
查詢n條數(shù)據(jù)
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"match_all":{ } },"size":2,"sort": [{"birthday":{"order":"desc" }},{"age":{"order":"asc"}}] }注意:size的位置適合query關(guān)鍵字并列,而不是在query關(guān)鍵字內(nèi)部
分頁查詢
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體:{"query":{"match_all":{ } },"size":2,"from":1,"sort": [{"birthday":{"order":"desc" }},{"age":{"order":"asc"}}] }同理:from和size是并列的,from表示從第幾頁查詢,size表示每頁幾條數(shù)據(jù)
查詢結(jié)果返回指定列
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"match_all":{ } },"size":2,"from":1,"_source":["name","age"]}注:關(guān)鍵字_source表示要顯示的指定列,且指定列以數(shù)組的形式表現(xiàn)?
關(guān)鍵字term查詢
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體:{"query":{"term":{"name":"梅超風(fēng)"}},"_source":["name","age"] }或者請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體:{"query":{"term":{"name":{"value":"梅超風(fēng)"}}},"_source":["name","age"] }注:使用term關(guān)鍵字查詢是特別需要注意text類型和keywordl類型的字段?
NOTE1: ?通過使用term查詢得知ES中默認(rèn)使用分詞器為標(biāo)準(zhǔn)分詞器(StandardAnalyzer),標(biāo)準(zhǔn)分詞器對于英文單詞分詞,對于中文單字分詞
NOTE2: ?通過使用term查詢得知,在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 這些類型不分詞**,**只有text類型分詞
范圍查詢(range)
說明:查詢年齡大于等于33小于等于35歲的文檔數(shù)據(jù)
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"range":{"age":{"gte":33,"lte":35}}},"_source":["name","age"] }注:其中g(shù)te表示大于等于,lte表示小于等于,gt表示大于,lt表示小于
注:關(guān)鍵字range在query內(nèi)部,而查詢的關(guān)鍵字在range內(nèi)部
前綴查詢(prefix)
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"prefix":{"name":{"value":"梅超"}}},"_source":["name","age"] }注:這里和term關(guān)鍵字有所區(qū)別,我的name字段類型是keyword(keyword默認(rèn)是不分詞),如果是term查詢結(jié)果則是無數(shù)據(jù),而使用perfix查詢時則表示查詢name是“梅超”前綴的修飾的文檔數(shù)據(jù)。
通配符查詢(wildcard)
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"wildcard":{"name":{"value":"梅*"}}},"_source":["name","age"] } 請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"wildcard":{"name":{"value":"梅?"}}},"_source":["name","age"] }注:*表示可以匹配一個或多個字符,而?只能匹配零個或一個,同時通配符要放到索引詞的后面
根據(jù)多個id查詢
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"ids":{"values":["AXnciHtbCxsROukG0JQu","AXnciHtbCxsROukG0JQv"]}},"_source":["name","age"] }注:這里ids和values都是關(guān)鍵字(千萬不要寫成id或value!!!!),且values的值是數(shù)組的形式展現(xiàn)
模糊查詢(fuzzy)
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體:{"query":{"fuzzy":{"name":{"value":"梅花風(fēng)"}}},"_source":["name","age"] }注:fuzzy 模糊查詢 ?最大模糊錯誤 必須在0-2之間
? ? ? ? # 搜索關(guān)鍵詞長度為 2 不允許存在模糊 0
? ? ? ? # 搜索關(guān)鍵詞長度為3-5 允許一次模糊 0 1?
? ? ? ?# 搜索關(guān)鍵詞長度大于5 允許最大2模糊
如上梅超風(fēng)三個字,則允許出現(xiàn)一個錯誤(官方文檔這樣定義的),因此查詢梅花風(fēng)也是能查詢到數(shù)據(jù)的
bool查詢
說明: 用來組合多個條件實(shí)現(xiàn)復(fù)雜查詢
> ??? ?**must: 相當(dāng)于&& 同時成立**
> ??? ?**should: 相當(dāng)于|| 成立一個就行**
> ??? ?**must_not: 相當(dāng)于! ?不能滿足任何一個**
說明:查詢年齡大于等于33小于等于35并且名稱是小黑1的文檔信息
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體:{"query":{"bool":{"must":[{ "term":{"name":{"value":"小黑1"}}} ,{"range":{"age":{"gte":33,"lte":35}}}]}},"_source":["name","age"] }注意的是多個條件查詢是比如must后面則是數(shù)組格式(內(nèi)部是json格式的對象),而單個條件查詢時則是json對象如下圖
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體:{"query":{"bool":{"must": {"range":{"age":{"gte":33,"lte":35}}}}},"_source":["name","age"] }高亮查詢
說明:關(guān)鍵字梅超風(fēng),所有字段含有梅超風(fēng)的文檔數(shù)據(jù)都高亮顯示
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"bool":{"must": {"term":{"name":{"value":"梅超風(fēng)"}}}}},"highlight":{"fields":{"*":{}}},"_source":["name","age"] }?
自定義高亮標(biāo)簽
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體: {"query":{"bool":{"must": {"term":{"name":{"value":"梅超風(fēng)"}}}}},"highlight":{"pre_tags": ["<span style='color:red'>"],"post_tags": ["</span>"],"fields":{"*":{}}},"_source":["name","age"] }?
多字段查詢
說明:在desc和content字段中查詢名稱是北京的文檔數(shù)據(jù)(這里的desc,content都是text類型,name是keyword類型)。
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體 {"query":{"multi_match":{"query": "北京","fields":["desc","content"]}},"_source":["name","age","desc","content"] }請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體{"query":{"multi_match":{"query": "北京","fields":["desc","content","name"]}},"_source":["name","age","desc","content"] }?1.多字段查詢時需要注意字段類型,這里的age定義的是integer類型因此這樣查詢報錯
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體 {"query":{"multi_match":{"query": "北京","fields":["desc","content","name","age"]}},"_source":["name","age","desc","content"] }?
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體{"query":{"multi_match":{"query": "北京歡迎","fields":["desc","content","name"]}},"_source":["name","age","desc","content"] }?
注:如果對搜索的字段分詞(即搜索的字段是text),則對query先分詞在搜索,如果對搜索的字段不分詞則直接使用query整體進(jìn)行搜索。
多字段分詞查詢
請求頭: GET 192.168.0.101:9200/log_index/log_type/_search請求體 {"query":{"query_string":{"query": "北京","fields":["desc","content","name"]}},"_source":["name","age","desc","content"] }?
?
總結(jié)
以上是生活随笔為你收集整理的ElasticSearch之QueryDSL查询语法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C、C++ 学习经历
- 下一篇: .net string format