Algolia 按嵌套属性过滤 JavaScript

Algolia filter by nested attribute JavaScript

所以我使用 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

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

您只需要调用

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

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

您应该将 attributes.hair_color 添加到您的 attributesForFaceting(索引仪表板中的“显示”选项卡) 你应该能够轻松做到

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

进行搜索。