一、查詢分類
1、基本查詢:使用es內置查詢條件進行查詢
2、組合查詢:把多個查詢組合在一起進行復合查詢
3、過濾查詢:查詢的同時,通過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、match查詢
GET lagou/job/_search
{
"query":{
"match":{
"title":"python"
}
}
}
#因為title字段做了分詞,python都能搜索出來
#搜索python網站也能搜索出來,把python和網站分成兩個詞
#搜索爬取也能搜索到,把爬和取分詞,去搜索
#只搜取 搜不到
2、term查詢
GET lagou/_search
{
"query":{
"term":{
"title":"python"
}
}
}
#會拿着要查詢的詞不做任何處理,直接查詢
#用python爬蟲,查不到,用match就能查到
GET lagou/_search
{
"query":{
"term":{
"company_name":"美團"
}
}
}
#通過美團,就查詢不到
3、terms查詢
GET lagou/_search
{
"query":{
"terms":{
"title":["工程師","django","運維"]
}
}
}
#三個詞,只要有一個,就會查詢出來
4、控制查詢的返回數量(分頁)
GET lagou/_search
{
"query":{
"match":{
"title":"python"
}
},
"form":1,
"size":2
}
#從第一條開始,大小為2
5、match_all查詢
GET lagou/_search
{
"query":{
"match_all":{}
}
}
#所有數據都返回
6、match_phrase查詢
GET lagou/_search
{
"query":{
"match_phrase":{
"title":{
"query":"python系統",
"slop":6
}
}
}
}
#短語查詢,
#會把查詢條件python和系統分詞,放到列表中,再去搜索的時候,必須滿足python和系統同時存在的才能搜出來
#"slop":6 python和系統這兩個詞之間最小的距離
7、multi_match查詢
GET lagou/_search
{
"query":{
"multy_match":{
"query":"python",
"fields":["title","desc"]
}
}
}
#可以指定多個字段
#比如查詢title和desc這個兩個字段中包含python關鍵詞的文檔
#"fields":["title^3","desc"]:權重,title中的python是desc中的三倍
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(默認),不會顯示
9、sort 結果排序
GET lagou/_search
{
"query":{
"match_all":{}
},
"sort":[
{
"comments":{
"order":"desc"
}
}
]
}
#查詢所有文檔,按comments按desc降序排序
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",
}
}
}
}
#對時間進行查詢
11、wildcard查詢
GET lagou/_search
{
"query":{
"wildcard":{
"title":{
"value":"pyth*n",
"boost":2.0
}
}
}
}
#模糊查詢,title中,有pyth任意值n得都能查出來
三、組合查詢
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、簡單過濾查詢
#select * from testjob where salary=20
GET lagou/testjob/_search
{
"query":{
"bool":{
"must":{
"match_all":{}
},
"filter":{
"term":{
"salary":20
}
}
}
}
}
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網絡開發工程師"
}
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}}
]
}
}
]
}
}
}