Algolia - 搜索条件以查看字符串数组

Algolia - Search with a condition to look into an array of string

我正在使用 rails 和 algolia gem 以及 mongoid 数据存储。

我正在向 algolia 发送模型 Question 的数据。 Algolia 系统中的文档示例之一是

objectID: 5691e056410213a381000000
text: "what is #cool about your name Mr. John? #name #cool"
asked_to: ["565571704102139759000000", "i7683yiq7r8998778346q686", "kjgusa67g87y8e7qtwe87qwe898989"]
asked_by: "564a9b804102132465000000"
created_at: "2016-01-10T04:38:46.201Z"
card_url: "http://localhost:3000/cards/5691e056410213a381000000"
answerers: []
has_answer: false
requestor_count: 0
status: "active"
popularity_point: 0
created_at_i: 1452400726
_tags: ["cool", "name"]

我想找到所有满足这两个条件的文档: 1) text 包含 your name 2) asked_to 包含 i7683yiq7r8998778346q686

我正在使用 Twitter 的 typeahead javascript 库。而我UI的javascript实现algolia搜索如下:

<input class="typeahead ui-widget form-control input-md search-box tt-input" id="typeahead-algolia" placeholder="Search questions" spellcheck="false" type="text" autocomplete="off" dir="auto" style="position: relative; vertical-align: top;">

$(document).on('ready page:load', function () {

  var client = algoliasearch("APPLICATION_ID", "SEARCH_KEY");
  var index = client.initIndex('Question');

  $('#typeahead-algolia').typeahead(
    {
      hint: false,
      highlight: true,
      minLength: 1
    }, 
    {
      source: index.ttAdapter({hitsPerPage: 10}),
      displayKey: 'text'
    }
  ).on('keyup', this, function (event) {
    if (event.keyCode == 13) {
      $('#typeahead-algolia').typeahead('close');
      window.location.href = "/?keyword="+encodeURIComponent($('#typeahead-algolia').val());
    }
  });

  $('.typeahead').bind('typeahead:select', function(ev, suggestion) {
    window.location.href = suggestion.card_url;
  });

});

所以我的问题是:

此代码完美运行。但是如何在上面的javascript中添加asked_to contains i7683yiq7r8998778346q686的条件来过滤掉结果。

您可以在查询中对 asked_to 属性使用分面过滤器。

您首先需要在索引设置中将属性 asked_to 声明为分面属性,然后通过 facetFilters 查询参数将 asked_to:i7683yiq7r8998778346q686 作为查询中的分面过滤器传递.

当您的索引设置更改时,您可以更改源以添加 facetFilters 参数:

$('#typeahead-algolia').typeahead(
    {
        hint: false,
        highlight: true,
        minLength: 1
    }, 
    {
        source: index.ttAdapter({hitsPerPage: 10, facetFilters: "asked_to:i7683yiq7r8998778346q686"}),
        displayKey: 'text'
    }
).on('keyup', this, function (event) {
    if (event.keyCode == 13) {
        $('#typeahead-algolia').typeahead('close');
        window.location.href = "/?keyword="+encodeURIComponent($('#typeahead-algolia').val());
    }
});