Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Fantastich ElasticSearch: add numeric meta key

View gist:6129265
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<?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;
View gist:6129265
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
["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)
}
}
}
Owner

This does index the value but it doesn't return the facet correctly.

["meta_price"] => array(2) {
[10]=> int(1)
[2]=> int(1)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.