duanchun1909 2018-09-07 00:16
浏览 80
已采纳

如果是elasticsearch查询构造中的else语句

I'm using elasticsearch php and trying to optimize my query contraction in one place. Typical Elasticsearch query like:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    [ 'match' => [ 'testField' => 'abc' ] ],
                    [ 'match' => [ 'testField2' => 'xyz' ] ],
                ]
            ]
        ]
    ]
];

So the question is, does it possible to put conditional query in $params before 'match' string maybe like:

<?php if (isset($_GET['query'])) [ 'match' => [ 'testField' => 'abc' ] ]; ?>

Thank you in any advice

  • 写回答

1条回答 默认 最新

  • dopt85756 2018-09-07 00:35
    关注

    You can use this:

     <?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
                 ]
             ]
        ]
     ];
    

    or this;

     <?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' ] ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转