php es領域検索、地図緯度検索、範囲検索

3870 ワード

php概要-es概要-領域検索
1:対角線領域検索
$search_params = array(
    'body' => array(
        'post_filter'=>array(
            'geo_bounding_box'=>array(
                'location'=>array(
                    'top_left'=>array(
                        'lat'=>$params_top_left_lat,
                        'lon'=>$params_top_left_lon
                    ),
                    'bottom_right'=>array(
                        'lat'=>$params_bottom_right_lat,
                        'lon'=>$params_bottom_right_lon
                    )
                )
            )
        )
    ),
    'from' => $offset,
    'size' => $limit,
    '_source' => $source,
);

2:座標で囲まれたポリゴン領域の検索
   $borders = “116.403183,40.067132;116.412095,40.063709;116.414825,40.058463;116.405483,40.059015;116.40268,40.062604;116.397219,40.066469;116.3965,40.067795”;
   foreach($borders as $key=>$v){
        $latLon = explode(",",$v);
        $borderArr[$key]['lon'] = $latLon[0];
        $borderArr[$key]['lat'] = $latLon[1];
    }
//1):       
	$geo_search = [
	    'geo_polygon' => [
	        'location' => [
	            'points'=>$borderArr
	        ]
	    ]
	];
	$search_params['body']['post_filter'] = $geo_search;
//2):          :we_type=1
	$search_params = array(
	    'body' => array(
	        'query' => array(
	            'filtered'=>array(
	                "filter"=>array(
	                    'bool'=>array(
	                        'must'=>array(
	                            array(
	                                'term'=>array(
	                                    'we_type'=>1 
	                                )
	                            ),
	                            array(
	                                'geo_polygon' => [
	                                    'location' => [
	                                        'points'=>$borderArr
	                                    ]
	                                ]
	                            )
	                        )
	                    )
	                )
	            )
	        ),
	        'sort' => array('sort_id' => array('order' => 'desc'))
	    ),
	    'size' => 1000
	);

データフォーマットの例:
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
            	'term':{
            		'we_type':1
				}
              
            },
           {
              "geo_polygon": {
                "location": {
                  "points": [
                    {
                        "lon": "116.425369",
                        "lat": "39.89931"
                    },
                    {
                        "lon": "116.426905",
                        "lat": "39.899359"
                    },
                    {
                        "lon": "116.428441",
                        "lat": "39.899387"
                    },
                    {
                        "lon": "116.429991",
                        "lat": "39.899421"
                    }
                  ]
                }
              }
            }
          ],
          "must_not":{
          	"term":{
          		"platforms":1
          	}
          }
        }
      }
    }
  }
}