Skip to content

Commit

Permalink
Index geo_shape, and rewrite geo_point sync/index to be consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
wxactly committed Dec 12, 2017
1 parent 251232d commit 055f4c6
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions ep-geo.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function ep_geo_setup() {
* @return array
*/
function ep_geo_config_mapping( $mapping ) {
$mapping['mappings']['post']['properties']['pin'] = array(
// Index geo_point:
$mapping['mappings']['post']['properties']['geo_point'] = array(
'properties' => array(
'location' => array(
'type' => 'geo_point',
Expand All @@ -38,6 +39,17 @@ function ep_geo_config_mapping( $mapping ) {
),
);

// Index geo_shape:
$mapping['mappings']['post']['properties']['geo_shape'] = array(
'properties' => array(
'location' => array(
'type' => 'geo_shape',
// https://github.com/elastic/elasticsearch/issues/23747
// 'ignore_malformed' => true,
),
),
);

return $mapping;
}

Expand All @@ -50,14 +62,28 @@ function ep_geo_config_mapping( $mapping ) {
* @return array
*/
function ep_geo_post_sync_args( $post_args, $post_id ) {
// Sync geo_point:
$geo_point = [
'location' => [],
];

if ( isset( $post_args['post_meta']['latitude'][0] ) ) {
$post_args['pin']['location']['lat'] = $post_args['post_meta']['latitude'][0];
$geo_point['location']['lat'] = $post_args['post_meta']['latitude'][0];
}

if ( isset( $post_args['post_meta']['longitude'][0] ) ) {
$post_args['pin']['location']['lon'] = $post_args['post_meta']['longitude'][0];
$geo_point['location']['lon'] = $post_args['post_meta']['longitude'][0];
}

$post_args['geo_point'] = apply_filters( 'ep_geo_post_sync_geo_point', $geo_point, $post_args, $post_id );

// Sync geo_shape:
$geo_shape = [
'location' => [],
];

$post_args['geo_shape'] = apply_filters( 'ep_geo_post_sync_geo_shape', $geo_shape, $post_args, $post_id );

return $post_args;
}

Expand All @@ -78,19 +104,19 @@ function ep_geo_formatted_args( $formatted_args, $args ) {
}

if ( isset( $args['geo_query']['lat'] ) ) {
$geo_distance['pin.location']['lat'] = $args['geo_query']['lat'];
$geo_distance['geo_point.location']['lat'] = $args['geo_query']['lat'];
}

if ( isset( $args['geo_query']['lon'] ) ) {
$geo_distance['pin.location']['lon'] = $args['geo_query']['lon'];
$geo_distance['geo_point.location']['lon'] = $args['geo_query']['lon'];
}

$formatted_args['post_filter']['bool']['filter']['geo_distance'] = $geo_distance;

if ( isset( $args['geo_query']['order'] ) ) {
array_unshift( $formatted_args['sort'], array(
'_geo_distance' => array(
'pin.location' => $geo_distance['pin.location'],
'geo_point.location' => $geo_distance['geo_point.location'],
'order' => $args['geo_query']['order'],
),
) );
Expand Down

0 comments on commit 055f4c6

Please sign in to comment.