示例#1
0
func (d *Datastore) Search(key ds.Key, query string) (*[]Model, error) {
	out, err := esCore.SearchRequest(true, d.esr.Index(key), key.Name(), query, "", 0)
	if err != nil {
		return nil, err
	}

	models := []Model{}
	for _, h := range out.Hits.Hits {

		// unmarshal response
		var data map[string]interface{}
		err := json.Unmarshal(h.Source, &data)
		if err != nil {
			return nil, fmt.Errorf("Unmarshal error: %v", err)
		}

		// get key
		k, ok := data["key"].(string)
		if !ok {
			return nil, fmt.Errorf("Unmarshal error. Key not a string: %v", data["key"])
		}

		// retrieve object
		model, err := d.Get(ds.NewKey(k))
		if err != nil {
			return nil, fmt.Errorf("%v (ElasticSearch inconsistent?)", err)
		}

		// ok! we have it.
		models = append(models, model)
	}
	return &models, nil
}
示例#2
0
func NewInstanceForKey(key ds.Key) datastore.Model {
	switch key.Type() {
	case "User":
		return &User{}
	case "Dataset":
		return &Dataset{}
	}
	return nil
}