代码之家  ›  专栏  ›  技术社区  ›  shruti

Q: Azure Cosmos DB Graph:当索引策略定义为手动时,如何在Graph API中运行查询?

  •  3
  • shruti  · 技术社区  · 8 年前

    在Cosmos DB graph中,当我将索引策略定义为自动时,我能够运行查询,但当我将索引策略更新为手动并定义索引路径时(/label/?)如果索引模式设置为“一致”,则查询不会获取任何数据。

    假设我的第一个查询(当索引策略设置为手动时)是:

    g.addV('Azure').property('name','Cerulean Software'))
    

    结果是:

    [
        {
            "id": "0c14a00a-edf6-46b1-9e40-45cc37f750ea",
            "label": "Azure",
            "type": "vertex",
            "properties": {
                "name": [
                    {
                        "id": "f89ee2ee-74df-4256-a5d4-2b47eb526976",
                        "value": "Cerulean Software"
                    }
                ]
            }
        }
    ]
    

    现在,我的第二个查询(当索引策略设置为手动时(请参见下面的编辑#1))是:

    g.V().hasLabel('Azure')
    

    即使在名为“Azure”的图中存在顶点,第二个查询也不会获取任何结果。

    这背后可能的原因是什么?

    编辑#1:更改前手动索引策略

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

    编辑#2:更改后的手动索引策略

    "indexingPolicy": {
        "automatic": false,
        "excludedPaths": [],
        "includedPaths": [
            {
                "path": "/*",
                "indexes": [
                    {
                        "dataType": "Number",
                        "kind": "Range",
                        "precision": -1
                    },
                    {
                        "dataType": "String",
                        "kind": "Hash",
                        "precision": 3
                    }
                ]
            },
            {
                "path": "/_isEdge/?",
                "indexes": [
                    {
                        "dataType": "String",
                        "kind": "Hash",
                        "precision": 3
                    },
                    {
                        "dataType": "Number",
                        "kind": "Range",
                        "precision": -1
                    }
                ]
            }
        ],
        "indexingMode": "consistent"
    },
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Jesse Carter    8 年前

    对于Cosmos,图形语句不会在Azure端作为遍历执行。图形客户端实际上将gremlin语句转换为文档SQL调用,然后在客户端将结果聚合回给您。就你的陈述而言 g.V().hasLabel('Azure') 呼叫实际上被转换为 {"query":"SELECT N_2 FROM Node N_2 WHERE (IS_DEFINED(N_2._isEdge) = false AND (N_2.label = 'Azure'))"}

    这可以通过使用代理(如Fiddler)进行验证,该代理将允许您检查机器的出站呼叫。

    _isEdge 属性似乎适用于几乎所有Gremlin翻译的查询,因此我怀疑如果将该属性添加到索引策略中,您应该会看到预期的结果。

    编辑: automatic: false . According to the Cosmos docs Opting in and opting out of indexing By default, all documents are automatically indexed, but you can choose to turn it off. When indexing is turned off, documents can be accessed only through their self-links or by queries using ID.

    如果您选择在索引关闭的情况下运行,那么索引策略的其余部分实际上是没有意义的,并且不直接按文档Id查询的查询将不再工作。你能详细说明一下你在这里真正想要完成什么吗?似乎有点混乱。您放置的索引设置 label isEdge * 这是匹配所有路径的默认规则。

    张贴你的索引策略的目标,我可以尝试提出一个适当的建议,但你肯定会想把 automatic: true