我正在使用elasticsearch php并尝试在一处优化查询收缩。典型的Elasticsearch查询,例如:
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'testField' => 'abc' ] ],
[ 'match' => [ 'testField2' => 'xyz' ] ],
]
]
]
]
];
所以问题是,是否可以在“match”字符串之前将条件查询放在$ params中,例如:
<?php if (isset($_GET['query'])) [ 'match' => [ 'testField' => 'abc' ] ]; ?>
谢谢你的任何建议
最佳答案
您可以使用此:
<?php
$must = [[ 'match' => [ 'testField2' => 'xyz' ] ] ];
if (isset($_GET['query']))
$must[] = [ 'match' => [ 'testField' => 'abc' ] ];
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => $must
]
]
]
];
或这个;
<?php
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'testField2' => 'xyz' ] ],
],
]
]
]
];
if (isset($_GET['query']))
$params['body']['query']['bool']['must'][] = [ 'match' => [ 'testField' => 'abc' ] ];
关于php - Elasticsearch查询构造中的if else语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213659/