Search code examples
phpelasticsearchelasticafoselasticabundle

How to add "runtime_mappings" to the query for FOS/Elastica in PHP?


Need to add the next json query to php code with using FOSElasticaBundle:

"runtime_mappings": {
  "Agreement": {
    "type": "keyword",
    "script": {
      "source": "if(doc['winningBidder.edrpou'].size()>0 && doc['seller'].size()>0)\r\n{\r\nemit(\r\n    doc['seller'].value+\":\"+\r\n    doc['trading.id'].value+\":\"+\r\n    doc['winningBidder.edrpou'].value+\":\"\r\n    )\r\n}"
    }
  }
}

If I'm set this in simple method (\Elastica\Query)->addParam():

->addParam('runtime_mappings', [
    'Agreement' => [
        'type' => 'keyword',
        'script' => [
            'source' => "if(doc['winningBidder.edrpou'].size()>0 && doc['seller'].size()>0)\r\n{\r\nemit(\r\n    doc['seller'].value+\":\"+\r\n    doc['trading.id'].value+\":\"+\r\n    doc['winningBidder.edrpou'].value+\":\"\r\n    )\r\n}"
        ]
    ]
])

Then I get an error when I try to collect the query:

Unknown key for a START_ARRAY in [runtime_mappings].

Solution

  • Alternatively, you can create a query by starting with the "runtime_mappings" array and adding all other parameters after:

    $query = \Elastica\Query::create(['runtime_mappings' => [
        'Agreement' => [
            'type' => 'keyword',
            'script' => [
                'source' => "if(doc['winningBidder.edrpou'].size()>0 && doc['seller'].size()>0)\r\n{\r\nemit(\r\n    doc['seller'].value+\":\"+\r\n    doc['trading.id'].value+\":\"+\r\n    doc['winningBidder.edrpou'].value+\":\"\r\n    )\r\n}"
            ]
        ],
    ]]);
    
    // add other params to query...
    $query->addParam();