如何使用php客户端创建elasticsearch bool查询

时间:2015-02-27 05:23:08

标签: php json elasticsearch

我需要运行一个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
                    )
                )
            )
        );

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

elasticsearch php client documentation中,这就是你需要放置多个匹配的索引值的方法:

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

所以我认为对你的查询来说会是一样的但是用term。就像你在编辑中看到的那样:或者像这样:

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