1. 程式人生 > 其它 >DAY 117 ES查詢

DAY 117 ES查詢

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得都能查出來

二 組合查詢

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
}
}
}
}
}

1.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網路開發工程師"
}

1.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}}
]
}
}
]
}
}
}