Skip to content

Commit

Permalink
Merge pull request #36 from alleyinteractive/es-2.3.3-author-facet
Browse files Browse the repository at this point in the history
Author Facets
  • Loading branch information
timatron authored Sep 22, 2016
2 parents 77b91b2 + 4c7dd3b commit ca77deb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
23 changes: 23 additions & 0 deletions lib/class-sp-wp-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ public static function wp_to_es_args( $args ) {
);

break;

case 'author':
$es_query_args['aggregations'][ $label ] = array(
'terms' => array(
'field' => 'post_author.login',
'size' => $facet['count'],
),
);

break;

}
}

Expand Down Expand Up @@ -447,6 +458,18 @@ public function get_facet_data() {

break;

case 'author':
$user = get_user_by( 'login', $item['key'] );

if ( ! $user ) {
continue 2;
}

$name = $user->display_name;
$query_vars = array( 'author' => $user->ID );

break;

case 'date_histogram':
$timestamp = $item['key'] / 1000;

Expand Down
34 changes: 30 additions & 4 deletions tests/test-faceting.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ function setUp() {

sp_index_flush_data();

$author_a = $this->factory->user->create( array( 'user_login' => 'author_a', 'user_pass' => rand_str(), 'role' => 'author' ) );
$post_author_a_1 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_a, 'post_date' => '2007-01-04 00:00:00' ) );
$post_author_a_2 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_a, 'post_date' => '2007-01-05 00:00:00' ) );

$author_b = $this->factory->user->create( array( 'user_login' => 'author_b', 'user_pass' => rand_str(), 'role' => 'author' ) );
$post_author_b_1 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_b, 'post_date' => '2007-01-03 00:00:00' ) );

$cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
$cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
$cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-c' ) );
Expand Down Expand Up @@ -61,6 +68,7 @@ function test_faceting() {
'facets' => array(
'Tag' => array( 'type' => 'taxonomy', 'taxonomy' => 'post_tag', 'count' => 10 ),
'Post Type' => array( 'type' => 'post_type', 'count' => 10 ),
'Author' => array( 'type' => 'author', 'count' => 10 ),
'Histogram' => array( 'type' => 'date_histogram', 'interval' => 'year', 'count' => 10 ),
),
) );
Expand All @@ -69,6 +77,7 @@ function test_faceting() {
$this->assertNotEmpty( $facets );
$this->assertNotEmpty( $facets['Tag']['buckets'] );
$this->assertNotEmpty( $facets['Post Type']['buckets'] );
$this->assertNotEmpty( $facets['Author']['buckets'] );
$this->assertNotEmpty( $facets['Histogram']['buckets'] );
}

Expand Down Expand Up @@ -100,7 +109,7 @@ function test_parsed_data() {
// Post Type
$this->assertEquals( 'Post', $facet_data['Post Type']['items'][0]['name'] );
$this->assertEquals( 'Page', $facet_data['Post Type']['items'][1]['name'] );
$this->assertEquals( 27, $facet_data['Post Type']['items'][0]['count'] );
$this->assertEquals( 30, $facet_data['Post Type']['items'][0]['count'] );
$this->assertEquals( 7, $facet_data['Post Type']['items'][1]['count'] );
$this->assertEquals( array( 'post_type' => 'post' ), $facet_data['Post Type']['items'][0]['query_vars'] );
$this->assertEquals( array( 'post_type' => 'page' ), $facet_data['Post Type']['items'][1]['query_vars'] );
Expand Down Expand Up @@ -138,7 +147,7 @@ function test_histograms() {
$this->assertEquals( '2008', $facet_data['Year']['items'][1]['name'] );
$this->assertEquals( '2009', $facet_data['Year']['items'][2]['name'] );
$this->assertEquals( '2010', $facet_data['Year']['items'][3]['name'] );
$this->assertEquals( 7, $facet_data['Year']['items'][0]['count'] );
$this->assertEquals( 10, $facet_data['Year']['items'][0]['count'] );
$this->assertEquals( 1, $facet_data['Year']['items'][1]['count'] );
$this->assertEquals( 13, $facet_data['Year']['items'][2]['count'] );
$this->assertEquals( 13, $facet_data['Year']['items'][3]['count'] );
Expand All @@ -148,11 +157,28 @@ function test_histograms() {
$this->assertEquals( array( 'year' => '2010' ), $facet_data['Year']['items'][3]['query_vars'] );

$this->assertEquals( 'January 2007', $facet_data['Month']['items'][0]['name'] );
$this->assertEquals( 7, $facet_data['Month']['items'][0]['count'] );
$this->assertEquals( 10, $facet_data['Month']['items'][0]['count'] );
$this->assertEquals( array( 'year' => '2007', 'monthnum' => 1 ), $facet_data['Month']['items'][0]['query_vars'] );

$this->assertEquals( 'January 1, 2007', $facet_data['Day']['items'][0]['name'] );
$this->assertEquals( 7, $facet_data['Day']['items'][0]['count'] );
$this->assertEquals( array( 'year' => '2007', 'monthnum' => 1, 'day' => 1 ), $facet_data['Day']['items'][0]['query_vars'] );
}
}

function test_author_facets() {
$s = new SP_WP_Search( array(
'post_type' => array( 'post', 'page' ),
'posts_per_page' => 0,
'facets' => array(
'Author' => array( 'type' => 'author', 'count' => 10 ),
),
) );
$facet_data = $s->get_facet_data();

$this->assertEquals( 'author_a', $facet_data['Author']['items'][0]['name'] );
$this->assertEquals( 2, $facet_data['Author']['items'][0]['count'] );

$this->assertEquals( 'author_b', $facet_data['Author']['items'][1]['name'] );
$this->assertEquals( 1, $facet_data['Author']['items'][1]['count'] );
}
}

0 comments on commit ca77deb

Please sign in to comment.