Esempio n. 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
}
Esempio n. 2
0
import (
	"fmt"
	"github.com/jbenet/data"
	"github.com/jbenet/datadex/datastore"
	ds "github.com/jbenet/datastore.go"
	"os"
	"strings"
)

var indexDB *IndexDB

type IndexDB struct {
	ds *datastore.Datastore
}

var kUser = ds.NewKey("/User")
var kDataset = ds.NewKey("/Dataset")
var kDatasetVersion = ds.NewKey("/DatasetVersion")

var ErrNotFound = ds.ErrNotFound

func init() {

	dbpath := data.ConfigGetString("db.path", ".indexdb")
	esurl := data.ConfigGetString("db.elasticsearch", "http://localhost:9200/datadex")

	pOut("Using db.path %s\n", dbpath)
	pOut("Using db.elasticsearch %s\n", esurl)

	// Setup Datastore
	d, err := datastore.NewDatastore(&datastore.Config{