如何在PHP中为FOS/Elastica的查询添加“runtime_mappings”?

ccgok5k5  于 2023-03-22  发布在  PHP
关注(0)|答案(2)|浏览(141)

需要使用FOSElasticaBundle将下一个json查询添加到php代码中:

"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}"
    }
  }
}

如果我在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}"
        ]
    ]
])

然后,当我尝试收集查询时,我得到一个错误:

Unknown key for a START_ARRAY in [runtime_mappings].
acruukt9

acruukt91#

或者,您可以从“runtime_mappings”数组开始创建查询,然后在以下位置添加所有其他参数:

$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();
vc9ivgsu

vc9ivgsu2#

请尝试使用setParam而不是addParam

->setParam('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}"
        ]
    ]
])

相关问题