Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

azure - DocumentDB: Removing default indexing

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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": "/*"
    }
  ]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...