Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use must instead of should for 'or' relations #2997

Merged
merged 6 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,19 @@ protected function parse_date( $args ) {
* @return array
*/
protected function parse_meta_queries( $args ) {
$meta_queries = [];
/**
* 'meta_query' arg support.
*
* Relation supports 'AND' and 'OR'. 'AND' is the default. For each individual query, the
* following 'compare' values are supported: =, !=, EXISTS, NOT EXISTS. '=' is the default.
*
* @since 1.3
*/
$meta_queries = ( ! empty( $args['meta_query'] ) ) ? $args['meta_query'] : [];

/**
* Todo: Support meta_type
*/

/**
* Support `meta_key`, `meta_value`, `meta_value_num`, and `meta_compare` query args
Expand All @@ -2016,32 +2028,18 @@ protected function parse_meta_queries( $args ) {
$meta_query_array['compare'] = $args['meta_compare'];
}

$meta_queries[] = $meta_query_array;
}

/**
* Todo: Support meta_type
*/

/**
* 'meta_query' arg support.
*
* Relation supports 'AND' and 'OR'. 'AND' is the default. For each individual query, the
* following 'compare' values are supported: =, !=, EXISTS, NOT EXISTS. '=' is the default.
*
* @since 1.3
*/
if ( ! empty( $args['meta_query'] ) ) {
$meta_queries = array_merge( $meta_queries, $args['meta_query'] );
if ( ! empty( $meta_queries ) ) {
$meta_queries = [
'relation' => 'AND',
$meta_query_array,
$meta_queries,
];
} else {
$meta_queries = [ $meta_query_array ];
}
}

if ( ! empty( $meta_queries ) ) {

$relation = 'must';
if ( ! empty( $args['meta_query'] ) && ! empty( $args['meta_query']['relation'] ) && 'or' === strtolower( $args['meta_query']['relation'] ) ) {
$relation = 'should';
}

// get meta query filter
$meta_filter = $this->build_meta_query( $meta_queries );

Expand Down
69 changes: 69 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3066,6 +3066,75 @@ public function testMetaQueryOrRelation() {
$this->assertEquals( 2, $query->found_posts );
}

/**
* Test an advanced meta filter query with or relation while sorting by Meta key
*
* @since 4.4.0
* @group post
*/
public function testMetaQueryOrRelationWithSort() {
$this->ep_factory->post->create(
array(
'post_content' => 'the post content findme',
'meta_input' => array( 'test_key' => date('Ymd') - 5 ),
),
);
$this->ep_factory->post->create(
array(
'post_content' => 'the post content findme',
'meta_input' => array(
'test_key' => date('Ymd') + 5,
'test_key2' => date('Ymd') + 6,
),
),
);
$this->ep_factory->post->create(
array(
'post_content' => 'the post content findme',
'meta_input' => array(
'test_key' => date('Ymd') + 5,
'test_key2' => date('Ymd') + 6,
),
),
);

$post = new \ElasticPress\Indexable\Post\Post();
ElasticPress\Elasticsearch::factory()->refresh_indices();
$args = array(
'ep_integrate' => true,
'meta_key' => 'test_key',
'meta_query' => array(
'relation' => 'or',
array(
'key' => 'test_key',
'value' => date('Ymd'),
'compare' => '<=',
'type' => 'NUMERIC',
),
array(
'key' => 'test_key2',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'NUMERIC',
),
),
'orderby' => 'meta_value_num',
'order' => 'ASC',
);

$query = new \WP_Query( $args );
$args = $post->format_args($args, new \WP_Query() );

$outer_must = $args['post_filter']['bool']['must'][0]['bool']['must'];

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 3, $query->post_count );
$this->assertEquals( 3, $query->found_posts );
$this->assertSame( 'meta.test_key', $outer_must[0]['exists']['field'] );
$this->assertArrayHasKey( 'meta.test_key.long', $outer_must[1]['bool']['should']['bool']['should'][0]['bool']['must'][0]['range'] );
$this->assertArrayHasKey( 'meta.test_key2.long', $outer_must[1]['bool']['should']['bool']['should'][1]['bool']['must'][0]['range'] );
}

/**
* Test an advanced meta filter query
*
Expand Down