我有很多存储在Cloudant数据库中的json文档。文件结构:
{
"_id": "00229e31d5751d337abf409a4bd75492",
"_rev": "1-d95d7ad32264d233453a0436b1557e7d",
"timestamp": "2017-07-04T21:28:46.886Z",
"APIresponse": {
"intents": [
{
"intent": "greetings",
"confidence": 1
},
{
"intent": "machineBusy",
"confidence": 0
},
{
"intent": "set_weights",
"confidence": 0
},
{
"intent": "faqGuidelinesPAAmount",
"confidence": 0
},
{
"intent": "chat",
"confidence": 0
},
{
"intent": "feedback-no",
"confidence": 0
},
{
"intent": "faqGuidelinesWhatsnew",
"confidence": 0
},
{
"intent": "faqGuidelinesChildren",
"confidence": 0
},
{
"intent": "thanks",
"confidence": 0
},
{
"intent": "faqGuidelinesPAvsFederal",
"confidence": 0
}
],
"entities": [],
"input": {
"text": "hey"
},
"output": {
"text": [
"Hey there!",
"How a beautiful day to train :) Do you agree #firstName?"
],
"nodes_visited": [
"Greetings",
"Initial greetings",
"node_2_1495711348295"
],
"error": "SpelEvaluationException when evaluating DialogNode Id [check PROFILING interrupted ASPFINDER]. Condition [$trainingContext.status == \"no_tp\" && $aspFinder.dob != null || $aspFinder.height != null || $aspFinder.weight != null || $aspFinder.impedIsPresent != null || $aspFinder.goal != null || $aspFinder.timesAWeek != null || $aspFinder.duration != null || $aspFinder.location != null || $aspFinder.level != null || $userContext.height != null] evaluated to FALSE.\norg.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'weight' cannot be found on null\n",
"log_messages": [
{
"level": "err",
"msg": "SpelEvaluationException when evaluating DialogNode Id [check PROFILING interrupted ASPFINDER]. Condition [$trainingContext.status == \"no_tp\" && $aspFinder.dob != null || $aspFinder.height != null || $aspFinder.weight != null || $aspFinder.impedIsPresent != null || $aspFinder.goal != null || $aspFinder.timesAWeek != null || $aspFinder.duration != null || $aspFinder.location != null || $aspFinder.level != null || $userContext.height != null] evaluated to FALSE.\norg.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'weight' cannot be found on null\n"
}
]
},
"context": {
"system": {
"dialog_stack": [
{
"dialog_node": "node_2_1495711348295"
}
],
"dialog_request_counter": 5,
"dialog_turn_counter": 5,
"_node_output_map": {
"node_3_1495711396477": [
0
],
"node_1_1495037449418": [
0,
0,
1
],
"node_12_1494424156763": [
0,
3,
0,
1,
2,
4
],
"in exercise": [
0
],
"Workout Feedback": [
0,
2,
1,
0,
3
],
"Greetings": [
0
],
"node_2_1495711348295": [
0,
2,
1,
0
]
}
},
"repeat": false,
"lastMsg": [
"Great job!",
"We're done for today training! Great job :)"
],
"conversation_id": "919d2d6b-e085-4fbe-bd12-19486aa0394e",
"last": {
"trainingContext": {
"status": "no_workout",
"suggested_workout_id": "W-fb2038d7d45a48a6af844a53f52a1759-T-74e57d317b9649feb5f024c049766fd7-F-technogym"
},
"notify_ui": "workout_closed",
"output": {
"nodes_visited": [
"node_9_1494423774989",
"node_1_1494427286758",
"node_12_1494424156763"
],
"text": [
"Great job!",
"We're done for today training! Great job :)"
],
"log_messages": []
}
},
"_currentSession": {
"current_exe": 7
},
"currentSession": {
"feedback": 5
},
"_timestamp": "2017-07-04T21:28:46.561Z",
"trainingContext": {
"status": "no_workout",
"suggested_workout_id": "W-fb2038d7d45a48a6af844a53f52a1759-T-74e57d317b9649feb5f024c049766fd7-F-technogym"
},
"userContext": {
"height": 160,
"userID": "e37e39f4-61ba-41da-a356-2c6cbfae8392",
"lastName": "#lastName",
"facilityId": "4419597c-b63e-4358-b8ba-cc388bb192c3",
"weight": 56,
"userToken": "MjAxNzA3MDQyMTI4MzR8MzQyYzJkZmEyNGVkNGZlZDhiOWU5NDg0NWE1YzAxNTJ8ZWMxZDM4ZDdkMzU5NDhkMGE2MGNkOGMwYjhmYjlkZjl8MXxXLiBFdXJvcGUgU3RhbmRhcmQgVGltZXxpdC1JVHxlMzdlMzlmNDYxYmE0MWRhYTM1NjJjNmNiZmFlODM5Mnx8fHwxfDF8MHwxMDB8fHw1OHw2ODk0fDB8Y29tLm15d2VsbG5lc3M1.BC8BBB1E2066444DEA5F5E1F42627CF6443F3077",
"firstName": "#firstName",
"dob": "03/07/1986"
}
}
}
}
我感兴趣的json的值是数组字段APIresponse.outout.nodes_visited中的最后一个。更具体地说,我希望有一个结构,我可以获得数组字段APIresponse.outout.nodes_visited的每个最后一个值的计数以及与该计数相关联的值(计数名称)。
我通常使用选择器来查询Cloudant,但似乎它确实提供了一种获取我正在寻找的特定计数的方法。
我试图在设计文档中创建一个map reduce函数,但我一直无法成功。
然后问题是:
感谢您的帮助,
菲利普
答案 0 :(得分:1)
您可以使用map-reduce视图执行此操作。首先,使用值为1的数组中的字段键入您的发射:
function(doc) {
if (doc && doc.APIresponse &&
doc.APIresponse.output && doc.APIresponse.output.nodes_visited){
var arr = doc.APIresponse.output.nodes_visited;
emit(arr[arr.length-1], 1);
}
}
并选择内置的_count
reduce函数。
您现在应该能够看到所有访问过的节点,总计使用类似
的内容curl 'https://USER:PASS@ACCOUNT.cloudant.com/DB/_design/DDOC/_view/VIEWNAME?group=true'
您可以在我的某个数据库中看到您的示例数据:
https://skruger.cloudant.com/testdb - 它可供阅读。
我把上面建议的视图函数放在了,这里是查询:
curl 'https://skruger.cloudant.com/testdb/_design/ddoc/_view/nodes?group=true'
{"rows":[
{"key":"ask exercise feedback","value":1},
{"key":"Assign workout","value":2},
{"key":"Capabilities","value":1},
{"key":"Feedback_tester","value":1},
{"key":"First Exercise","value":2},
{"key":"mark exercise done","value":1},
{"key":"no_tp","value":8},
{"key":"node_1_1495716162431","value":10},
{"key":"node_2_1495711348295","value":1},
{"key":"node_21_1494245522737","value":1},
{"key":"node_26_1494249967653","value":1},
{"key":"node_3_1495716340965","value":2},
{"key":"node_3_1495922533369","value":2},
{"key":"node_4_1494233636522","value":2},
{"key":"node_6_1493835340035","value":1},
{"key":"node_7_1493835487968","value":1},
{"key":"node_8_1492030091650","value":1},
{"key":"node_8_1494235961152","value":2},
{"key":"node_9_1495711958382","value":1},
{"key":"pr exit service fail","value":7},
{"key":"profiling - ending","value":8},
{"key":"profiling - question duration","value":7},
{"key":"profiling - question goal","value":7},
{"key":"profiling - question impediments","value":7},
{"key":"profiling - question level","value":7},
{"key":"profiling - question location","value":7},
{"key":"profiling - question timesWeek","value":7},
{"key":"show next exercise","value":2}
]}