Algolia按嵌套属性JavaScript过滤

时间:2015-11-25 07:43:28

标签: javascript json search indexing algolia

所以我使用Algolia.com为用户编制索引,以便快速搜索。

索引中的示例对象:

{
    id: 1,
    username: "john.doe",
    name: "John Doe",
    attributes: {
        gender: "male",
        hair_color: "blonde",
        eye_color: "blue",
        height: 165
    }
}

我想通过属性对象中的特定属性(对象键)过滤结果。

我想也许使用facetFilters可以完成这项工作,但我无法让它发挥作用。

我尝试了很多此代码的差异:

user_index.search('', {
    facets: '*',
    facetFilters: ['attributes.hair_color:blonde', 'attributes.eye_color:blue']
}, function(error, data) {
    console.log(error, data);
});

- / -

user_index.search('', {
    facets: '*',
    facetFilters: ['hair_color:blonde']
}, function(error, data) {
    console.log(error, data);
});

请在此处找到文档:https://www.algolia.com/doc/javascript

2 个答案:

答案 0 :(得分:3)

听起来像使用分面过滤器是实现您所寻找的最佳方式。最简单的方法是处理这些过滤器可能是使用Javascript Helper https://github.com/algolia/algoliasearch-helper-js#filtering-results

然后你只需要打电话

helper.addFacetRefinement('hair_color', 'blonde').search();

答案 1 :(得分:1)

只是为了能够将这个问题标记为已回答:

您应该将attributes.hair_color添加到attributesForFaceting(索引信息中心中的“显示”标签) 你应该能够轻松地做到

user_index.search('', {
  facetFilters: 'attributes.hair_color:blonde'
}, function () {
  console.log(arguments);
});

进行搜索。