【问题标题】:Azure DocumentDB partial search against multiple fields针对多个字段的 Azure DocumentDB 部分搜索
【发布时间】:2016-05-11 14:33:43
【问题描述】:

我正在尝试使用部分搜索序列号来查询文档。文档的结构如下:

   {
      "id": "AAAAA",
      "serialNumber": "AAA12345",
      "type1Component":
        {
          "componentId": "component1",
          "serialNumber": "BBBCOMPSN1"
        },
      "type2Component": 
        {
          "componentId": "component2",
          "serialNumber": "CCCCOMPSN2"
        },
      "subComponents": [
        "ZZZCOMP9",
        "YYYCOMP8",
        "XXXCOMP7",
        "ZZZCOMP6"
      ]
    }

查询应返回任何顶级或组件序列号包含与输入部分匹配的文档。例如,如果搜索条件为以下任何一项,则上述文档应返回:AAA、BBB、CCC、XXX、YYY、ZZZ

我不知道该怎么做。这是我迄今为止最好的尝试:

    SELECT VALUE root FROM root 
 join comp1 in root.type1Component
 join comp2 in root.type2Component
 join sub in root.subComponents
 where ( contains(upper(root.serialNumber), 'AAA') or
         contains(upper(comp1.serialNumber), 'AAA') or
         contains(upper(comp2.serialNumber), 'AAA') or 
         contains(upper(sub), 'AAA')
       ) 

但是这个查询没有命中。谁能告诉我我做错了什么?

【问题讨论】:

    标签: azure-cosmosdb


    【解决方案1】:

    subComponents 数组会导致这种方法出现问题,因为您需要将 ARRAY_CONTAINS 与字符串 CONTAINS 组合在一起,而在查询中无法做到这一点。

    另外,upper() 的使用意味着它将是全表扫描。如果您可以接受,您可以使用以下用户定义函数 (UDF) 来完成此操作:

    function snSearch (rootSN, sub1, sub2, subArray, s) {
    
      if (rootSN.toUpperCase().indexOf(s) >= 0) {
        return true;
      };
    
      if (sub1.serialNumber.toUpperCase().indexOf(s) >= 0) {
        return true;
      };
    
      if (sub2.serialNumber.toUpperCase().indexOf(s) >= 0) {
        return true;
      };
    
      var i, len, row;
      for (i = 0, len = subArray.length; i < len; i++) {
        row = subArray[i];
        if (row.toUpperCase().indexOf(s) >= 0) {
          return true;
        };
      };
    
      return false;
    }
    

    你在这样的查询中调用它:

    SELECT * FROM c WHERE 
      udf.snSearch(c.serialNumber, c.type1Component, c.type2Component, c.subComponents, 'ZZZ')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多