MarkLogic Optic API聚合算术

时间:2018-06-07 14:58:29

标签: marklogic

我试图让Arithmetics使用MarkLogics Optic API来处理聚合函数,但我收到以下错误:

message=OPTIC-INVALARGS: fn.error(null, 'OPTIC-INVALARGS', -- Invalid arguments: right argument at 1 of op.multiply value must have xs.numeric datatype: op.count(op.col('count_42'), null)

我正在请求行端点:

POST http://marklogic:8000/LATEST/rows?database=SQLData HTTP/1.1

我正在尝试一个相当基本的查询,请参阅以下内容:

{
"$optic": {
    "ns": "op",
    "fn": "operators",
    "args": [{
            "ns": "op",
            "fn": "from-view",
            "args": [null, "employees", "e", null]
        }, {
            "ns": "op",
            "fn": "select",
            "args": [[{
                        "ns": "op",
                        "fn": "col",
                        "args": ["firstname"]
                    }, {
                        "ns": "op",
                        "fn": "col",
                        "args": ["lastname"]
                    }
                ], null]
        }, {
            "ns": "op",
            "fn": "group-by",
            "args": [[{
                        "ns": "op",
                        "fn": "col",
                        "args": ["firstname"]
                    }
                ], [{
                        "ns": "op",
                        "fn": "as",
                        "args": ["es", {
                                "ns": "op",
                                "fn": "multiply",
                                "args": [10, {
                                        "ns": "op",
                                        "fn": "count",
                                        "args": ["count_42"]
                                    }
                                ]
                            }
                        ]
                    }
                ]]
        }
    ]
  }
}

我不确定我是否做错了什么或端点不支持聚合算术。如果有人有线索,我将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:1)

op.count()聚合不是表达式函数。与其他聚合一样,它会填充列而不是返回值。该函数需要填充列的名称,并且必须出现在groupBy()操作的值列表的顶层:

http://docs.marklogic.com/op.count

它应该可以将乘法表达式移动到后续的select()操作中,该操作将填充的count列作为参数之一。换句话说,在分组操作之后,您可以使用表达式中的聚合列,方法与任何其他列相同。

通常,通过在QueryConsole中使用SJS或XQuery构建器测试Optic查询(使用limit()生成一个小结果集)可以更容易地进行调试。一旦查询按预期工作,使用op.export()生成JSON AST。

希望有帮助,