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)) } }
func (s *S) TestQuery(t *C) { ctx := s.ctx ds := datastore.New() k := datastore.NewKey("tweet", "123") tweet := &tweettest{"kimchy", "NY", "2010-11-15T14:12:12", "trying out Elasticsearch", version} err := ds.Put(ctx, k, tweet) if err != nil { t.Errorf("%v", err) } k = datastore.NewKey("tweet", "234") tweet = &tweettest{"kimchy2", "NY", "2010-11-15T14:12:12", "trying out Elasticsearch again", version} err = ds.Put(ctx, k, tweet) if err != nil { t.Errorf("%v", err) } query := search.Query().Search("_exists_:State") testSearch := search.Search("twitter").Type("tweet").Size("10000").Query(query) q := datastore.NewQuery(ctx) msgs, err := q.Execute(testSearch) if err != nil { t.Errorf("Unepected error %v", err) } if msgs.Len() != 2 { t.Errorf("Expected 2 msgs, got %v", msgs.Len()) } //query for non-existant entity query = search.Query().Search("_exists_:blam") testSearch = search.Search("twitter").Type("tweet").Size("10000").Query(query) q = datastore.NewQuery(ctx) msgs, err = q.Execute(testSearch) if err != nil { t.Errorf("Unepected error %v", err) } if msgs.Len() != 0 { t.Errorf("Expected 0 msgs, got %v", msgs.Len()) } }
// GetN returns all hosts up to limit. func (hs *HostStore) GetN(ctx datastore.Context, limit uint64) ([]Host, error) { q := datastore.NewQuery(ctx) query := search.Query().Search("_exists_:ID") search := search.Search("controlplane").Type(kind).Size(strconv.FormatUint(limit, 10)).Query(query) results, err := q.Execute(search) if err != nil { return nil, err } return convert(results) }
func (s *Store) GetServiceAddressAssignments(ctx datastore.Context, serviceID string) ([]AddressAssignment, error) { q := datastore.NewQuery(ctx) query := search.Query().Term("ServiceID", serviceID) search := search.Search("controlplane").Type(kind).Size("50000").Query(query) results, err := q.Execute(search) if err != nil { return nil, err } return convert(results) }
func query(ctx datastore.Context, query string) ([]Service, error) { q := datastore.NewQuery(ctx) elasticQuery := search.Query().Search(query) search := search.Search("controlplane").Type(kind).Size("50000").Query(elasticQuery) results, err := q.Execute(search) if err != nil { return nil, err } return convert(results) }
//GetUpdatedServices returns all services updated since "since" time.Duration ago func (s *Store) GetUpdatedServices(ctx datastore.Context, since time.Duration) ([]Service, error) { q := datastore.NewQuery(ctx) t0 := time.Now().Add(-since).Format(time.RFC3339) elasticQuery := search.Query().Range(search.Range().Field("UpdatedAt").From(t0)).Search("_exists_:ID") search := search.Search("controlplane").Type(kind).Size("50000").Query(elasticQuery) results, err := q.Execute(search) if err != nil { return nil, err } return convert(results) }
// GetServiceTemplates returns all ServiceTemplates func (s *Store) GetServiceTemplates(ctx datastore.Context) ([]*ServiceTemplate, error) { glog.V(3).Infof("Store.GetServiceTemplates") q := datastore.NewQuery(ctx) query := search.Query().Search("_exists_:ID") search := search.Search("controlplane").Type(kind).Query(query) results, err := q.Execute(search) if err != nil { return nil, err } return convert(results) }
// FindHostsWithPoolID returns all hosts with the given poolid. func (hs *HostStore) FindHostsWithPoolID(ctx datastore.Context, poolID string) ([]Host, error) { id := strings.TrimSpace(poolID) if id == "" { return nil, errors.New("empty poolId not allowed") } q := datastore.NewQuery(ctx) query := search.Query().Term("PoolID", id) search := search.Search("controlplane").Type(kind).Size("50000").Query(query) results, err := q.Execute(search) if err != nil { return nil, err } return convert(results) }
func (s *Store) GetServiceAddressAssignmentsByPort(ctx datastore.Context, port uint16) ([]AddressAssignment, error) { if port == 0 { return nil, fmt.Errorf("port must be greater than 0") } query := search.Query().Term("Port", strconv.FormatUint(uint64(port), 10)) search := search.Search("controlplane").Type(kind).Size("50000").Query(query) if results, err := datastore.NewQuery(ctx).Execute(search); err != nil { return nil, err } else { return convert(results) } }
//GetChildServices returns services that are children of the given parent service id func (s *Store) GetChildServices(ctx datastore.Context, parentID string) ([]Service, error) { id := strings.TrimSpace(parentID) if id == "" { return nil, errors.New("empty parent service id not allowed") } q := datastore.NewQuery(ctx) query := search.Query().Term("ParentServiceID", id) search := search.Search("controlplane").Type(kind).Size("50000").Query(query) results, err := q.Execute(search) if err != nil { return nil, err } return convert(results) }
// GetHostByIP looks up a host by the given ip address func (hs *HostStore) GetHostByIP(ctx datastore.Context, hostIP string) (*Host, error) { if hostIP = strings.TrimSpace(hostIP); hostIP == "" { return nil, errors.New("empty hostIP not allowed") } query := search.Query().Term("IPs.IPAddress", hostIP) search := search.Search("controlplane").Type(kind).Query(query) results, err := datastore.NewQuery(ctx).Execute(search) if err != nil { return nil, err } if results.Len() == 0 { return nil, nil } else if hosts, err := convert(results); err != nil { return nil, err } else { return &hosts[0], nil } }