Elfocrash / Cosmonaut

🌐 A supercharged Azure CosmosDB .NET SDK with ORM support
https://cosmonaut.readthedocs.io
MIT License
342 stars 44 forks source link

Cross partition query is required but disabled #39

Closed leonpotgieter closed 5 years ago

leonpotgieter commented 5 years ago

Hi, I'm seeing the following since updating to the latest version of Cosmonaut. Where/how would I best enable Cross partition query, can't seem to find it. Many thanks, L

Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception. ActivityId: dfb5cf0a-cdff-4b1b-9996-2c714b5690bd, Microsoft.Azure.Documents.Common/2.1.0.0, Darwin/10.14 documentdb-netcore-sdk/2.1.3

leonpotgieter commented 5 years ago

Have been experimenting further and tried the following but still getting the Cross partition query required issue.. Any assistance would be appreciated. Tx.

var _tracks = await _searchtrackStore.QueryMultipleAsync($"select * from c", new FeedOptions() { EnableCrossPartitionQuery = true });

Elfocrash commented 5 years ago

Hello @leonpotgieter,

I would recommend you check the parameters you are providing. It looks like you are providing the FeedOptions in the place of the parameters object.

Try this: var _tracks = await _searchtrackStore.QueryMultipleAsync<Models.StreamSearchTrack>($"select * from c", feedOptions: new FeedOptions() { EnableCrossPartitionQuery = true });

It also worths noting that you can do the same thing by doing this: var _tracks = await _searchtrackStore.Query(new FeedOptions() { EnableCrossPartitionQuery = true }).ToListAsync();

leonpotgieter commented 5 years ago

Tx, ok I tried that but still being told that Cross Partition query is disabled... Anything else you could recommend I try to get it working? Perhaps in CosmoStoreSettings?

Elfocrash commented 5 years ago

Do you mind providing the StreamSearchTrack contents? I will try and recreate locally. Thanks!

leonpotgieter commented 5 years ago

Sure, some simple code below as well which may assist....

public static ICosmosStore _searchtrackStore;

public static async Task InitCosmosDataStores() { var setting = new CosmosStoreSettings("streamstagingdatabase","https://ss..documents.azure.com:443/", "secret", defaultCollectionThroughput: 1000);

        _searchtrackStore = new CosmosStore<Models.StreamSearchTrack>(setting);
    }

Models.StreamSearchTrack

using System; using System.Collections.Generic; using Cosmonaut; using Cosmonaut.Attributes;

namespace StreamCore.Models { [SharedCosmosCollection("stagingcollection")] public class StreamSearchTrack : ISharedCosmosEntity { public string Id { get; set; } = Guid.NewGuid().ToString(); public string Description { get; set; } = ""; public string SearchTrack { get; set; } = ""; public string Username { get; set; } = "";

    public KeyValuePair<string, string> KVMeta { get; set; } = new KeyValuePair<string, string>();

    public DateTime? FirstActivated { get; set; }
    public DateTime? LastDeActivated { get; set; }

    public Boolean Active { get; set; } = false;

    public string CosmosEntityName { get; set; }
}

}

Sample Doc

{ "Description": "", "SearchTrack": "Macbook", "Username": "", "KVMeta": { "Key": null, "Value": null }, "FirstActivated": "2018-09-04T08:34:28.4794+00:00", "LastDeActivated": null, "Active": false, "CosmosEntityName": "streamsearchtracks", "id": "65db2257-576f-4bd9-8fb3-831babb4e3fc", "_rid": "bBU-AP5LueUBAAAAAAAAAA==", "_self": "dbs/bBU-AA==/colls/bBU-AP5LueU=/docs/bBU-AP5LueUBAAAAAAAAAA==/", "_etag": "\"a500d975-0000-0000-0000-5b9ebd3d0000\"", "_attachments": "attachments/", "_ts": 1537129789 }

Elfocrash commented 5 years ago

Can you let me know which property is the partition key please?

Elfocrash commented 5 years ago

I just tested your code with SearchTrack being the partition key definition and I was unable to recreate it. All of the methods mentioned above work.

leonpotgieter commented 5 years ago

Was afraid you going to say something like that, let me investigate a little further to try figure out what's happening... will revert shortly.

leonpotgieter commented 5 years ago

Hi Nick, using /id as the partition key and still seeing the same issue. Is there any reason why this would be happening or why I shouldn't use /id?

Elfocrash commented 5 years ago

I just tested the exact scenario on the CosmosDB emulator and I'm unable to recreate it. Is there some type of information you are not sharing?

(Unrelated object properties removed for simplicity.)

image

image

leonpotgieter commented 5 years ago

Don't know what more to share... can't explain it.

If I use the Azure.DocumentDB.Core (2.1.3) client with the following code it works fine. Btw running on Visual Studio Mac (console app)... not that I think that should make a difference.

string query = $"select top 10 * from c where c.CosmosEntityName='streamsearchtracks'";
var results = _client.CreateDocumentQuery<dynamic>(_collection.DocumentsLink, query, new FeedOptions { EnableCrossPartitionQuery = true }).ToArray();
Elfocrash commented 5 years ago

Ok let's try to clear some things out.

Can you please change your Id property from this: public string Id { get; set; } = Guid.NewGuid().ToString();

To this:

[JsonProperty("id")]
[CosmosPartitionKey]
public string Id { get; set; } = Guid.NewGuid().ToString();

And the run all of the following lines of code and tell me what happens for each one:

  1. var result = await _searchtrackStore.QueryMultipleAsync<Models.StreamSearchTrack>("select * from c");
  2. var result = await _searchtrackStore.QueryMultipleAsync("select * from c");
  3. var result = await _searchtrackStore.Query("select * from c").ToListAsync();
  4. var result = await _searchtrackStore.Query().ToListAsync();
leonpotgieter commented 5 years ago

Hi Nick, Ok so adding [CosmosPartitionKey] has resolved the problem so I'm not going to hassle you with the rest of the queries. Missed this in your documentation and wasn't aware it was a requirement as I've never used anything similar with the MS DocumentDB client. Thank you SO much for your assistance in sorting this out.

Elfocrash commented 5 years ago

No worries, happy the problem is solved! Feel free to open a different issue if you have any other problems.