【问题标题】:Algolia - Search with a condition to look into an array of stringAlgolia - 使用条件搜索以查看字符串数组
【发布时间】:2016-01-11 00:41:29
【问题描述】:

我正在使用带有 mongoid 数据存储的 rails 和 algolia gem。

我正在向 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 实现 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添加条件来过滤掉结果。

【问题讨论】:

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


    【解决方案1】:

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

    您首先需要在索引设置中将属性asked_to 声明为分面属性,然后通过facetFiltersquery 参数在查询中将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());
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      相关资源
      最近更新 更多