1

First of all, I am aware that we cannot use the OR operator inside the KeyConditionExpression. I am storing my I18n keys in the database and I have the following data structure:

{
  "de": "Key in German",
  "en": "Key in English",
  "pk_id": "I18N-12345"
}

I also have a UI where I have a text field that should allow me to filter the keys by any language text. In this case english or german, represented by the keys de and en.

I wanted to achieve a query whose Key Condition Expression looks like this:

{
  KeyConditionExpression: "begins_with(#de, :search_text) OR begins_with(#en, :search_text)"
}

As said before, this would be ideal, but since we don't have the OR operation, I really don't know how to modify the query or how to create a GSI that allow me to make this query.

Thanks

Juan Rivillas
  • 897
  • 2
  • 9
  • 23

1 Answers1

0

You didn't explain what are the partition and sort keys in your example. Unfortunately, each table can only have one sort key - and only that sort key can be efficiently filtered using a begins_with() function as you did. And you can't have both "en" and "de" be sort keys of the same table.

But, there's something you can do with LSI to work around this limitation. You can have "en" be the sort key of the original table (I assume pk_id is the partition key?), and then create an LSI (it doesn't even need to be a GSI - LSI's are more efficient) whose partition key is the same (pk_id) but sort key is "de". Then, you just do every query twice - once on the base table with begins_with(#en, :search_text) and once on the LSI with `begins_with(#de, :search_text). The "OR" you wanted just means take both querys' results.

Nadav Har'El
  • 11,785
  • 1
  • 24
  • 45
  • Thank you so much for your explanation. I think that information is enough for me to solve the problem. Since we avoid to create LSI, I think I'll create two GSIs and make two separate queries. Thank you! – Juan Rivillas Jan 16 '20 at 17:03
  • Good luck. I think you don't need two GSIs - just one GSI (whose sort key is "de") plus the original table (whose sort key is "en"). Of course, if your original table had a different sort key (which you didn't mention), then, yes, you can use two GSIs, one on "de" and one on "en". – Nadav Har'El Jan 16 '20 at 17:05