3

I am using rails and algolia gem with mongoid datastore.

I am sending data to algolia for a model Question. One of the doc example in Algolia system is

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"]

I want to find all those documents, where it meets these two conditions: 1) text contains your name 2) asked_to contains i7683yiq7r8998778346q686

I am using Twitter's typeahead javascript library. And my UI's javascript to implement algolia search is as follows:

<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;
  });

});

So my question is:

This code works perfectly. But how to add condition for asked_to contains i7683yiq7r8998778346q686 in above javascript to filter out result.

4

1 回答 1

4

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());
    }
});
于 2016-01-11T07:55:00.990 回答