1. 程式人生 > 其它 >ES操作整理文件

ES操作整理文件

一、索引配置

1、查詢setting

GET /saas-mp-promotion-search_activity/_settings

2、建立setting

// 可以根據5拿到的配置建立索引
// number_of_shards-分片
// number_of_replicas-備份數
// analysis-分詞分析器

PUT es_create_test
{
    "settings":{
        "index":{
            "number_of_shards":"3",
            "number_of_replicas":"0",
            "analysis":{
                "analyzer":{
                    "my_analyzer":{
                        "tokenizer":"my_tokenizer"
                    }
                },
                "tokenizer":{
                    "my_tokenizer":{
                        "token_chars":[
                            "letter",
                            "digit",
                            "whitespace",
                            "punctuation",
                            "symbol"
                        ],
                        "type":"ngram"
                    }
                }
            }
        }
    }
}

3、查詢mapping

GET /saas-mp-promotion-search_activity/_mapping

4、建立mapping

type注意大小寫

PUT saas-mp-promotion-goods-sync-test/_doc/_mapping?include_type_name=true
{
    "properties":{
        "activityId":{
            "type":"long"
        },
        "activityIdUnique":{
            "type":"keyword"
        },
        "activityLevelType":{
            "type":"integer"
        },
        "activityName":{
            "type":"text",
            "fields":{
                "cn":{
                    "type":"text",
                    "analyzer":"ik_max_word"
                },
                "en":{
                    "type":"text",
                    "analyzer":"english"
                },
                "raw":{
                    "type":"keyword"
                }
            },
            "analyzer":"my_analyzer"
        },
        "startTime":{
            "type":"date",
            "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "timeEffect":{
            "type":"nested",
            "properties":{
                "repeatDay":{
                    "type":"keyword"
                },
                "repeatEndInterval":{
                    "type":"keyword"
                },
                "repeatGmt":{
                    "type":"integer"
                },
                "repeatStartInterval":{
                    "type":"keyword"
                },
                "repeatType":{
                    "type":"integer"
                },
                "searchRepeatEndInterval":{
                    "type":"keyword"
                },
                "searchRepeatStartInterval":{
                    "type":"keyword"
                }
            }
        },
        "update_time":{
            "type":"date",
            "format":"yyyy-MM-dd HH:mm:ss"
        }
    }
}

二、操作doc

1、insert

POST /saas-mp-promotion-search_activity/_doc
{
	"activityIdUnique": "999-1721299"
}

2、delete

2.1、根據query刪除

DELETE /saas-mp-promotion-search_activity/_doc/id

2.2、根據id刪除

POST /saas-mp-promotion-search_activity/_delete_by_query
{
    "query":{
        "term":{
            "activityIdUnique":{
                "value":"3-1721299"
            }
        }
    }
}

3、update

update 請求最簡單的一種形式是接收文件的一部分作為 doc 的引數, 它只是與現有的文件進行合併。物件被合併到一起,覆蓋現有的欄位,增加新的欄位

3.1、根據id修改

POST /saas-mp-promotion-search_activity_scope/_update/101_3-21000950540215
{
    "doc":{
        "storeId":[
            1
        ]
    }
}

3.2、根據query修改

POST /saas-mp-promotion-search_activity_scope/_update_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "activityId": {
              "value": 2161
            }
          }
        }
      ]
    }
  },
  "script": {
    "source": "ctx._source.payType = 1;ctx._source.deliveryType = [1,2]"
  }
}

3.3、給已有欄位為空的設定預設值

POST index_test/_doc/_update_by_query
{
    "script":{
        "lang":"painless",
        "source":"if (ctx._source.skuNumber == null) {ctx._source.skuNumber = 'sss'}"
    }
}

3.4、移除已有欄位

POST saas-mp-promotion-search_goods/_update_by_query
{
  "script": {
    "lang": "painless",
    "inline": """ctx._source.remove("titleList.vid")"""
  },
  "query": {
    "ids": {
      "values" : ["103_120000_100313970052000"]
    }
  }
}

