Query GSI
sheshankkodam opened this issue · comments
Is it possible to query with index name?
For example, I have a user table with index name user-index
and would like to get all orders by userId
. Here is the aws cli command
aws dynamodb query --table-name customer --index-name user-index --key-condition-expression "userId = :userId and orderId = :orderId" --expression-attribute-values '{":userId": {"S": "tom"}}'
Following is the migration file for User table
defmodule App.Repo.Migrations.AddUserTable do
use Ecto.Migration
def change do
create table(:user, primary_key: false,
options: [
global_indexes: [
[index_name: "user-index",
keys: [:userId, :orderId],
provisioned_throughput: [20,20],
create_if_not_exists: true]
],
provisioned_throughput: [20,20]
]
) do
add :userId, :string, hash_key: true # gsi primary key
add :orderId, :string, hash_key: true # gsi sort key
timestamps()
end
end
end
Hello @darrenklein any update on this?
I believe that any query on those keys will use that index. Off the top of my head -
Repo.all(from m in Model, where: m.userId == "foo" and m.orderId == "bar")
I'm not sure that Ecto supports queries on name-specified indexes.
@alhambra1 correct me if I'm wrong on this.
@sheshankkodam Please close this issue if my answer has been sufficient.
yes it worked. Thanks @darrenklein
@sheshankkodam we've been revisiting this topic, and feel that, although the adapter's default behavior is to select the "best" index based on the query, there are some circumstances where the user may want to be able to specify the index to be used. Keep an eye on issue #25, it's something we might try to include in the relatively near future.
sure thanks @darrenklein
@sheshankkodam Ok, this feature is now available in the latest release, version 1.2.0