Example #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
}
Example #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
}