aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

Unable to use @searchable with sort param, breaks pagination #5009

Closed VicFrolov closed 3 years ago

VicFrolov commented 4 years ago

Describe the bug When calling the search query on a @model with @searchable generated from the API, it inaccurately returns an empty array when sorting AND paginating. If I remove the sort param, it returns all the values as expected.

To Reproduce If I have the following model

type Foo
  @model
  @key(fields: ["id"])
  @searchable
 {
  id: ID!
  bar: ID! 
  count: Int!
}

the following query

query searchFoo {
  searchFoo(
    filter: { bar: { eq: "SOME_ID" }, 
    limit: 5,
    nextToken: "SOME_NEXT_TOKEN_OR_UNDEFINED",
    sort:{ field: count, direction: desc }
  ) {
    items {
      id
      count
    }
    nextToken
  }
}

If I comment out sort:{ field: count, direction: desc }, nextToken format is e19097z0-f5f9-22f1-3369-addd5aacf3d0, and I am able to successfully paginate through all the data. if I call it with sort,

1) the token becomes the float value of the int count 2) when the nextToken is 0.0, pagination breaks and the payload has an empty array for items, when I should be getting some.

I've also noticed that if I have 9 items, and I remove the limit (it then defaults to 10), it still returns a nextToken value of "0.0", which if used as nextToken returns empty items array. Expected behavior for the return, but strange that nextToken is populated if there are no more items...

Expected behavior Pagination to work when sorting a search query

Environment System: OS: macOS 10.15.3 CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz Memory: 120.73 MB / 16.00 GB Shell: 5.7.1 - /bin/zsh Binaries: Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node Yarn: 1.10.1 - /usr/local/bin/yarn npm: 6.9.0 - ~/.nvm/versions/node/v10.16.0/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman Browsers: Chrome: 84.0.4147.105 Safari: 13.0.5 npmPackages: @apollo/client: ^3.0.0-rc.3 => 3.0.0-rc.3 @apollo/react-common: 3.1.3 => 3.1.3 @apollo/react-components: 3.1.3 => 3.1.3 @apollo/react-hoc: 3.1.3 => 3.1.3 @apollo/react-hooks: 3.1.3 => 3.1.3 @apollo/react-testing: 3.1.3 => 3.1.3 @aws-amplify/auth: 2.1.6 => 2.1.6 @aws-amplify/core: 2.2.5 => 2.2.5 @aws-amplify/storage: 2.2.1 => 2.2.1 @babel/core: 7.5.0 => 7.5.0 @babel/runtime: 7.5.1 => 7.5.1 @react-native-community/datetimepicker: 2.2.1 => 2.2.1 @react-native-community/masked-view: 0.1.6 => 0.1.6 @react-native-community/netinfo: 4.7.0 => 4.7.0 @react-navigation/bottom-tabs: 5.0.0 => 5.0.0 @react-navigation/material-top-tabs: 5.0.0 => 5.0.0 @react-navigation/native: 5.0.0 => 5.0.0 @react-navigation/stack: 5.0.0 => 5.0.0 @types/bugsnag: 2.5.28 => 2.5.28 @types/jest: 24.0.15 => 24.0.15 @types/lodash: 4.14.144 => 4.14.144 @types/moment: 2.13.0 => 2.13.0 @types/pluralize: 0.0.29 => 0.0.29 @types/react: 16.8.23 => 16.8.23 @types/react-native: 0.57.65 => 0.57.65 @types/react-native-material-textfield: 0.12.3 => 0.12.3 @types/react-native-vector-icons: 6.4.4 => 6.4.4 @types/react-test-renderer: 16.8.2 => 16.8.2 @types/uuid: 3.4.6 => 3.4.6 amazon-cognito-identity-js: 3.2.0 => 3.2.0 apollo-cache-inmemory: 1.6.3 => 1.6.3 apollo-client: 2.6.10 => 2.6.10 apollo-link-context: 1.0.19 => 1.0.19 apollo-link-error: 1.1.12 => 1.1.12 apollo-link-http: 1.5.16 => 1.5.16 aws-amplify-react-native: 4.0.3 => 4.0.3 aws-appsync: 3.0.2 => 3.0.2 aws-appsync-auth-link: 2.0.1 => 2.0.1 aws-appsync-react: 3.0.2 => 3.0.2 aws-appsync-subscription-link: 2.0.1 => 2.0.1 babel-jest: 24.8.0 => 24.8.0 babel-plugin-module-resolver: 3.2.0 => 3.2.0 bugsnag-react-native: 2.23.2 => 2.23.2 graphql: 14.6.0 => 14.6.0 graphql-anywhere: 4.2.7 => 4.2.7 graphql-tag: ^2.10.4 => 2.10.4 husky: 3.0.0 => 3.0.0 jest: 24.8.0 => 24.8.0 lodash: 4.17.15 => 4.17.15 lottie-ios: 3.1.3 => 3.1.3 lottie-react-native: 3.3.2 => 3.3.2 metro-react-native-babel-preset: 0.58.0 => 0.58.0 moment: 2.24.0 => 2.24.0 pluralize: 8.0.0 => 8.0.0 prettier: 1.19.1 => 1.19.1 react: 16.11.0 => 16.11.0 react-apollo: 3.1.3 => 3.1.3 react-native: 0.62.2 => 0.62.2 react-native-animatable: 1.3.3 => 1.3.3 react-native-animation-hooks: 1.0.1 => 1.0.1 react-native-camera: 3.8.0 => 3.8.0 react-native-confetti-cannon: 1.1.3 => 1.1.3 react-native-device-info: 5.5.8 => 5.5.8 react-native-fast-image: 7.0.2 => 7.0.2 react-native-geocoding: 0.4.0 => 0.4.0 react-native-gesture-handler: 1.5.2 => 1.5.2 react-native-haptic-feedback: 1.10.0 => 1.10.0 react-native-image-picker: 0.27.1 => 0.27.1 react-native-image-resizer: 1.2.0 => 1.2.0 react-native-image-viewing: 0.1.6 => 0.1.6 react-native-location: 2.5.0 => 2.5.0 react-native-material-textfield: github:n4kz/react-native-material-textfield#729008b847eb38129d0b886e98253d4c11f6d4d3 => 0.16.1 react-native-modal: 11.3.1 => 11.3.1 react-native-reanimated: 1.4.0 => 1.4.0 react-native-safe-area-context: 0.6.2 => 0.6.2 react-native-screens: 2.0.0-alpha.23 => 2.0.0-alpha.23 react-native-svg: 9.9.4 => 9.9.4 react-native-svg-transformer: 0.13.0 => 0.13.0 react-native-vector-icons: 6.6.0 => 6.6.0 react-test-renderer: 16.11.0 => 16.11.0 tslint: 5.18.0 => 5.18.0 tslint-config-prettier: 1.18.0 => 1.18.0 tslint-config-standard: 8.0.1 => 8.0.1 tslint-react: 4.0.0 => 4.0.0 typescript: 3.7.2 => 3.7.2 uuid: 3.3.3 => 3.3.3 npmGlobalPackages: @aws-amplify/cli: 4.26.0 amplify-cli: 1.0.0 amplify: 0.0.11 gatsby-cli: 2.7.21 npm: 6.9.0 react-native-cli: 2.0.1 react-native-git-upgrade: 0.2.7 tslint-to-eslint-config: 0.5.1
akshbhu commented 4 years ago

Hi @VicFrolov

I have few questions:

VicFrolov commented 4 years ago

Hey @akshbhu ,

1) yes it does. 2) shoot, I can't remember which version I was on, I'm on 4.27.3 right now, but I updated recently, and added @searchable to another @model (and ran the script to load all existing entries) on this version.

Another more simple example just turned up with sorting not working on this new @model I have:

searchFoo(
  filter: { bar: { eq: "someBarString" } }
  sort: { field: updatedAt, direction: asc } # if I comment this, it returns all the values
) {
  items {
    id
  }
}

assuming the filter attribute is valid, I get the values I want if I comment out the sort line, but if I add it (I tried createdAt and updatedAt), empty array is returned.

I do my PUT/CREATE operations via a custom lambda function, and I set updatedAt/createdAt myself. My format is: 2020-08-15T21:16:43.608Z, perhaps this is causing issues? I doubt it, because when I check out kibana, it is able to parse updatedAt/createdAt without any issues in the GUI.

VicFrolov commented 4 years ago

In case it's helpful, here is the data for one of these new Foo items in ES:

{
  "_index": "newFooExample",
  "_type": "doc",
  "_id": "05d6d41b-81c0-4287-a1c4-6a0e10e00a27|98aa805f-3867-4be6-9b93-cda139f285d0",
  "_version": 1,
  "_score": 1,
  "_source": {
    "createdAt": "2020-08-15T21:16:43.608Z",
    "owner": "98aa805f-3867-4be6-9b93-cda139f285d0",
    "bar": "someBarString",
    "id": "5d2a5a8b-bad8-4d58-bce8-48eb431ebcb8",
    "someUserID": "05d6d41b-81c0-4287-a1c4-6a0e10e00a27",
    "updatedAt": "2020-08-15T21:16:43.608Z"
  },
  "fields": {
    "createdAt": [
      "2020-08-15T21:16:43.608Z"
    ],
    "updatedAt": [
      "2020-08-15T21:16:43.608Z"
    ]
  }
}
VicFrolov commented 4 years ago

@akshbhu bump, don't want this issue to be marked stale

taneba commented 3 years ago

I have the same issue. When I use sort with field id, it returns correct size of result, but it doesn't work correctly with any other sort field.

akshbhu commented 3 years ago

Hi @VicFrolov

I think there are 2 issues. One with sorting with createdAt and other with bar field.

For the bar field I tried to reproduce this but I am getting sorted results correctly using the same filter you described above. I tried with the latest amplify version.

Can you also tell me if @searchable was added to any existing project or you created a new project when faced this error?

@taneba @VicFrolov

For the search results when using createdAt , Can you check if in your schema this field has type String or AWSDateTime?

If its a String Type, can you convert that to AWSDateTime and try the seach again?

Let me know if that works for you?

akshbhu commented 3 years ago

Closing this due to lack of response. Feel free to comment if the above thing doesnt works for you

VicFrolov commented 3 years ago

Hi @akshbhu , Sorry for the late reply, for the above case I did a workaround and called it a day, but the issue has crept back in the exact same way for a new query as the original comment.

I have an attribute fooBar that is of type Float!, if I sort, descending on fooBar, I only get the first page of values (or values that are > 0.0), once I reach 0.0, pagination stops, nextToken is "0.0" and results are an empty array when I should still have hundreds of results. if I remove the sort, I get all values as expected, and the nextToken is not "0.0", and pagination works perfectly.

I created this project a while ago, and have definitely upgraded the cli various times, currently on the latest version.I'm fairly confident when I added @searchable to this particular model, it was right away before adding any data, as I knew I would be searching on it, and I do remember running the migration script for some models that were added later.

Another note, if I filter using match, e.g. by some id, it most certainly does not match correctly. If I use matchPhrase, then the results aren't muddied. There seems to be various issues occurring, and hard to pin point how or why, as ES data via kibana looks correct.

github-actions[bot] commented 3 years ago

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.