Last active
February 5, 2016 11:07
-
-
Save markoheijnen/6129265 to your computer and use it in GitHub Desktop.
Fantastich ElasticSearch: add numeric meta key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class My_Elasticsearch { | |
public function __construct() { | |
add_filter( 'elasticsearch_config_facets', array( $this, 'add_facets' ) ); | |
add_filter( 'elasticsearch_config_fields', array( $this, 'add_fields' ) ); | |
add_filter( 'elasticsearch_indexer_map_field', array( $this, 'indexer_map_field' ), 10, 2 ); | |
add_filter( 'elasticsearch_indexer_build_document', array( $this, 'indexer_build_document' ), 10, 2 ); | |
} | |
public function add_facets( $facets ) { | |
$facets[] = 'meta_price'; | |
return $facets; | |
} | |
public function add_fields( $fieldnames ) { | |
$fieldnames[] = 'meta_price'; | |
return $fieldnames; | |
} | |
// not needed since it can be done via settings | |
public function indexer_map_field( $props, $field ) { | |
if( 'meta_price' == $field ) | |
$props['type'] = 'float'; | |
return $props; | |
} | |
public function indexer_build_document( $document, $post ) { | |
if( 'arrangement' == $post->post_type ) { | |
$document['meta_price'] = get_post_meta( $post->ID, '_price', true ); | |
} | |
return $document; | |
} | |
} | |
new My_Elasticsearch; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
["meta_price"]=> | |
array(5) { | |
["_type"]=> | |
string(5) "terms" | |
["missing"]=> | |
int(6) | |
["total"]=> | |
int(2) | |
["other"]=> | |
int(0) | |
["terms"]=> | |
array(2) { | |
[0]=> | |
array(2) { | |
["term"]=> | |
float(10.140000343323) | |
["count"]=> | |
int(1) | |
} | |
[1]=> | |
array(2) { | |
["term"]=> | |
float(2) | |
["count"]=> | |
int(1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does index the value but it doesn't return the facet correctly.
["meta_price"] => array(2) {
[10]=> int(1)
[2]=> int(1)
}