简体   繁体   English

创建数组结果的 1-1 映射

[英]Creating 1-1 mapping of Array results

I am trying to use data coming back from an ElasticSearch query, and get it into an Array in jsonata in a certain format.我正在尝试使用从 ElasticSearch 查询返回的数据,并以某种格式将其放入 jsonata 中的数组中。

Essentially, I need my result set to be like this:本质上,我需要我的结果集是这样的:

{
  "userName": [
    "david2@david2.com",
    "david2@david2.com",
    "david2@david2.com",
    "david2@david2.com"
  ],
  "label": [
    "Dealer",
    "Inquiry",
    "DD Test Skill1",
    "_11DavidTest"
  ],
  "value": [
    3,
    5,
    2,
    1
  ]
}

However, what I am getting is this:但是,我得到的是:

{
  "userName": "david2@david2.com",
  "label": [
    "Dealer",
    "Inquiry",
    "DD Test Skill1",
    "_11DavidTest"
  ],
  "value": [
    3,
    5,
    2,
    1
  ]
}

I am using the following to map the results:我使用以下 map 结果:

(
    $data := $map(data.hits.hits._source.item."Prod::User", function($v) {
        {

            "userName": $v.userName,
            "label": $v.userSkillLevels.skill.name,
            "value": $v.userSkillLevels.level
            
            
        }
    });

)

And my overall dataset returned form ElasticSearch is as follows:我从 ElasticSearch 返回的整体数据集如下:

{
  "data": {
    "took": 3,
    "timed_out": false,
    "_shards": {
      "total": 15,
      "successful": 15,
      "skipped": 0,
      "failed": 0
    },
    "hits": {
      "total": {
        "value": 2,
        "relation": "eq"
      },
      "max_score": 1.002851,
      "hits": [
        {
          "_index": "items_latest_production1_user",
          "_type": "_doc",
          "_id": "63d000766f67d40a73073d5d_f6144acf2b3ff31209ef9f6d461cd849",
          "_score": 1.002851,
          "_source": {
            "item": {
              "Prod::User": {
                "userSkillLevels": [
                  {
                    "level": 3,
                    "skill": {
                      "name": "Dealer"
                    }
                  },
                  {
                    "level": 5,
                    "skill": {
                      "name": "Inquiry"
                    }
                  },
                  {
                    "level": 2,
                    "skill": {
                      "name": "DD Test Skill1"
                    }
                  },
                  {
                    "level": 1,
                    "skill": {
                      "name": "_11DavidTest"
                    }
                  }
                ],
                "userName": "david2@david2.com"
              }
            }
          }
        }
      ]
    }
  }
}

I can see that each user that comes back from Elastic, then as all the skills/levels in an array associated to 1 username.我可以看到每个从 Elastic 返回的用户,然后是与 1 个用户名关联的数组中的所有技能/级别。

I need to have the same number of userNames, as their are skills... and am struggling to get it just right.我需要拥有相同数量的用户名,因为他们的技能......并且正在努力让它恰到好处。

Thoughts?想法? Appreciate any help, I'm sure I'm overlooking something simple.感谢任何帮助,我确定我忽略了一些简单的事情。

Actually, need this format:实际上,需要这种格式:

[
{
"userName" : "david2@david2.com"
"label" : "Dealer"
"value" : 3
},
{
"userName" : "david2@david2.com"
"label" : "Inquiry"
"value" : 5
},
{
"userName" : "david2@david2.com"
"label" : "DD Test Skill"
"value" : 2
},
{
"userName" : "david2@david2.com"
"label" : "_11DavidTest"
"value" : 1
}
]

One way you could solve this is to create the userName array yourself and making sure it has the same length as userSkillLevels.skill , here's how you can do this:解决此问题的一种方法是自己创建userName数组并确保它的长度与userSkillLevels.skill相同,这是您可以执行此操作的方法:

(
    $data := $map(data.hits.hits._source.item."Prod::User", function($v, $i, $a) {
        {

            "userName": $map([1..$count($v.userSkillLevels.skill)], function() {$v.userName}),
            "label": $v.userSkillLevels.skill.name,
            "value": $v.userSkillLevels.level
        }
    });
)

Feel free to check out this response in Stedi JSONata Playground: https://stedi.link/PVwGacu请随时在 Stedi JSONata Playground 中查看此回复: https://stedi.link/PVwGacu

Actually, need this format:实际上,需要这种格式:

For the updated format from your question, this solution can produce it using the parent operator :对于您问题的更新格式,此解决方案可以使用父运算符生成它:

data.hits.hits._source.item."Prod::User".userSkillLevels.{
  "userName": %.userName,
  "label": skill.name,
  "value": level
}

Check it out on the playground: https://stedi.link/gexB3Cb在操场上查看: https://stedi.link/gexB3Cb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM