簡體   English   中英

Elasticsearch查詢構造中的if else語句

[英]If else statement in elasticsearch query construction

我正在使用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' ] ];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM