0
votes

I wish the Google API documentation was a little more newbie-proof.

I've worked my way through Selecting all entities, Updating an entity, inserting, and deleting. Now I would like to start selecting specific entities by criteria. The API https://datastore.googleapis.com/v1/projects/project-id-5200707080506492774:runQuery is for this purpose, and if I provide the payload of "query: {}" I get all entities. I can also filter by Kind. But I cannot figure out how to filter by a property. I try to get an entity by name with this JSON stringified payload:

var payload = 

{
  "query": {
    "kind": [
      {
        "name": "Test"
      }
    ],
    "filter": {
      "propertyFilter": {
        "property": {
          "name": "id"
        },
        "op": "EQUAL",
        "value": {
          "stringValue": "5634472569470976"
        }
      }
    }
  }
}

But I get the 200 results batch:

{
  "batch": {
    "entityResultType": "FULL",
    "endCursor": "CgA=",
    "moreResults": "NO_MORE_RESULTS"
  }
}

AKA: Nothing was found.

Could someone enlighten me with regards to how to select by the entity's name/id or other field of data?


EDIT:

Here is the file structure of my entities. They are organized under the kind Test:

    {
      "batch": {
        "entityResultType": "FULL",
        "entityResults": [
          {
            "entity": {
              "key": {
                 "partitionId": {
                   "projectId": "project-id-5200707080506492774"
                 },
                 "path": [
                  {
                    "kind": "Test",
                    "id": "5634472569470976"
                  }
                ]
              },
              "properties": {
                "test": {
                  "stringValue": "Hi it is me"
                }
              }
            },
            "cursor": "CjsSNWogc35wcm9qZWN0LWlkLTUyMDA3MDcwODA1
                   MDY0OTI3NzRyEQsSBFRlc3QYgICAgN6QgQoMGAAgAA==",
            "version": "1503343869436000"
      },
      {
        "entity": {
           "key": {
            "partitionId": {
          "projectId": "project-id-5200707080506492774"
        },
        "path": [
          {
            "kind": "Test",
            "id": "5639445604728832"
          }
        ]
      },
      "properties": {
        "test": {
          "stringValue": "testtesttest"
        }
      }
    },
    "cursor": "CjsSNWogc35wcm9qZWN0LWlkLTUyMDA3MDcwODA1MDY0OTI3NzRyEQsSBFRlc3QYgICAgLyhggoMGAAgAA==",
    "version": "1503343008992000"
  },
  {
    "entity": {
      "key": {
        "partitionId": {
          "projectId": "project-id-5200707080506492774"
        },
        "path": [
          {
            "kind": "Test",
            "id": "5649391675244544"
          }
        ]
      },
      "properties": {
        "test": {
          "stringValue": "testtest"
        }
      }
    },
    "cursor": "CjsSNWogc35wcm9qZWN0LWlkLTUyMDA3MDcwODA1MDY0OTI3NzRyEQsSBFRlc3QYgICAgPjChAoMGAAgAA==",
    "version": "1503342946693000"
  },
  {
    "entity": {
      "key": {
        "partitionId": {
          "projectId": "project-id-5200707080506492774"
        },
        "path": [
          {
            "kind": "Test",
            "id": "5659313586569216"
          }
        ]
      },
      "properties": {
        "test": {
          "stringValue": "testtesttest"
        }
      }
    },
    "cursor": "CjsSNWogc35wcm9qZWN0LWlkLTUyMDA3MDcwODA1MDY0OTI3NzRyEQsSBFRlc3QYgICAgNrjhgoMGAAgAA==",
    "version": "1503343059530000"
  },
  {
    "entity": {
      "key": {
        "partitionId": {
          "projectId": "project-id-5200707080506492774"
        },
        "path": [
          {
            "kind": "Test",
            "id": "5715999101812736"
          }
        ]
      },
      "properties": {
        "test": {
          "stringValue": "hello world"
        }
      }
    },
    "cursor": "CjsSNWogc35wcm9qZWN0LWlkLTUyMDA3MDcwODA1MDY0OTI3NzRyEQsSBFRlc3QYgICAgLzVkwoMGAAgAA==",
    "version": "1503343819165000"
  }
],
"endCursor": "CjsSNWogc35wcm9qZWN0LWlkLTUyMDA3MDcwODA1MDY0OTI3NzRyEQsSBFRlc3QYgICAgLzVkwoMGAAgAA==",
"moreResults": "NO_MORE_RESULTS"
  }
}

Edit:

I completed a filter query to check against my test field, as requested, and got the below 200 response:

{
  "batch": {
    "entityResultType": "FULL",
     "entityResults": [
       {
         "entity": {
           "key": {
            "partitionId": {
               "projectId": "project-id-5200707080506492774"
            },
            "path": [
              {
                "kind": "Test",
                "id": "5715999101812736"
              }
            ]
          },
           "properties": {
              "test": {
               "stringValue": "hello world"
            }
           }
          },
         "cursor": 
              "CjsSNWogc35wcm9qZWN0LWlkLTUyMDA3MDcwODA
               1MDY0OTI3NzRyEQsSBFRlc3QYgICAgLzVkwoMGAAgAA==",
          "version": "1503343819165000"
       }
     ],
      "endCursor": "CjsSNWogc35wcm9qZWN0LWlkLTUyMDA3MDcwODA1MDY0OTI3NzRyEQsSBFRlc3QYgICAgLzVkwoMGAAgAA==",
"moreResults": "NO_MORE_RESULTS"
  }
}
1
Do you have a Test entity with id 5634472569470976 and no ancestor? - Dan Cornilescu
The request looks correct. Is id a property of the entity or are you trying to search by entity key? If id is in fact a property, what is the data type? Just looking at the data (5634472569470976), it appears that it may be an Integer, but the filter is looking for a string type. Please double check/confirm. - Sai Pullabhotla
I added my data structure from a kind query to my original post. Say I want to pull entity 5634472569470976 by its ID. How do I filter by it's key when the API only lets me filter by properties? Do I need to add a separate ID field just so I can pull by an ID? - Nathaniel MacIver

1 Answers

1
votes

At least in the ndb python datastore library id is not allowed as a property name. Which might be the reason your query is not working (as you expect) either.

After all you don't see any property named id within the properties structures of your entities, it is actually part of the key -> path structure.

Just to confirm, try using a valid property (i.e. one listed inside properties), for example:

"filter": {
    "propertyFilter": {
        "property": {
            "name": "test"
        },
        "op": "EQUAL",
        "value": {
            "stringValue": "hello world"
        }
    }
}

If you have the entity key ids you don't need to perform queries to get the entities (you might not even be allowed to do that inside transactions), you can directly pull the entities by keys, using the projects.lookup method. I think something along these lines:

{
    "keys": [
        {
            "path": [
                {
                    "kind": "Test",
                    "id": "5634472569470976"
                }
            ]
        }
    ],
}