2017-01-18 22 views
0

我在Wordpress中使用Algolia搜索,我試圖創建一個方面,允許用戶根據數字範圍篩選結果。問題是我需要獲得許多屬性的總和才能進行比較。Algolia for Wordpress:在創建過濾器構面小部件時,如何彙總來自多個屬性的值?

舉例來說,假設我有以下數據:

{ 
    "paid_staff_male": 24, 
    "paid_staff_female": 21, 
    "paid_staff_other": 2 
} 

如何創建一個小插件,它允許用戶要麼使用一個最小到最大滑塊,或者兩個輸入的最小值和最大值,以根據總付費員工篩選結果?

所以,在上面的例子中,這個職位有47個總支付員工。我怎麼會產生一個小/過濾器看起來是這樣的:

Paid Staff 
0 <--[40]---------[500]--------------> 1000 

...或者,這樣的:

Paid Staff 
    Min  Max 
[__40___] - [__500__] 

這是我目前面怎麼看我的instantsearch.js文件:

search.addWidget(
    instantsearch.widgets.menu({ 
     container: '#some-facet', 
     attributeName: 'some_attribute', 
     sortBy: ['isRefined:desc', 'count:desc', 'name:asc'], 
     templates: { 
      header: '<h3 class="widgettitle">Facet Title</h3>' 
     } 
    }) 
); 

在「attributeName」下,我需要返回「paid_staff_male」,「paid_staff_female」和「paid_staff_other」的總和。

我需要這樣的:

attributeName: sum('paid_staff_male', "paid_staff_female", "paid_staff_other"), 

任何意見,將不勝感激:)

回答

6

instantsearch.js價格範圍部件

關於用戶界面,你可能會想使用一個或以下的2 instantsearch.js窗口小部件:

用於過濾

Algolia計算很多東西在索引時間,以確保在查詢時的速度準備數據是儘可能好。

在你的情況下,能夠有一個獨特的過濾器3個不同的屬性,你應該在你的應用層面做了計算和發送計算作爲記錄的一個新的屬性部分的結果。與Algolia WordPress插件

這裏

如何發送自定義屬性是一些代碼,這將有助於你推的總和作爲新的屬性,也使得讀數刻面:

<?php 
/** 
* Compute and push the sum as part of the post records. 
* 
* @param array $shared_attributes 
* @param WP_Post $post 
* 
* @return array 
*/ 
function custom_shared_attributes(array $shared_attributes, WP_Post $post) { 
    $shared_attributes['paid_staff_sum'] = (float) get_paid_staff_sum($post); 

    return $shared_attributes; 
} 
add_filter('algolia_post_shared_attributes', 'custom_shared_attributes', 10, 2); 
add_filter('algolia_searchable_post_shared_attributes', 'custom_shared_attributes', 10, 2); 

function get_paid_staff_sum(WP_Post $post) { 
    // This is just an example, you should adjust it to match your way 
    // of computing the sum. 
    return get_post_meta($post->ID, 'paid_staff_male', true) 
    + get_post_meta($post->ID, 'paid_staff_female', true) 
    + get_post_meta($post->ID, 'paid_staff_other', true); 
} 

/** 
* Make sure you can facet on the newly available attribute. 
* 
* @param array $settings 
* 
* @return array 
*/ 
function custom_attributes_for_faceting(array $settings) { 

    $settings['attributesForFaceting'][] = 'paid_staff_sum'; 

    return $settings; 
} 

add_filter('algolia_posts_index_settings', 'custom_attributes_for_faceting'); 
add_filter('algolia_searchable_posts_index_settings', 'custom_attributes_for_faceting'); 
+0

非常感謝!這就像一個魅力! –

+0

很高興聽到;) – rayrutjes

相關問題