如何使用php客户端创建弹性搜索布尔查询


How to create an elasticsearch bool query with php client

我需要运行一个ES布尔查询,它看起来像这个

{
  "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
                    )
                )
            )
        );

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

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

在elasticsearch php客户端文档中,这是您处理需要放置多个匹配索引值的方法:

$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"
                            ]
                        ]    
                    ]
                )
            )
        )
    );