【问题标题】:Algolia filter by nested attribute JavaScriptAlgolia 按嵌套属性 JavaScript 过滤
【发布时间】:2016-02-27 21:57:18
【问题描述】:

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

【问题讨论】:

  • 您是否已将attributes.hair_color 添加到您的attributesForFaceting(在您的索引仪表板中显示选项卡)?您应该可以轻松地通过 user_index.search('', { facetFilters: 'attributes.hair_color:blonde' }, function () { console.log(arguments); }) 进行搜索。
  • @Jerska 谢谢!这解决了我的问题!这么简单的事情。
  • @LeviCole Algolia 的搜索 API没有什么简单的。它被疯狂地过度设计,使得执行最简单的搜索变得异常困难。

标签: javascript json search indexing algolia


【解决方案1】:

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

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

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

搜索。

【讨论】:

    【解决方案2】:

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

    你只需要打电话

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

    【讨论】:

    • 感谢您的帮助!不幸的是,这实际上并没有帮助,但很高兴知道未来!
    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    相关资源
    最近更新 更多