DocumentDb: Querying without an index

我的梦境 提交于 2019-12-23 21:48:39

问题


When excluding all paths from indexing, how come I am still able to perform a successful query on fields other than ID?

Excluding all paths:

collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath()
    {
        Path = "/*"
    });

Query:

SELECT * FROM c where c.Key = "117dfd49-a71d-413b-a9b1-841e88db06e8"

Accordingly this MSDN article:

When indexing is turned off, documents can be accessed only through their self-links or by queries using ID.

EDIT:

When looking into the indexing policy I see what my paths aren't excluded:

{
  "indexingMode": "consistent",
  "automatic": true,
  "includedPaths": [
    {
      "path": "/*",
      "indexes": [
        {
          "kind": "Range",
          "dataType": "Number",
          "precision": -1
        },
        {
          "kind": "Hash",
          "dataType": "String",
          "precision": 3
        }
      ]
    },
    {
      "path": "/\"_ts\"/?",
      "indexes": [
        {
          "kind": "Range",
          "dataType": "Number",
          "precision": -1
        },
        {
          "kind": "Hash",
          "dataType": "String",
          "precision": 3
        }
      ]
    }
  ],
  "excludedPaths": []
}

What am I doing wrong?

I am using consistent indexing, so surely this is not an issue of eventual consistency with the index propagation, if I understand it correctly?

Here is all the code:

var collectionSpec = new DocumentCollection { Id = collectionId };
var requestOptions = new RequestOptions { OfferType = "S1" };

collection = Client.CreateDocumentCollectionAsync(databaseLink, collectionSpec, requestOptions).Result;

collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath()
    {
        Path = "/*"
    });

回答1:


I don't see any call to replace the document collection after setting up the new indexing policy.

You need to call Client.ReplaceDocumentCollectionAsync() to have your indexing policy changes take effect.

The method documentation is here.

And here's an article written by the DocumentDB team about updating index policies online (which refers to the need to call ReplaceDocumentCollectionAsync()).




回答2:


You will want to change the code snippet to define the indexing policy on the collection spec prior to client.CreateDocumentCollectionAsync() - so that it is included in the network request for creating a collection to DocumentDB.

Setting a custom Indexing Policy at the time of Collection Creation

var collection = new DocumentCollection { Id = "myCollection" };

collection.IndexingPolicy.IndexingMode = IndexingMode.Consistent;

collection.IndexingPolicy.IncludedPaths.Add(
    new IncludedPath { 
        Path = "/*", 
        Indexes = new Collection<Index> { 
            new RangeIndex(DataType.String) { Precision = -1 }, 
            new RangeIndex(DataType.Number) { Precision = -1 }
        }
    });

await client.CreateDocumentCollectionAsync(database.SelfLink, collection);

Updating Indexing Policy on an Existing Collection

As David mentioned - you can also update the index policy on an existing collection with Client.ReplaceDocumentCollectionAsync().



来源:https://stackoverflow.com/questions/32733740/documentdb-querying-without-an-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!