在ElasticSearch中一起使用geo_polygon和geo_bounding_box过滤器

时间:2017-01-16 05:06:54

标签: elasticsearch

我是ElasticSearch的新手,并尝试在搜索中同时使用geo_bounding_box和geo_polygon过滤器。如果我一个一个地单独使用它们,它们就能工作。但如果我尝试使用它们,它会给我错误。这是我的代码。

$json = [
        'query' => [
            'bool' => [
                'must_not' => [
                    ['terms' => ['_id' => []]],
                    ['terms' => ['rarity' => []]]
                ],
                'must' => [
                    'range' => [
                        'disappearTime' => [
                            'gte' => 'now',
                            'lte' => 'now+1d'
                        ]
                    ]
                ],
                'filter' => [
                    'geo_bounding_box' => [
                        'location' => [
                            'top_left' => [
                                'lat' => 52.280577919216356,
                                'lon' => -113.78533601760866
                            ],
                            'bottom_right' => [
                                'lat' => 52.26306210545918,
                                'lon' => -113.81855249404909
                            ]
                        ]
                    ],
                    'geo_polygon' => [
                        'location' => [
                            "points" => [
                                [-113.78721646175813, 52.29637194474555],
                                [-113.76335508934484, 52.281770664368565],
                                [-113.76335508934484, 52.26112133563143],
                                [-113.78721646175813, 52.24652005525444],
                                [-113.82096153824187, 52.24652005525444],
                                [-113.84482291065517, 52.26112133563143],
                                [-113.84482291065517, 52.281770664368565],
                                [-113.82096153824187, 52.29637194474555],
                                [-113.78721646175813, 52.29637194474555],
                                [-113.69997059121626, 52.298658944745554],
                                [-113.67610798767082, 52.28405766436857],
                                [-113.67610798767082, 52.26340833563143],
                                [-113.69997059121626, 52.248807055254446],
                                [-113.73371740878373, 52.248807055254446],
                                [-113.75758001232917, 52.26340833563143],
                                [-113.75758001232917, 52.28405766436857],
                                [-113.73371740878373, 52.298658944745554],
                                [-113.69997059121626, 52.298658944745554]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ];

非常感谢任何帮助。 感谢

1 个答案:

答案 0 :(得分:0)

您只需将每个地理过滤器放在自己的PHP数组中,就像使用terms子句中的bool/must查询一样

$json = [
    'query' => [
        'bool' => [
            'must_not' => [
                ['terms' => ['_id' => []]],
                ['terms' => ['rarity' => []]]
            ],
            'must' => [
                'range' => [
                    'disappearTime' => [
                        'gte' => 'now',
                        'lte' => 'now+1d'
                    ]
                ]
            ],
            'filter' => [
              [
                'geo_bounding_box' => [
                    'location' => [
                        'top_left' => [
                            'lat' => 52.280577919216356,
                            'lon' => -113.78533601760866
                        ],
                        'bottom_right' => [
                            'lat' => 52.26306210545918,
                            'lon' => -113.81855249404909
                        ]
                    ]
                ]
              ],
              [
                'geo_polygon' => [
                    'location' => [
                        "points" => [
                            [-113.78721646175813, 52.29637194474555],
                            [-113.76335508934484, 52.281770664368565],
                            [-113.76335508934484, 52.26112133563143],
                            [-113.78721646175813, 52.24652005525444],
                            [-113.82096153824187, 52.24652005525444],
                            [-113.84482291065517, 52.26112133563143],
                            [-113.84482291065517, 52.281770664368565],
                            [-113.82096153824187, 52.29637194474555],
                            [-113.78721646175813, 52.29637194474555],
                            [-113.69997059121626, 52.298658944745554],
                            [-113.67610798767082, 52.28405766436857],
                            [-113.67610798767082, 52.26340833563143],
                            [-113.69997059121626, 52.248807055254446],
                            [-113.73371740878373, 52.248807055254446],
                            [-113.75758001232917, 52.26340833563143],
                            [-113.75758001232917, 52.28405766436857],
                            [-113.73371740878373, 52.298658944745554],
                            [-113.69997059121626, 52.298658944745554]
                        ]
                    ]
                ]
              ]
            ]
        ]
    ]
];