elasticsearch基礎(gulimall高階學習的)
阿新 • • 發佈:2020-12-21
技術標籤:gulimall專案
GET /bank/_search?q=*&sort=account_number:asc
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": "desc"
}
],
"from": 3,
"size": 3,
"_source": [ "firstname","balance"]
}
##全文檢索,按照評分進行排序,會對檢索條件進行分詞匹配
GET /bank/_search
{
"query": {
"match": {
"balance": 16418
}
}
}
GET /bank/_search
{
"query": {
"match": {
"address": "mill lane"
}
}
}
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "mill lane"
}
}
}
##多欄位匹配會進行分詞
GET /bank/_search
{
"query": {
"multi_match": {
"query": "mill Movico",
"fields" : ["address","city"]
}
}
}
##bool查詢 組合查詢,must:必須滿足;must:必須不滿足;should:滿不滿足都可以,滿足了分數就會更高
GET bank/_search
{
"query": {
"bool": {
"must": [
{"match": {
"gender": "M"
}},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{"match": {
"age": "18"
}}
],
"should": [
{"match": {
"lastname": "Wallace"
}}
]
}
}
}
## filter 不會貢獻相關性得分
GET bank/_search
{
"query": {
"bool": {
"must": [
{ "range": {
"age": {
"gte": 18,
"lte": 30
}
}}
],
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 25
}
}
}
}
}
}
GET bank/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
}
}
}
## 精確值用term,全文檢索就有match
GET bank/_search
{
"query": {
"term": {
"age": 28
}
}
}
##match_phrase 不會對查詢條件進行分詞, 查詢時只要匹配一部分即可
GET bank/_search
{
"query": {
"match_phrase": {
"address": "789 Madison"
}
}
}
##精確匹配,必須和查詢條件值一樣才可以 即不會對查詢條件進行分詞, 查詢時必須完全匹配
GET bank/_search
{
"query": {
"match": {
"address.keyword": "789 Madison"
}
}
}
##搜尋address中包含mill的所有人的年齡分佈以及平均年齡,但不顯示這些人的詳情
GET bank/_search
{
"query": {
"match": {
"address": "mill"
}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 10
}
},
"ageAvg": {
"avg": {
"field": "age"
}
},
"balanceAvg": {
"avg": {
"field": "balance"
}
}
},
"size": 0
}
##按照年齡聚合,並且求這些年齡段的這些人的平均薪資(子聚合)
GET bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"ageAvg": {
"avg": {
"field": "balance"
}
}
}
}
}
}
##查出所有年齡分佈,並且這些年齡段中M的平均薪資和F的平均薪資以及這個年齡段的總體平均薪資
GET bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"genderAgg": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
},
"ageBalance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
##檢視對映
GET bank/_mapping
##建立索引是指定型別
PUT /my_index
{
"mappings": {
"properties": {
"age": {"type": "integer"},
"email": {"type": "keyword"},
"name": {"type": "text"}
}
}
}
##修改對映(對映作名詞就是對應資料庫的列的屬性)"index" 預設是true,表示可以被檢索,false表示不能被檢索,作為冗餘欄位 ##(這裡的不能被檢索是否是指不能被當作檢索條件,自己驗證) 這裡相當於添加了一個列的(對映)
PUT /my_index/_mapping
{
"properties": {
"emloyee-id": {
"type": "keyword",
"index": false
}
}
}
GET /bank/_search
GET /newbank/_mapping
##如何修改已存在的對映 只能是資料遷移,建立一個新的索引將資料遷移進去
PUT /newbank
{
"mappings": {
"properties": {
"account_number": {
"type": "long"
},
"address": {
"type": "text"
},
"age": {
"type": "integer"
},
"balance": {
"type": "long"
},
"city": {
"type": "keyword"
},
"email": {
"type": "keyword"
},
"employer": {
"type": "keyword"
},
"firstname": {
"type": "text"
},
"gender": {
"type": "keyword"
},
"lastname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"state": {
"type": "keyword"
}
}
}
}
##執行資料遷移 ES7以後的版本不在建議使用型別了,自己會有一個預設型別,—doc
POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest": {
"index": "newbank"
}
}
GET /newbank/_search
##分詞
POST _analyze
{
"analyzer": "standard",
"text": ["I love you yty."]
}
##可以識別正文的ik分詞器,自定義詞庫用Nginx配置,放在Nginx預設的檔案html
POST _analyze
{
"analyzer": "ik_max_word",
"text": ["喬碧蘿殿下,我愛你"]
}