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

Categories

0 votes
930 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...