Rules scripting export to ElasticSearch

I have…

I’m submitting a…

  • [ ] Regression (a behavior that stopped working in a new release)
  • [ ] Bug report
  • [ ] Performance issue
  • [X] Documentation issue or request

Current behavior

Using rules (scripts) with getReferences i was wondering whether it is possible to enclose the result within an “iv” object, the reasoning behind this is because some articles we are obtaining via the content API and others via ElasticSearch and the export to elasticsearch changes the structure of the article.
We have tried explicitly tryied adding the “iv” object to the "var jsonObject = " at the start, but to no avail.
Do you happen to have any thoughts on this?

Expected behavior

        "Structure": {
          "iv": [
          {
            "id": "e0149afe-1ab0-46c7-a550-f494588767e1",
            "title": "TEST STRUTTURA SUPERIORE",
            "summary": "TEST",
            "slug": "test-struttura-superiore"
          }
         ]
        }

Minimal reproduction of the problem

Environment

App Name:

  • [X] Self hosted with docker
  • [ ] Self hosted with IIS
  • [ ] Self hosted with other version
  • [] Cloud version

Version: 7.3.0.0

Browser:

  • [ ] Chrome (desktop)
  • [ ] Chrome (Android)
  • [ ] Chrome (iOS)
  • [ ] Firefox
  • [ ] Safari (desktop)
  • [ ] Safari (iOS)
  • [ ] IE
  • [ ] Edge

Others:

Can you give me an example, what you want to achieve?

hi Sebastian,
Here’s an example:

var jsonObject = {
   id:event.id,
   created: event.created,
   lastModified: event.lastModified,
   status: event.status,
   schemaName: event.schemaId.name,
   data: {
      title: event.data.titolo,
      summary: event.data.breve,
      slug: event.data.slug,
      image: event.data.immagine,
      audio: event.data.audio,
      strutturaSuperiore:[]
   }
};

getReferences(event.data.strutturaSuperiore.iv, function(strutture){
   for(var struttura of strutture){
      jsonObject.data.structure.push({
        id: struttura.id,
        titolo: struttura.data.titolo.iv,
        breve: struttura.data.breve.iv,
        slug: struttura.data.slug.iv,
        immagine: struttura.data.immagine.iv,
      })
   }
   complete(JSON.stringify(jsonObject));        
});`

output:

"strutturaSuperiore": [{
  "id": "e0149afe-1ab0-46c7-a550-f494588767e1",
  "titolo": "TEST STRUTTURA SUPERIORE",
  "breve": "TEST",
  "slug": "test-struttura-superiore"
}]

What I would like:

"strutturaSuperiore": {
   "iv": [{
      "id": "e0149afe-1ab0-46c7-a550-f494588767e1",
      "title": "TEST STRUTTURA SUPERIORE",
      "summary": "TEST",
      "slug": "test-struttura-superiore"
   }
]}

Hi Sebastian… I’ve managed to figure it out!!!

Could you fix the formatting of your previous post and post the solution? Could be helpful for others.

The solution was to insert the iv variable into the initial structure.

var jsonObject = {
   id:event.id,
   created: event.created,
   lastModified: event.lastModified,
   status: event.status,
   schemaName: event.schemaId.name,
   data: {
      title: event.data.titolo,
      summary: event.data.breve,
      slug: event.data.slug,
      image: event.data.immagine,
      audio: event.data.audio,
      strutturaSuperiore: { iv: [] },
   }
};

Then push to internal object 

getReferences(event.data.strutturaSuperiore.iv, function(strutture){
   for(var struttura of strutture){
      jsonObject.data.structure.iv.push({
        id: struttura.id,
        titolo: struttura.data.titolo.iv,
        breve: struttura.data.breve.iv,
        slug: struttura.data.slug.iv,
        immagine: struttura.data.immagine.iv,
      })
   }
   complete(JSON.stringify(jsonObject));        
});
1 Like

Yes, exactly. it is just javascript, nothing special.