amplify-cli: Appsync cannot sort items by string value with Elasticsearch

Describe the bug I’m using amplify-cli to generate appsync resolvers with @searchable directive, but it seems that search resolver cannot sort items by String value, but it works with AWSDateTime value.

To Reproduce Example request:

{
  searchUsers(sort: {
    field: fullName,
    direction: asc
  }) {
    items {
      id
      fullName
    }
  }
}

Expected behavior User items sorted by fullName.

Screenshots image

Desktop (please complete the following information):

  • Using AWS Console

Smartphone (please complete the following information):

  • Using AWS Console

Additional context It also happened to my past appsync project.

You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = ‘DEBUG’; in your app.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 8
  • Comments: 15 (2 by maintainers)

Most upvoted comments

@hakimio is right, AWS decided do not overhead it with fielddata, resouce:

Fielddata can consume a lot of heap space, especially when loading high cardinality text fields. Once fielddata has been loaded into the heap, it remains there for the lifetime of the segment. Also, loading fielddata is an expensive process which can cause users to experience latency hits. This is why fielddata is disabled by default.

Resource: Elasticsearch docs

I guess AWS team decided to use keyword as mentioned in the docs

This query works perfectly:

GET your_pefect_index/_search
{
  "sort": [
      {
        "your_pefect_field.keyword": {
          "order": "asc"
        }
      }
    ]
}

I haven’t figured out how to make it work from JS side yet

@ShepelievD The velocity templating language allows you to do a check like if string field array contains sort field, add “.keyword” postfix, otherwise leave it as it is.

This does not work (elasticsearch 400 argument error )

const users = await API.graphql(graphqlOperation(searchUser, {
  sort: {
    field: "name", // string type
    direction: "asc"
  }
}));

You can fix that by modifying the resolver to use “name.keyword” for field name when the field is of string type like I shown in my previous post.