You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package EsUtil
import (
"dsTools/Utils/CommonUtil"
"dsTools/Utils/ConfigUtil"
"fmt"
"github.com/valyala/fasthttp"
"io/ioutil"
"net/http"
"strings"
)
/**
功能:判断指定的索引是否存在
作者:黄海
时间2020-03-20
*/
func IndexIsExists(indexName string) bool {
indexArray := GetAllIndex()
var found = false
for i := 0; i < len(indexArray); i++ {
if indexName == indexArray[i] {
found = true
break
}
}
return found
}
/**
功能:获取有哪些索引
作者:黄海
时间2020-03-20
*/
func GetAllIndex() []string {
var result []string
url := ConfigUtil.EsHost + "/_cat/indices?v"
status, resp, err := fasthttp.Get(nil, url)
if err != nil {
fmt.Println("请求失败:", err.Error())
return nil
}
if status != fasthttp.StatusOK {
fmt.Println("请求没有成功:", status)
return nil
}
body := string(resp)
var lines = strings.Split(body, "\n")
for i := 1; i < len(lines); i++ {
line := lines[i]
line = CommonUtil.DeleteExtraSpace(line)
var carr = strings.Split(line, " ")
if len(carr) < 2 {
break
}
result = append(result, carr[2])
}
return result
}
/**
功能获取指定index的mapping
作者:黄海
时间2020-03-20
*/
func GetMapping(indexName string) (string, error) {
url := ConfigUtil.EsHost + "/" + indexName + "/_mapping"
status, resp, err := fasthttp.Get(nil, url)
if err != nil {
fmt.Println("请求失败:", err.Error())
return "", err
}
if status != fasthttp.StatusOK {
fmt.Println("请求没有成功:", status)
return "", err
}
body := string(resp)
return body, nil
}
/**
功能:删除指定的索引
作者:黄海
时间2020-03-20
*/
func DeleteIndex(indexName string) {
url := ConfigUtil.EsHost + "/" + indexName + "?pretty"
req, _ := http.NewRequest("DELETE", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
/**
功能:创建索引
作者:黄海
时间2020-03-21
*/
func CreateIndex(indexName string, body string) {
url := ConfigUtil.EsHost + "/real_" + indexName
req, _ := http.NewRequest("PUT", url, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
_body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(_body))
}
/**
功能追加mapping
作者:黄海
时间2020-03-23
*/
func AddMapping(indexName string, createMappingStr string) {
url := ConfigUtil.EsHost + "/" + indexName + "/_mapping"
req, _ := http.NewRequest("POST", url, strings.NewReader(createMappingStr))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
_body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(_body))
}
/**
功能设置指定index的最大响应数据上限
作者:黄海
时间2020-03-023
*/
func SetMaxResult(indexName string) {
url := ConfigUtil.EsHost + "/" + indexName + "/_settings"
body := `{"index":{"max_result_window":2147483647}}`
req, _ := http.NewRequest("PUT", url, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
_body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(_body))
}
/**
功能:增加别名
作者:黄海
时间2020-03-23
*/
func AddAliase(indexName string, alias string){
url := ConfigUtil.EsHost + "/_aliases"
body:=`
{
"actions" : [
{ "add" : { "index" : "`+indexName+`", "alias" : "`+alias+`" } }
]
}
`
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
_body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(_body))
}