DocumentDB: Removing default indexing

我怕爱的太早我们不能终老 提交于 2019-12-18 04:23:15

问题


I am trying to remove the default indexes which are created for a new collection:

{
  "indexingMode": "lazy",
  "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": []
}

As far as I understand, this will index every attribute in every resource and its sub-resources.

When attempting to exclude everything using this:

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

client.ReplaceDocumentCollectionAsync(collection).Wait();

I get the following error on ReplaceDocumentCollectionAsync():

The indexing path '/*' could not be accepted. Please ensure that the path is unique across all sets of indexing paths and it's valid.

I want to be able to define my own, custom, index paths. In order to do this, I need to remove the default indexes (which index everything).

UPDATE

I have removed the index by assigning the includes to an empty collection AND by excluding all paths:

collection.IndexingPolicy.IncludedPaths = new Collection<IncludedPath>();
collection.IndexingPolicy.ExcludedPaths = new Collection<ExcludedPath>();
collection.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath
{
    Path = "/*"
});

Note 1: For some reason, if only doing the first statement, nothing changes on the index policy... and without error.

Note 2: ExcludePaths has to be set to an empty collection initially or else (if the path already exists) it will detect the conflict and throw a error (when doing a ReplaceDocumentCollectionAsync of course).

Indexing document:

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

I assume that the /_ts/? path is mandatory.


回答1:


Sounds like you've pretty much figured it out. To clarify, id and _ts are treated as special properties in regards to indexing.

  • id is implicitly treated as the document's primary key - in which, id will always be indexed with uniqueness enforced.

  • _ts is an epoch timestamp of when a document was last written (create or replace), and will also always be indexed. This property will be explicitly noted within the index policy.

The following indexing policy illustrated how to index only the document.prop.subprop property (along with id and _ts):

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



回答2:


An easy way to exclude everything would be to switch indexingMode: None



来源:https://stackoverflow.com/questions/33981829/documentdb-removing-default-indexing

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