简体   繁体   中英

How to get all entries using query state with Dapr?

I use state store in Dapr to save and get items to/from Redis using .NET client, it works fine. Now I need to get all entries with query state . I try different forms of query:

var query1 = """
    {
      "filter": {}
    }
    """;
var query2 = """
    "filter": {}
    """;
var query3 = "{}";

var queryResult = await daprClient.QueryStateAsync<Product>("statestore", query, cancellationToken: ct);

But I constantly get failed query in state store statestore: query index not found :

Dapr.DaprException: Query state operation failed: the Dapr endpointed indicated a failure. See InnerException for details.
       ---> Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="failed query in state store statestore: query index not found")
         at Dapr.Client.DaprClientGrpc.QueryStateAsync[TValue](String storeName, String jsonQuery, IReadOnlyDictionary`2 metadata, CancellationToken cancellationToken)

What is the correct form for the query? Or do I need to initialize the store? I haven't found anytning about that in the documentation.

You first need to run a version of Redis that has 2 modules enabled:

  1. RediSearch: a full-featured search engine
  2. RedisJSON: a native JSON data type

This can be done by using this docker command:

 docker run --name redis2 -d -p 6380:6379 redislabs/redismod

Next, add query indexes to your Dapr config as described here .

Example:

  metadata:
  - name: redisHost
    value: "localhost:6380"
  - name: queryIndexes
    value: |
      [
        {
          "name": "orgIndx",
          "indexes": [
            {
              "key": "person.org",
              "type": "TEXT"
            },
            {
              "key": "state",
              "type": "TEXT"
            }
          ]
        }
      ]  

Finally, in your query, add metadata to indicate which index should be used for that specific query, like this:

Dictionary<string, string> metadata = new() { "contentType", "application/json" }, { "queryIndexName", "orgIndx" } };
var queryResult = await daprClient.QueryStateAsync<Product>("statestore", query, cancellationToken: ct, metadata: metadata);

Make sure to pass at least the contentType metadata during your write operations too.

Once completed, your query will work. Keep an eye on this GH issue, and upvote, as this is clearly not a great developer experience ootb.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM