Exemplo n.º 1
0
func (s *S) TestQuery(t *C) {
	esdriver := s.Driver()
	conn, err := esdriver.GetConnection()
	if err != nil {
		t.Fatalf("Error getting connection: %v", err)
	}

	k := datastore.NewKey("tweet", "1")
	tweet := map[string]string{
		"user":      "******",
		"state":     "NY",
		"post_date": "2009-11-15T14:12:12",
		"message":   "trying out Elasticsearch",
	}
	tweetJSON, err := json.Marshal(tweet)
	err = conn.Put(k, datastore.NewJSONMessage(tweetJSON, 0))
	if err != nil {
		t.Errorf("%v", err)
	}

	k = datastore.NewKey("tweet", "2")
	tweet = map[string]string{
		"user":      "******",
		"state":     "NY",
		"post_date": "2010-11-15T14:12:12",
		"message":   "trying out Elasticsearch again",
	}
	tweetJSON, err = json.Marshal(tweet)
	err = conn.Put(k, datastore.NewJSONMessage(tweetJSON, 0))
	if err != nil {
		t.Errorf("%v", err)
	}

	query := search.Query().Search("_exists_:state")
	testSearch := search.Search("twitter").Type("tweet").Size("10000").Query(query)

	msgs, err := conn.Query(testSearch)
	if err != nil {
		t.Errorf("Unepected error %v", err)
	}
	if len(msgs) != 2 {
		t.Errorf("Expected 2 msgs, got  %v", len(msgs))
	}

	//query for non-existant entity
	query = search.Query().Search("_exists_:blam")
	testSearch = search.Search("twitter").Type("tweet").Size("10000").Query(query)

	msgs, err = conn.Query(testSearch)
	if err != nil {
		t.Errorf("Unepected error %v", err)
	}
	if len(msgs) != 0 {
		t.Errorf("Expected 0 msgs, got  %V", len(msgs))
	}

}
Exemplo n.º 2
0
//func TestPutGetDelete(t *testing.T) {
func (s *S) TestPutGetDelete(t *C) {
	esdriver := s.Driver()
	//	driver, err := getConnection()
	//	if err != nil {
	//		t.Fatalf("Error initializing driver: %v", err)
	//	}

	conn, err := esdriver.GetConnection()
	if err != nil {
		t.Fatalf("Error getting connection: %v", err)
	}

	k := datastore.NewKey("tweet", "1")
	tweet := map[string]string{
		"user":      "******",
		"post_date": "2009-11-15T14:12:12",
		"message":   "trying out Elasticsearch",
	}
	tweetJSON, err := json.Marshal(tweet)
	err = conn.Put(k, datastore.NewJSONMessage(tweetJSON, 0))
	if err != nil {
		t.Errorf("%v", err)
	}

	//Get tweet
	raw, err := conn.Get(k)
	if err != nil {
		t.Fatalf("Unexpected: %v", err)
	}
	glog.Infof("raw is %v", string(raw.Bytes()))
	var tweetMap map[string]string
	json.Unmarshal(raw.Bytes(), &tweetMap)
	glog.Infof("tweet is %v", tweetMap)

	if tweetMap["user"] != "kimchy" {
		t.Errorf("Expected kimchy, found %s", tweetMap["user"])
	}

	//Delete tweet
	err = conn.Delete(k)
	if err != nil {
		t.Errorf("Unexpected delete error: %v", err)
	}

	//test not found
	raw, err = conn.Get(k)
	if raw != nil {
		t.Errorf("Expected nil return;")
	}
	if err == nil {
		t.Error("Expected error, not nil")
	} else if !datastore.IsErrNoSuchEntity(err) {
		glog.Infof("type is %s", reflect.ValueOf(err))
		t.Fatalf("Unexpected: %v", err)
	}

}
Exemplo n.º 3
0
// convert search result of json host to dao.Host array
func toJSONMessages(result *core.SearchResult) []datastore.JSONMessage {
	glog.V(4).Infof("Converting results %v", result)
	var total = len(result.Hits.Hits)
	var msgs = make([]datastore.JSONMessage, total)
	for i := 0; i < total; i++ {
		glog.V(4).Infof("Adding result %s", string(result.Hits.Hits[i].Source))
		src := result.Hits.Hits[i].Source
		version := result.Hits.Hits[i].Version
		msg := datastore.NewJSONMessage(src, version)
		msgs[i] = msg
	}
	return msgs
}
Exemplo n.º 4
0
func (ec *elasticConnection) Get(key datastore.Key) (datastore.JSONMessage, error) {
	//	func Get(pretty bool, index string, _type string, id string) (api.BaseResponse, error) {
	glog.V(4).Infof("Get for {kind:%v, id:%v}", key.Kind(), key.ID())
	//	err := core.GetSource(ec.index, key.Kind(), key.ID(), &bytes)
	response, err := elasticGet(false, ec.index, key.Kind(), key.ID())
	if err != nil {
		glog.Errorf("Error is %v", err)
		return nil, err
	}
	if !response.Exists {
		glog.V(4).Infof("Entity not found for {kind:%s, id:%s}", key.Kind(), key.ID())
		return nil, datastore.ErrNoSuchEntity{Key: key}
	}
	bytes := response.Source
	msg := datastore.NewJSONMessage(bytes, response.Version)
	return msg, nil
}