ruby-on-rails-4 - Algolia - 搜索条件以查看字符串数组

标签 ruby-on-rails-4 mongoid typeahead.js algolia

我将 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 包含你的名字 2) asked_to 包含 i7683yiq7r8998778346q686

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

<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作为索引设置中分面的属性,然后传递 asked_to:i7683yiq7r8998778346q686通过 facetFilters 作为查询中的分面过滤器查询参数。

当您的索引设置更改时,您可以更改源以添加 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());
    }
});

关于ruby-on-rails-4 - Algolia - 搜索条件以查看字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712539/

相关文章:

ruby-on-rails - 使用带有 Resque 后台作业的 Controller 方法

ruby-on-rails - 对 ruby​​ HashMap 进行排序

ruby-on-rails - 新数据不会持久保存到 Postgres 上的 Rails 数组列

ruby-on-rails - 替换直到删除所有匹配项

ruby-on-rails - Devise 2.1默认是关闭盐的吗?

javascript - Twitter typeahead 0.1 w/bloodhound - 无法使用 URL 进行预取

javascript - typeahead.js - 显示所有预取数据

ruby-on-rails - 为什么 elem_match 返回 0 个元素?

ruby-on-rails - 获取所有 child 的 child 等等

javascript - 防止对空白 typeahead.js 的 ajax 调用