amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

@searchable returning an error but unable to work out how to fix

Open PeteDuncanson opened this issue 4 years ago • 11 comments

Before opening, please confirm:

  • [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • [X] I have searched for duplicate or closed issues.
  • [X] I have read the guide for submitting bug reports.
  • [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

How did you install the Amplify CLI?

No response

If applicable, what version of Node.js are you using?

No response

Amplify CLI Version

7.6.5

What operating system are you using?

windows

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

Nope.

Amplify Categories

api

Amplify Commands

Not applicable

Describe the bug

I believe my search index has got some duff data in it but I'm unsure how to correct it or fix it from the error message give.

I either need a clear way to brute force a reindex of all the data or better error message on how to fix the data issue. As is I'm stuck.

Error:

{"data":null,"errors":[{"path":null,"locations":[{"line":387,"column":11,"sourceName":null}],"message":"Validation error of type FieldUndefined: Field 'user' in type 'StaffMember' is undefined @ 'searchAddresss/items/area/accountManager/user'"},{"path":null,"locations":[{"line":416,"column":11,"sourceName":null}],"message":"Validation error of type FieldUndefined: Field 'user' in type 'StaffMember' is undefined @ 'searchAddresss/items/area/technician/user'"}]}

Now I can guess what its complaining about, some Model doesn't have a connection set that is a requirement, probably due to us changing our schema around during development. Trouble is how do I find that missing conection? We've tried using GraphQL an exisits:false but nothing comes back. We can't find the missing bit of data.

We've tried to trigger a reindex (why is that not a button somewhere btw?) but that seems to result in the same error which makes me think that its still within the source data in DynamoDB?

Expected behavior

Hmmm this is tricky to answer, what is the expected behaviour?

I guess for me it shouldn't be possible to get in this state in the first place. And now I'm in it there should be a clear path to get out of it.

For the ES index, how about not indexing the data if it doesnt' match some validation?

How about we make to easier to reindex.

How about we make it easier to find orphaned connections (if indeed that is what it is).

A lot of fixes for stuff like this seem to always be buried in issues like this rather than out there front of house helping us do our thing quicker without having to be tapping you lovely lovely folks on the shoulder every few hours. I feel like a kid at school constantly queuing up at teachers desk to check a spelling rather than actually doing any writing.

Reproduction steps

I think this might have happened but isn't in itself a full on recreation.

Created a model, Article. Added some data for that model. Created a new model, Authors. Added some data to Authors. Later wanted to link Authors to their Articles. Create a @connection on Article and Author id is required. New content will now have to have an Author assigned. Existing content might not have an Author assigned (ideally this would need back filling but for some reason hasn't been) Not all of the Articles are updated to include an Author, ES indexes it anyway and GraphQL then blows up when you query it.

Alternatively an Author that was assigned to an Article has then been removed and although referenced still in Article can't be found so returns null?

GraphQL schema(s)

# Put schemas below this line
Recently emails this over to @josefaidt for https://github.com/aws-amplify/amplify-cli/issues/9398#issuecomment-1011493131

Log output

# Put your logs below this line


Additional information

No response

PeteDuncanson avatar Jan 13 '22 10:01 PeteDuncanson

Forgot to add some of the bits we've tried.

We've gone through and added a "unknown" user to all Staff that didn't have one set (test data) and then ensured each Area has StaffMembers set. That from the error is what the problem seems to be. Yet still no fix.

We've tried removing the @searchable from the model in question and pushing, then re-adding it and pushing again. This was in a hope of it rebuilding the index completely incase there was any left over duff setup in ES or similar, scorched earth! Same error.

PeteDuncanson avatar Jan 13 '22 10:01 PeteDuncanson

ok we are now trying to remove the manditory flag on the connectoins to see if that lets it slip throuhg the validation. Believe we had that on there to keep DataStore happy but now we've stripped that out we can have non-manditory fields in connections we think.

PeteDuncanson avatar Jan 13 '22 11:01 PeteDuncanson

Removing the mandatory flag on the staffMember connections didn't work Re-reading the error, it's not the staffMember that's null, it's the user connection on staff that's returning as null

For reference in the error {"data":null,"errors":[{"path":null,"locations":[{"line":387,"column":11,"sourceName":null}],"message":"Validation error of type FieldUndefined: Field 'user' in type 'StaffMember' is undefined @ 'searchAddresss/items/area/accountManager/user'"},{"path":null,"locations":[{"line":416,"column":11,"sourceName":null}],"message":"Validation error of type FieldUndefined: Field 'user' in type 'StaffMember' is undefined @ 'searchAddresss/items/area/technician/user'"}]}

The technician and accountManager fields are of type StaffMember StaffMember does not have a user field, there used to be a connection for it but it was removed (This was to work around a DataStore issue so we could expose the userId on the StaffMember model)

So why is the ES index trying to reference a non existent field? How do we refresh this?

Removing and re-adding @searchable didn't work Running the ddb_to_es.py script on the table didn't work There's nothing in the UI to let us rebuild

JackPenney avatar Jan 13 '22 12:01 JackPenney

Here's the relevant bits of our Schema for reference

type StaffMember
  @model
  @key(name: "byEmail", fields: ["email"], queryField: "staffByEmail")
  @auth(
    rules: [
      { allow: groups, groups: ["admin"] }
      { allow: public, provider: apiKey }
    ]
  ) {
  id: ID!
  firstName: String!
  lastName: String!
  email: String
  phoneNumber: String
  roles: [String]
  roundDirectionPreference: String
  profilePictureUrl: String
  userID: ID
  # user: User @connection(fields: ["userID"]) THIS IS COMMENTED OUT AND HAS NOT BEEN ACTIVE FOR SOME TIME
  defaultSectorID: ID
  addressLine1: String
  addressLine2: String
  addressLine3: String
  town: String
  county: String
  postcode: String
  geoCords: Location
  hasGeocodingIssues: Boolean
}

type Area
  @model
  @searchable
  @auth(
    rules: [
      { allow: groups, groups: ["admin"] }
      { allow: public, provider: apiKey }
    ]
  )
  @key(name: "bySectorID", fields: ["sectorID"], queryField: "areaBySectorID") {
  id: ID!
  name: String!
  displayName: String!
  active: Boolean
  officePhone: String
  officeAddressName: String
  officeAddress1: String
  officeAddress2: String
  officeTown: String
  officePostcode: String
  officeEmail: String
  geoCords: Location
  hasGeocodingIssues: Boolean
  sectorID: ID!
  sector: Sector @connection(fields: ["sectorID"])
  accountManagerID: ID
  accountManager: StaffMember @connection(fields: ["accountManagerID"])
  technicianID: ID
  technician: StaffMember @connection(fields: ["technicianID"])
  mature: Boolean
  servicePricings: [ServicePricing]
    @connection(keyName: "byAreaID", fields: ["id"])
  postcodesCovered: [String]
  bankName: String
  bankSortCode: String
  bankAccountNumber: String
}

type Address
  @model
  @searchable
  @auth(
    rules: [
      { allow: groups, groups: ["admin"] }
      { allow: public, provider: apiKey }
    ]
  )
  @key(
    name: "byCustomerID"
    fields: ["customerID"]
    queryField: "addressesByCustomerID"
  )
  @key(
    name: "byAreaAndStatus"
    fields: ["areaID", "addressStatus"]
    queryField: "addressesByAreaAndStatus"
  )
  @key(name: "byAreaID", fields: ["areaID"], queryField: "addressesByAreaID") {
  id: ID!
  companyName: String
  addressLine1: String!
  addressLine2: String
  addressLine3: String
  town: String
  county: String
  postcode: String!
  phoneNumber: String
  bedrooms: Int!
  houseTypeID: ID!
  houseType: HouseType @connection(fields: ["houseTypeID"])
  hasConservatory: Boolean
  customerID: ID!
  customer: Customer! @connection(fields: ["customerID"])
  addressStatus: String!
  importantVisitSummary: String
  importantCAMSummary: String
  comments: [Comment] @connection(keyName: "bySourceID", fields: ["id"])
  subscribedServices: [ServiceSubscription]
    @connection(keyName: "byAddressID", fields: ["id"])
  sectorID: ID
  areaID: ID!
  area: Area! @connection(fields: ["areaID"])
  geoCords: Location
  hasGeocodingIssues: Boolean
}

I can attempt to re-add the user connection now we've removed DataStore from the project but it looks like it's just holding onto stale data and I can't find a way to refresh it

There's an option to delete the whole Index in the UI but that makes me nervous as we've been bit by similar UI interactions before (also unsure which index is for which environment) I don't know if that's "Safe" to do, I don't want to unknowingly break the project

JackPenney avatar Jan 13 '22 12:01 JackPenney

Re-adding the user connection to StaffMember fixed that error but what if we don't want to re-add an old connection? How would we work around that?

In Addition to the error reported, we're also getting these from the same search

Can't serialize value (/searchAddresss/items[1]/_lastChangedAt) : Unable to serialize 1.6190332464E12 as a valid timestamp. Ensure that the value provided is an Integer that lies within the limits specified in this scalar's description.

Not sure what to do about this one, _lastChangedAt is an internal field we can't touch

Cannot return null for non-nullable type: 'Customer' within parent 'Address' (/searchAddresss/items[0]/customer)

I've queried all addresses and checked that each customerID matches an existing customer and they all do, none of them return as null/undefined My first suspicion is stale data in the ES index but I'll see what I can find

JackPenney avatar Jan 13 '22 13:01 JackPenney

We're now also getting this error

Can't serialize value (/searchCustomers/items[16]/signUpDate) : Unable to serialize 2021-03-31T17:06:20.707Z as a valid date.

Our signUpDates were originally AWSDateTime but with the release of AWSDate we've been using that All of the data we have is in the date format without a timestamp eg. "2021-03-31"

Again it looks like the ES index has stale data it tries to validate that against the most recent schema

JackPenney avatar Jan 13 '22 14:01 JackPenney

Hey @PeteDuncanson and @JackPenney - I know we discussed some strategies around this last week - what is the current status?

The biggest dangling thread that I still see is from your comment "it shouldn't be possible to get in this state in the first place" - but I also want to make sure you've been able to get yourself unblocked.

johnpc avatar Jan 19 '22 16:01 johnpc

Hey @johnpc

So I tried clearing the index and reindexing via the python script in the docs (https://github.com/aws-amplify/amplify-cli/blob/master/packages/graphql-elasticsearch-transformer/scripts/ddb_to_es.py). The reindex said it worked but no updated data appears. Then then I tried deleting the index, then creating a new mapping and reindexing again. Script reports it works but no data appears. Then I gave up.

Since then I had the idea to spin up a new environment (which I hope will spin up a clean ES instance with the correct mappings) and then "steal" those mappings to apply to my other ES instance (or just use that instance for my other environment) but that again seems "wrong" so I'm loathed to spend time on it, feels like I shouldn't have to spin up a whole new environment each time I do a schema change.

Open to ideas, options and suggestion. I still don't understand how it go into this state...again :(

Cheers

Pete

PeteDuncanson avatar Jan 20 '22 19:01 PeteDuncanson

I guess this issue tracks the latest better, its all related I feel https://github.com/aws-amplify/amplify-cli/issues/9536#issuecomment-1016715768

PeteDuncanson avatar Jan 20 '22 19:01 PeteDuncanson

Closing this as a duplicate #9536

sachscode avatar Jan 21 '22 19:01 sachscode

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar Mar 23 '22 02:03 github-actions[bot]