2017-01-18 11 views
0

私はWordpress内でAlgolia検索を使用しています。ユーザーが数値範囲に基づいて結果をフィルタリングできるようにするファセットを作成しようとしています。問題は、比較のために多数の属性の合計を取得する必要があることです。例えばAlgolia for Wordpress:フィルターファセットウィジェットの作成で、複数の属性から値を合計するにはどうすればよいですか?

、のは、私は次のようなデータを持っているとしましょう:

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

はどのようにして最大のスライダーに分を使用し、またはminとmaxのための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」の合計を返す必要があります。私はこの必要

:価格instantsearch.js何かアドバイスをいただければ幸いです:)

答えて

6

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

は、ユーザーインターフェースについてウィジェット

の範囲は、おそらく使用したいと思うでしょう次のinstantsearch.jsウィジェットの1つまたは2つ:

Algoliaは、問合せ時にスピードを確保するために、インデックスの時にたくさんのことを計算し

フィルタリングするためのデータを準備することは、可能な限り良いです。

3つの異なる属性に対して一意のフィルタを使用できるようにするには、アプリケーションレベルで計算を行い、その計算結果をレコードの新しい属性部分として送信する必要があります。カスタムを送信する方法

は、ここでのWordPress

ためAlgoliaプラグインで属性の新しい属性として合計を押す助け、またそれがファセットのための読書になりますいくつかのコードです:

<?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

関連する問題