【问题标题】:How to create an elasticsearch bool query with php client如何使用 php 客户端创建 elasticsearch bool 查询
【发布时间】:2015-04-29 18:03:56
【问题描述】:

我需要运行一个看起来像这样的 ES bool 查询

{
  "aggs": {
    "column1": {
      "terms": {
        "field": "column1.raw",
        "size": 5
      }
    }
  },
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "time": {
                  "gt": "2015-02-19 00:00:00",
                  "lt": "2015-02-20 00:00:00"
                }
              }
            }
          ],
          "should": [
            {
              "term": {
                "column1.raw": "value1"
              }
            },
            {
              "term": {
                "column1.raw": "value2"
              }
            }
          ]
        }
      }
    }
  }
}

我已经完成了应该和现在需要制定条款的部分。由于它多次具有相同的索引值,我该如何为它制定数组?

我现在使用以下 PHP 代码:

        foreach ($terms as $term) {
            $termFIlter = array(
                "term" => array(
                    "column1.raw" => $term
                )
            );
        }

        $options['body']['query'] = array(
            "filtered" => array(
                "filter" => array(
                    "bool" => array(
                        "must" => array(
                            "range" => array(
                                "time" => array(
                                    "gt" => $start,
                                    "lt" => $end
                                )
                            )
                        ),
                        "should" => $termFIlter
                    )
                )
            )
        );

【问题讨论】:

    标签: php json elasticsearch


    【解决方案1】:

    以这个例子为例,请更改字段名称。设置您​​正在使用的字段。

    $options = array(
       'fields' => array('title', 'content', 'profile_id', 'type', 'name', 'description', 'date', 'url'),
       'from' => 0,
       'size' => 10,
       'query' => array(
          ($type ?
              array(
                  'bool' => array(
                      'must' => array(
                          array('term' => array('_all' => $term)),
                          array('term' => array('type' => $type))
                       )
                  )
              ) :
                array('match' => array('_all' => $term))
        )
      )
    );
    

    【讨论】:

    • 但是我需要在 should 部分中多次使用术语 thing 来指定多个值。
    • 您能否指定 $term 变量以及您是如何制定它的?
    • 不,我没有使用任何东西。我希望您已经看到我添加到问题中的代码。
    【解决方案2】:

    elasticsearch php client documentation 中,这是您需要放置多个匹配索引值的处理方式:

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

    所以我认为您的查询与术语相同。所以就像你在编辑中显示的那样或这样:

            $options['body']['query'] = array(
            "filtered" => array(
                "filter" => array(
                    "bool" => array(
                        "must" => array(
                            "range" => array(
                                "time" => array(
                                    "gt" => $start,
                                    "lt" => $end
                                )
                            )
                        ),
                        "should" => [
                            [
                                "term" => [
                                    "column1.raw" => "value1"
                                ]
                            ],
                             [
                                "term" => [
                                    "column1.raw" => "value2"
                                ]
                            ]    
                        ]
                    )
                )
            )
        );
    

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 2016-10-20
      • 1970-01-01
      • 2011-12-12
      • 2015-11-10
      • 2020-08-31
      • 2016-05-20
      • 1970-01-01
      • 2022-12-03
      相关资源
      最近更新 更多