Skip to content

Commit

Permalink
Merge pull request #108 from 10up/feature/match-all
Browse files Browse the repository at this point in the history
Add helper function to allow elasticpress with other conditions
  • Loading branch information
mattonomics committed Sep 12, 2014
2 parents 363a0e7 + 3226441 commit 49d7d21
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
27 changes: 26 additions & 1 deletion classes/class-ep-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,11 @@ public function format_args( $args ) {
);
}*/

if ( isset( $args['s'] ) ) {
if ( isset( $args['s'] ) && ! isset( $args['ep_match_all'] ) ) {
$query['bool']['must']['fuzzy_like_this']['like_text'] = $args['s'];
$formatted_args['query'] = $query;
} else if ( isset( $args['ep_match_all'] ) && true === $args['ep_match_all'] ) {
$formatted_args['query']['match_all'] = array();
}

if ( isset( $args['post_type'] ) ) {
Expand Down Expand Up @@ -835,6 +837,25 @@ public function format_args( $args ) {
public function get_sites() {
return apply_filters( 'ep_indexable_sites', wp_get_sites() );
}

/**
* Check to see if we should allow elasticpress to override this query
*
* @param $query
*
* @return bool
*/
public function elasticpress_enabled( $query ) {
$enabled = false;

if ( $query->is_search() ) {
$enabled = true;
} else if ( isset( $query->query['ep_match_all'] ) && true === $query->query['ep_match_all'] ) {
$enabled = true;
}

return $enabled;
}
}

EP_API::factory();
Expand Down Expand Up @@ -893,4 +914,8 @@ function ep_prepare_post( $post_id ) {

function ep_get_sites() {
return EP_API::factory()->get_sites();
}

function ep_elasticpress_enabled( $query ) {
return EP_API::factory()->elasticpress_enabled( $query );
}
9 changes: 5 additions & 4 deletions classes/class-ep-wp-query-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function setup() {
}

public function action_pre_get_posts( $query ) {
if ( ! $query->is_search() )
if ( ! ep_elasticpress_enabled( $query ) ) {
return;
}

$query->set( 'cache_results', false );
}
Expand Down Expand Up @@ -73,7 +74,7 @@ public function action_loop_end() {
*/
public function filter_the_posts( $posts, &$query ) {

if ( ! $query->is_search() ) {
if ( ! ep_elasticpress_enabled( $query ) ) {
return $posts;
}

Expand Down Expand Up @@ -138,7 +139,7 @@ public function filter_the_posts( $posts, &$query ) {
* @return string
*/
public function filter_found_posts_query( $sql, $query ) {
if ( ! $query->is_search() ) {
if ( ! ep_elasticpress_enabled( $query ) ) {
return $sql;
}

Expand All @@ -154,7 +155,7 @@ public function filter_found_posts_query( $sql, $query ) {
* @return string
*/
public function filter_posts_request( $request, $query ) {
if ( ! $query->is_search() ) {
if ( ! ep_elasticpress_enabled( $query ) ) {
return $request;
}

Expand Down

0 comments on commit 49d7d21

Please sign in to comment.