Created
October 6, 2014 14:25
-
-
Save rinatkhaziev/73d6c78671ad25e18e64 to your computer and use it in GitHub Desktop.
wpcom_elasticsearch_query_args example nested boolean OR filter
This file contains 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 | |
/** | |
* ES Query args filter | |
*/ | |
add_filter( 'wpcom_elasticsearch_query_args', function( $args, $query ) { | |
// Modify sort query | |
$args['sort'] = array( | |
array( | |
'modified' => array( "order" => 'desc' ), | |
), | |
array( | |
"_score" => array( "order" => 'desc' ) ), | |
); | |
// Account for guest authors, boost the field 5 times, to ensure more results on first page of search if search term is authors name | |
$args['query']['multi_match']['fields'][] = "taxonomy.author.name^5"; | |
// Fine tune 'fields' arg | |
$args['query']['multi_match']['fields'] = array_map( function( $e ) { | |
switch ( $e ) { | |
case 'title': | |
// Boost title | |
$e = 'title^5'; | |
break; | |
case 'author': | |
$e = 'author^5'; | |
default: | |
} | |
return $e; | |
}, $args['query']['multi_match']['fields'] ); | |
/** | |
* Replace default AND filter with boolean nested OR | |
*/ | |
$args['filter'] = array( "or" => | |
/** | |
* Include all posts no older than 5 years if they don't have ooyala shortcode | |
*/ | |
array( | |
array( "and" => array( | |
array( | |
"terms" => array( "post_type" => array( "post", "page" ) ) | |
), | |
array( | |
"range" => array( "date" => array( "gte" => strtotime( "-5 years" ) ) ) | |
), | |
array( | |
"term" => array( "shortcode.ooyala.count" => 0 ) | |
) | |
) | |
), | |
/** | |
* Include posts that are not older than 1 year if a post contains at least one ooyala shortcode | |
*/ | |
array( "and" => array( | |
array( | |
"terms" => array( "post_type" => array( "post", "page" ) ) | |
), | |
array( | |
"range" => array( "date" => array( "gte" => strtotime( "-12 months" ) ) ) | |
), | |
array( | |
"range" => array( "shortcode.ooyala.count" => array( "gte" => 1 ) ) | |
), | |
), | |
), | |
// Any other all clauses should go here | |
) // End of OR filter | |
); | |
return $args; | |
}, 10, 2 ); | |
// End of ElasticSearch integration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment