網(wǎng)站首頁 編程語言 正文
Elasticsearch查詢
查詢分類:
基本查詢:使用es內(nèi)置查詢條件進行查詢
組合查詢:把多個查詢組合在一起進行復合查詢
過濾:查詢的同時,通過filter條件在不影響打分的情況下篩選數(shù)據(jù)
一 基本查詢
#添加映射
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"
}
}
}
}
#測試數(shù)據(jù)
POST lago/job
{
"title":"python django 開發(fā)工程師",
"company_name":"美團科技有限公司",
"desc":"對django熟悉,掌握mysql和非關系型數(shù)據(jù)庫,網(wǎng)站開發(fā)",
"comments:200,
"add_time":"2018-4-1"
}
POST lago/job
{
"title":"python數(shù)據(jù)分析",
"company_name":"百度科技有限公司",
"desc":"熟悉python基礎語法,熟悉數(shù)據(jù)分析",
"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網(wǎng)站也能搜索出來,把python和網(wǎng)站分成兩個詞
#搜索爬取也能搜索到,把爬和取分詞,去搜索
#只搜取 搜不到
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 控制查詢的返回數(shù)量(分頁)
GET lagou/_search
{
"query":{
"match":{
"title":"python"
}
},
"form":1,
"size":2
}
#從第一條開始,大小為2
1.5 match_all 查詢
GET lagou/_search
{
"query":{
"match_all":{}
}
}
#所有數(shù)據(jù)都返回
1.6 match_phrase查詢
GET lagou/_search
{
"query":{
"match_phrase":{
"title":{
"query":"python系統(tǒng)",
"slop":6
}
}
}
}
#短語查詢,
#會把查詢條件python和系統(tǒng)分詞,放到列表中,再去搜索的時候,必須滿足python和系統(tǒng)同時存在的才能搜出來
#"slop":6 :python和系統(tǒng)這兩個詞之間最小的距離
1.7 multi_match
GET lagou/_search
{
"query":{
"multy_match":{
"query":"python",
"fields":["title","desc"]
}
}
}
#可以指定多個字段
#比如查詢title和desc這個兩個字段中包含python關鍵詞的文檔
#"fields":["title^3","desc"]:權(quán)重,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 結(jié)果排序
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:權(quán)重
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相反
}
'''
# 建立測試數(shù)據(jù)
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字段,會做大小寫轉(zhuǎn)換,term不會預處理,拿著大寫Python去查查不到
#可以改成小寫,或者用match來查詢
'''
"filter":{
"match":{
"title":"Python"
}
}
'''
#查看分析器解析結(jié)果
GET _analyze
{
"analyzer":"ik_max_word",
"text":"python網(wǎng)絡開發(fā)工程師"
}
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-03-18 go?sync.Map基本原理深入解析_Golang
- 2022-07-16 四種解決Nginx出現(xiàn)403 forbidden 報錯的方法
- 2022-04-17 css absolute絕對定位 讓 top 和bottom 同時生效
- 2022-05-08 SQL利用游標遍歷日期查詢的過程詳解_MsSql
- 2022-08-18 Go?Web編程添加服務器錯誤和訪問日志_Golang
- 2022-09-25 CSS-元素隱藏的兩種主要方式
- 2021-12-24 SQL注入詳解及防范方法_數(shù)據(jù)庫其它
- 2022-08-29 Python?GUI?圖形用戶界面_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支