Exemplo n.º 1
0
func updateAliases(client *elasticsearch.Client, addActions, removeActions []Action) error {
	actions := []map[string]Action{}
	for _, action := range addActions {
		actions = append(actions, map[string]Action{
			"add": action,
		})
	}
	for _, action := range removeActions {
		actions = append(actions, map[string]Action{
			"remove": action,
		})
	}

	body, err := json.Marshal(map[string]interface{}{"actions": actions})
	if err != nil {
		return err
	}

	req := elasticsearch.NewRequest("POST", "_aliases", bytes.NewReader(body))
	res, err := client.Do(req)
	if err != nil {
		return err
	}

	if v, ok := res["error"].(string); ok && len(v) > 0 {
		return errors.New(v)
	}
	return nil
}
Exemplo n.º 2
0
func indexNamesAndAliasedIndexByAliasName(client *elasticsearch.Client, aliasName string) (names []string, aliased map[string]bool) {
	req := elasticsearch.NewRequest("GET", "_aliases", nil)
	res, err := client.Do(req)
	if err != nil {
		return
	}

	aliased = map[string]bool{}
	for idxName, idx := range res {
		if aliasNameFromIndexName(idxName) != aliasName {
			continue
		}
		names = append(names, idxName)
		if idx, ok := idx.(map[string]interface{}); ok {
			if aliases, ok := idx["aliases"].(map[string]interface{}); ok {
				for alias := range aliases {
					if alias == aliasName {
						aliased[idxName] = true
					}
				}
			}
		}
	}
	sort.Sort(sort.StringSlice(names))
	return
}
Exemplo n.º 3
0
func (cmd *Settings) Run(args []string) error {
	if len(args) < 1 {
		cmd.Usage(os.Stdout)
		return nil
	}
	indexName := args[0]
	cmdFlag.Parse(args[1:])

	client := elasticsearch.NewClient(
		elasticsearch.NewConfig().WithURL(*urlStr),
	)

	req := elasticsearch.NewRequest("GET", path.Join(indexName, "_settings"), nil)
	res, err := client.Do(req)
	if err != nil {
		return err
	}
	b, _ := json.MarshalIndent(res[indexName], "", "  ")
	fmt.Println(string(b))

	return nil
}
Exemplo n.º 4
0
func (cmd *Create) Run(args []string) error {
	if len(args) < 1 {
		cmd.Usage(os.Stdout)
		return nil
	}
	aliasName := args[0]
	indexName := indexNameFromAliasName(aliasName)
	cmdFlag.Parse(args[1:])

	client := elasticsearch.NewClient(
		elasticsearch.NewConfig().WithURL(*urlStr),
	)

	req := elasticsearch.NewRequest("PUT", indexName, strings.NewReader(*cmd.mappings))
	res, err := client.Do(req)
	if err != nil {
		return err
	}

	if v, ok := res["acknowledged"].(bool); ok && v {
		_, aliased := indexNamesAndAliasedIndexByAliasName(client, aliasName)
		if len(aliased) == 0 {
			if err := makeAlias(client, aliasName, indexName); err != nil {
				return err
			}
			fmt.Printf("%s <- %s\n", indexName, aliasName)
			return nil
		}
		fmt.Printf("%s\n", indexName)
	}

	if v, ok := res["error"].(string); ok && len(v) > 0 {
		return errors.New(v)
	}

	return nil
}