【问题标题】:checking for a contains in a specific string in cosmosDB graph using pythongremlin api使用 pythongremlin api 检查 cosmosDB 图中特定字符串中的包含
【发布时间】:2026-01-12 22:15:02
【问题描述】:

我在 azure cosmosdb 图上有 2 个“WEEK”顶点。

g.V().hasLabel('WEEK').valueMap()

输出:


{
    "type":["1 week|1 month|1 wk|one month|one week|one wk"]
},
{
    "type":["11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks"]
}

我正在尝试在“类型”属性中搜索 CONTAINS 并返回顶点。

STRING = "1 周"

g.V().hasLabel('WEEK').has('type',TextP.containing('1 week')).valueMap()

输出:


{
    "type":["1 week|1 month|1 wk|one month|one week|one wk"]
},
{
    "type":["11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks"]
}

我得到了所有的顶点,因为“11 周|11 个月|11 周|11 个月|11 周|11 周”也有“1 周”。

我的要求是我必须搜索包含操作,但应该只出现第一个顶点而不是第二个。

一个想法可以是更改“类型”属性中的数据并更改搜索字符串,如下所示

g.V().hasLabel('WEEK').valueMap()

输出:


{
    "type":["(1 week)|(1 month)|(1 wk)|(one month)|(one week)|(one wk)"]
},
{
    "type":["(11 weeks)|(11 months)|(11 wks)|(eleven months)|(eleven weeks)|(eleven wks)"]
}

STRING = "(1 周)"

g.V().hasLabel('WEEK').has('type',TextP.containing('(1 week)')).valueMap()

输出:


{
    "type":["(1 week)|(1 month)|(1 wk)|(one month)|(one week)|(one wk)"]
}

但是这样我们需要更改“type”属性中的整个数据,并且必须将 STRING 从“1 week”更改为“(1 week)”(因为“1 week”是从上游接收的)

请让我知道上述场景的任何其他想法(包含是强制性的)

提前致谢。

【问题讨论】:

  • 不要用大量语言标签向您的问题发送垃圾邮件。只选择你正在使用的那个。是 java、python 还是 C#?此外,它似乎与 azure cosmosdb 无关
  • 请指定您使用的语言(看起来像 python?)。否则,我只能建议看看正则表达式

标签: python-3.x gremlin logical-operators azure-cosmosdb-gremlinapi


【解决方案1】:

您可以搜索包含您的搜索字符串 + 具有相同数量的字符的项目?

【讨论】:

    【解决方案2】:

    您可以在数据的开头放置一个|,然后查找

     STRING= "|1 week"
    

    【讨论】:

      【解决方案3】:

      您的问题有点令人困惑,但类似下面的内容应该返回 ONLY LIST[0],因为它只是 LIST[2]。它只会返回第一个数组位置的字符串,即 LIST[0]

      public string[] LIST = { "1 week|1 month|1 wk|one month|one week| one wk","11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks"};
      public string STRING = "1 Week";
      public int LISTLength = LIST.Length;
      
      for(int x = 0; x < LISTLength; x++)
       {
         if (LIST[x] == STRING)
           {
             //Your action here
           }
       }
      

      【讨论】:

        最近更新 更多