網站首頁 編程語言 正文
Elasticsearch查詢
查詢分類:
基本查詢:使用es內置查詢條件進行查詢
組合查詢:把多個查詢組合在一起進行復合查詢
過濾:查詢的同時,通過filter條件在不影響打分的情況下篩選數據
一 基本查詢
#添加映射
PUT lago
{
"mappings": {
"properties":{
"title":{
"stort":true,
"type":"text",
"analyzer":"ik_max_word"
},
"company_name":{
"stort":true,
"type":"keyword",
},
"desc":{
"type":"text"
},
"comments":{
"type":"integer"
},
"add_time":{
"type":"date",
"format":"yyy-MM-dd"
}
}
}
}
#測試數據
POST lago/job
{
"title":"python django 開發工程師",
"company_name":"美團科技有限公司",
"desc":"對django熟悉,掌握mysql和非關系型數據庫,網站開發",
"comments:200,
"add_time":"2018-4-1"
}
POST lago/job
{
"title":"python數據分析",
"company_name":"百度科技有限公司",
"desc":"熟悉python基礎語法,熟悉數據分析",
"comments:5,
"add_time":"2018-10-1"
}
POST lago/job
{
"title":"python自動化運維",
"company_name":"上海華為",
"desc":"熟悉python基礎語法,精通Linux",
"comments:90,
"add_time":"2019-9-18"
}
1.1 match查詢
GET lagou/job/_search
{
"query":{
"match":{
"title":"python"
}
}
}
#因為title字段做了分詞,python都能搜索出來
#搜索python網站也能搜索出來,把python和網站分成兩個詞
#搜索爬取也能搜索到,把爬和取分詞,去搜索
#只搜取 搜不到
1.2 term查詢
GET lagou/_search
{
"query":{
"term":{
"title":"python"
}
}
}
#會拿著要查詢的詞不做任何處理,直接查詢
#用python爬蟲,查不到,用match就能查到
{
"query":{
"term":{
"company_name":"美團"
}
}
}
#通過美團,就查詢不到
1.3 terms查詢
GET lagou/_search
{
"query":{
"terms":{
"title":["工程師","django","運維"]
}
}
}
#三個詞,只要有一個,就會查詢出來
1.4 控制查詢的返回數量(分頁)
GET lagou/_search
{
"query":{
"match":{
"title":"python"
}
},
"form":1,
"size":2
}
#從第一條開始,大小為2
1.5 match_all 查詢
GET lagou/_search
{
"query":{
"match_all":{}
}
}
#所有數據都返回
1.6 match_phrase查詢
GET lagou/_search
{
"query":{
"match_phrase":{
"title":{
"query":"python系統",
"slop":6
}
}
}
}
#短語查詢,
#會把查詢條件python和系統分詞,放到列表中,再去搜索的時候,必須滿足python和系統同時存在的才能搜出來
#"slop":6 :python和系統這兩個詞之間最小的距離
1.7 multi_match
GET lagou/_search
{
"query":{
"multy_match":{
"query":"python",
"fields":["title","desc"]
}
}
}
#可以指定多個字段
#比如查詢title和desc這個兩個字段中包含python關鍵詞的文檔
#"fields":["title^3","desc"]:權重,title中的python是desc中的三倍
1.8 指定返回的字段
GET lagou/_search
{
"query":{
"stored_fields":["title","company_name"]
"match":{
"title":"python"
}
}
}
#只返回title和company_name字段
#"stored_fields":["title","company_name",'dsc'],不會返回dsc,因為我們要求stroed_fields,之前desc字段設為false(默認),不會顯示
1.9 sort 結果排序
GET lagou/_search
{
"query":{
"match_all":{}
},
"sort":[
{
"comments":{
"order":"desc"
}
}
]
}
#查詢所有文檔,按comments按desc降序排序
1.10 range范圍查詢
GET lagou/_search
{
"query":{
"range":{
"comments":{
"gte":10,
"lte":20,
"boost":2.0
}
}
}
}
#指定comments字段大于等于10,小于等于20
#boost:權重
GET lagou/_search
{
"query":{
"range":{
"add_time":{
"gte":"2019-10-11",
"lte":"now",
}
}
}
}
#對時間進行查詢
1.11 wildcard查詢
GET lagou/_search
{
"query":{
"wildcard":{
"title":{
"value":"pyth*n",
"boost":2.0
}
}
}
}
#模糊查詢,title中,有pyth任意值n得都能查出來
1.12 exists存在
exists:字段包含,存在的
# 包含followers_count字段
GET user_toutiao/_search
{
"query": {
"bool": {
"must": [
{"exists": {
"field": "followers_count"
}}
]
}
}
}
# 不包含followers_count字段
GET user_toutiao/_count
{
"query": {
"bool": {
"must_not": [
{"exists": {
"field": "followers_count"
}}
]
}
}
}
# 不包含followers_count且updata_timestamp>1614221216
GET user_toutiao/_count
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "followers_count"
}
}
],
"must": [
{"range": {
"updata_timestamp": {
"gt": 1614221216
}
}}
]
}
}
}
二 組合查詢
2.1 bool查詢
#bool查詢包括must should must_not filter
'''
bool:{
"filter":[], 字段過濾
"must":[], 所有查詢條件都滿足
"should":[], 滿足一個或多個
"must_not":{} 都不滿足于must相反
}
'''
# 建立測試數據
POST lago/testjob/_bulk
{"index":{"_id":1}}
{"salary":10,"title":"Python"}
{"index":{"_id":2}}
{"salary":20,"title":"Scrapy"}
{"index":{"_id":3}}
{"salary":30,"title":"Django"}
{"index":{"_id":4}}
{"salary":30,"title":"Elasticsearch"}
2.2 簡單過濾查詢
#select * from testjob where salary=20
GET lagou/testjob/_search
{
"query":{
"bool":{
"must":{
"match_all":{}
},
"filter":{
"term":{
"salary":20
}
}
}
}
}
2.3 查詢多個值
#查詢薪資是10k或20k的
GET lagou/testjob/_search
{
"query":{
"bool":{
"must":{
"match_all":{}
},
"filter":{
"terms":{
"salary":[10,20]
}
}
}
}
}
#select * from testjob where title="python"
GET lagou/testjob/_search
{
"query":{
"bool":{
"must":{
"match_all":{}
},
"filter":{
"term":{
"title":"Python"
}
}
}
}
}
#title 是text字段,會做大小寫轉換,term不會預處理,拿著大寫Python去查查不到
#可以改成小寫,或者用match來查詢
'''
"filter":{
"match":{
"title":"Python"
}
}
'''
#查看分析器解析結果
GET _analyze
{
"analyzer":"ik_max_word",
"text":"python網絡開發工程師"
}
2.4 bool過濾查詢,可以做組合過濾查詢
#select * from testjob where (salary=20 or title=Python) and (salary!=30)
#查詢薪資等于20k或者工作為python的工作,排除價格為30k的
{
"query":{
"bool":{
"should":[
{"term":{"salary":20}},
{"term":{"title":"python"}}
],
"must_not":{
"term":{"salary":30}
}
}
}
}
#select * from testjob where title=python or (title=django and salary=30)
{
"query":{
"bool":{
"should":[
{"term":{"title":"python"}},
{
"bool":{
"must":[
{"term":{"title":"django"}},
{"term":{"salary":30}}
]
}
}
]
}
}
}
原文鏈接:https://www.cnblogs.com/guyouyin123/p/13308669.html
相關推薦
- 2023-07-25 mybatis-plus在實際開發中的應用
- 2022-10-11 tidb-gc長時間不變動引起的慢查詢
- 2022-04-16 PyInstaller如何打包依賴文件至目標程序目錄_python
- 2023-02-05 C++解決業務辦理時間問題示例解析_C 語言
- 2022-04-30 python?format格式化和數字格式化_python
- 2022-04-01 kubectl 命令執行報錯:(Unable to connect to the server: x
- 2022-09-15 Python淺析迭代器Iterator的使用_python
- 2022-06-06 canvas保存圖片時,谷歌瀏覽器Chrome報錯解決方案Not allowed to naviga
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支