ES使用命令說明
索引映射相關
# 查詢索引
GET goods_index
# 添加索引
PUT person
# 添加映射
PUT person/_mapping
{
"properties":
{
"name":
{
"type":"keyword"
},
"age":
{
"type":"integer"
}
}
}
# 查詢映射
GET person/_mapping
# 創建并添加映射
PUT person
{
"properties":
{
"name":
{
"type":"keyword"
},
"age":
{
"type":"integer"
}
}
}
# 映射添加欄位
PUT person/_mapping
{
"properties":
{
"address":{
"type":"text"
}
}
}
檔案相關
# 添加檔案,指定id
PUT person/_doc/1
{
"name":"abzz",
"age":10,
"abbress":"成都市錦江區"
}
# 添加檔案,不指定id
POST person/_doc
{
"name":"abzz",
"age":10,
"abbress":"成都市錦江區"
}
# 修改檔案
PUT person/_doc/1
{
"name":"abzz_1",
"age":10,
"abbress":"成都市錦江區"
}
# 根據id查詢檔案
GET person/_doc/1
# 查詢所有檔案
GET person/_doc/_search
# 根據id洗掉檔案
DELETE person/_doc/1HTcj34BJqQfCGFUhRkz
分詞器相關
# 分詞器使用
GET _analyze
{
"analyzer": "standard",
"text": [
"i have a dream"
]
}
term查詢和match查詢
# 分詞器使用
GET _analyze
{
"analyzer": "ik_max_word",
"text": [
"青島市市南區"
]
}
# 洗掉索引
DELETE person
# 新建一個帶有指定分詞器的索引
PUT person
PUT person/_mapping
{
"properties":{
"name":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"address":{
"type":"text",
"analyzer":"ik_max_word"
}
}
}
# 添加檔案
PUT person/_doc/1
{
"name":"abzz_1",
"age":10,
"address":"成都市錦江區"
}
PUT person/_doc/2
{
"name":"abzz_2",
"age":11,
"address":"成都市高新區"
}
PUT person/_doc/3
{
"name":"abzz_3",
"age":11,
"address":"青島市市南區"
}
GET person/_search
# term查詢,需要詞條完全匹配,不對查詢條件進行分詞
GET person/_search
{
"query": {
"term": {
"address": {
"value": "都市"
}
}
}
}
# match查詢,首先會對查詢條件進行分詞,然后對各個分詞的搜索條件進行求并集
GET person/_search
{
"query": {
"match": {
"address": "青島是一個好地方"
}
}
}
批量操作
# 1.洗掉id為1的記錄
# 2.創建id為6的張國榮記錄
# 3.修改id為3的記錄為黎明
POST _bulk
{"delete":{"_index":"person", "_id":"1"}}
{"create":{"_index":"person", "_id":"6"}}
{"name":"張國榮","age":55,"address":"香港"}
{"update":{"_index":"person", "_id":"3"}}
{"doc":{"name":"黎明"}}
matchAll示例
# matchAll演示,默認查詢前十條,from和size控制分頁
GET goods/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
模糊查詢
wildcard查詢
# wildcard查詢 ?代表一個字符 *
GET goods/_search
{
"query": {
"wildcard": {
"title": {
"value": "魅族"
}
}
}
}
正則查詢
# 正則運算式查詢
GET goods/_search
{
"query": {
"regexp": {
"title": "魅族111*"
}
}
}
前綴查詢
# 前綴查詢
GET goods/_search
{
"query": {
"prefix": {
"title": {
"value": "魅"
}
}
}
}
范圍查詢
# range查詢
GET goods/_search
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 20000
}
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
query_string和simple_query_string
# queryString和simpleQueryString
# queryString中的query條件支持and或者or關鍵字,并且關鍵字需要大寫,而simpleQueryString不支持
GET goods/_search
{
"query": {
"query_string": {
"fields": ["title", "categoryName", "brandName"],
"query": "小米 AND 手機"
}
}
}
bool查詢
說明:
- must 回傳的檔案必須滿足must子句的條件,并且參與計算分值
- filter 回傳的檔案必須滿足filter子句的條件,不計算分值
- should 回傳的檔案可能滿足should子句的條件,在一個Bool查詢中,如果沒有must或者filter,有一個或者多個should子句,那么只要滿足一個就可以回傳,
minimum_should_match引數定義了至少滿足幾個子句 - must_not 回傳的檔案必須不滿足must_not定義的條件
# bool查詢
GET goods/_search
{
"query": {
"bool": {
"must": [
{"term": {
"brandName": {
"value": "小米"
}
}},
{"match": {
"title": "手機筆記本"
}}
]
}
}
}
聚合查詢
- 指標聚合(聚合函式)
- 桶聚合(分組)
# 聚合
# 指標聚合-聚合函式
GET goods/_search
{
"query": {
"match": {
"title": "手機"
}
},
"aggs": {
"min_price": {
"min": {
"field": "price"
}
}
}
}
# 桶聚合-分組聚合
GET goods/_search
{
"query": {
"term": {
"brandName": {
"value": "魅族"
}
}
},
"aggs": {
"brand_name": {
"terms": {
"field": "brandName",
"size": 10
}
}
}
}
高亮查詢
# 高亮查詢
GET goods/_search
{
"query": {
"match": {
"title": "手機"
}
},
"highlight": {
"pre_tags": "<abzz>",
"post_tags": "</abzz>",
"fields": {
"title": {}
}
}
}
重建索引
# 重建索引
# 初始索引
# 1.添加索引
PUT test_index_v1
# 2.設定映射
PUT test_index_v1/_mapping
{
"properties":{
"name":{
"type":"keyword"
},
"age":{
"type":"integer"
}
}
}
GET test_index_v1/_mapping
# 3.添加資料
PUT test_index_v1/_doc/1
{
"name":"abzz",
"age":13
}
GET test_index_v1/_search
# 業務需求變了,需要修改映射結構,將name的型別修改為text
PUT test_index_v2
PUT test_index_v2/_mapping
{
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"integer"
}
}
}
# 復制資料
POST _reindex
{
"source": {"index": "test_index_v1"},
"dest": {"index": "test_index_v2"}
}
GET test_index_v2/_search
說明:由于索引映射結構變化需要走上述流程,但是代碼里面的索引還是用的原始的,有以下解決方案
- 修改代碼中的索引庫名稱(不推薦,懂得都懂)
- 將新建的索引賦值別名(推薦)
# 先洗掉之前的索引
DELETE test_index_v1
# 給新的索引賦值別名
POST test_index_v2/_alias/test_index_v1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/423972.html
標籤:其他
上一篇:doris生產使用記錄