3.5、給欄位加一個物件

POST test/_update_by_query
{
  "query": {
    "ids": {
      "values": [
        "103_120000_100313947052000"
      ]
    }
  },
  "script": {
    "lang": "painless",
    "source": "if(ctx._source.goodsTitleList==null){List ls = new ArrayList();ls.add(params.member);ctx._source.goodsTitleList=ls;}else{ctx._source.goodsTitleList.add(params.member)}",
    "params": {
      "member": {
        "belongVid": 0,
        "title": ""
      }
    }
  }
}

3.6、給已有list物件修改值(for迴圈使用)

POST test/_update_by_query
{
  "query": {
    "ids": {
      "values": ["103_120000_100313970052000"]
    }
  },
  "script": {
    "lang": "painless",
    "source": "for(e in ctx._source.goodsTitleList){e.belongVid = ctx._source.goodsBelongVid;e.title = ctx._source.title;}"
  }
}

3.7、刪除已有list的其中一個物件

POST test/_update_by_query
{
  "query": {
    "ids": {
      "values": ["103_120000_100313970052000"]
    }
  },
  "script": {
    "lang": "painless",
    "source": "ctx._source.titleList.removeIf(list_item -> list_item.belongVid==120000)"
  }
}

4、query

4.1、根據id查詢

GET test/_doc/id

4.2、根據ids查詢

GET test/_search
{
    "query":{
        "ids":{
            "values":[1]
        }
    }
}

4.3、簡單查詢

GET /saas-mp-promotion-search_activity/_search
{
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "bosId":{
                            "value":4000180336837
                        }
                    }
                },
                {
                    "range":{
                        "endTime":{
                            "gte":1642504062959
                        }
                    }
                },
                {
                    "terms":{
                        "activityStatus":[
                            "0",
                            "1"
                        ]
                    }
                }
            ],
            "must_not":[
                {
                    "exists":{
                        "field":"activityIdUnique"
                    }
                }
            ]
        }
    }
}

4.4、nested查詢

GET /saas-mp-promotion-search_goods/_search
{
  "track_total_hits": true,
  "query": {
    "nested": {
      "path": "goodsStock",
      "query": {
        "exists": {
          "field": "goodsStock.sellQuantity"
        }
      }
    }
  }
}

4.5、nested判空查詢

GET saas-mp-promotion-search_goods/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "goodsVidInfoList",
            "query": {
              "exists": {
                "field": "goodsVidInfoList"
              }
            }
          }
        }
      ]
    }
  }
}

4.6、判斷兩個欄位是否相等

POST /saas-mp-promotion-search_activity/_search
{
  "track_total_hits": true,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "bizSource": {
              "value": 101
            }
          }
        },
        {
          "exists": {
            "field": "outActivityId"
          }
        },
        {
          "script": {
            "script": {
               "inline": "doc['activityId'] != doc['outActivityId']",
               "lang": "painless"
            }
          }
        }
      ]
    }
  }
}

5、重建索引

POST _reindex
{
    "source":{
        "index":"test1"
    },
    "dest":{
        "index":"test_20220117"
    },
    "script":{
        "inline":"ctx._routing= ctx._source.bosId",
        "lang":"painless"
    }
}

6、加別名

POST _aliases
{
    "actions":[
        {
            "add":{
                "index":"test",
                "alias":"test_alias"
            }
        }
    ]
}

7、檢視後臺任務

GET _tasks?detailed=true&actions=indices:data/write/update/byquery

8、修改原有欄位型別

es不支援直接修改欄位型別,需要通過重建索引來實現
步驟:
1、建立一個和原先索引結構一致(除了需要修改的欄位)的新索引(setting/mapping)
2、通過reindex將老索引資料匯入到新索引裡
3、將新索引設定到現有別名
4、刪除老索引