Rails searchkick无法根据查询位置获取结果

时间:2018-11-22 11:41:19

标签: ruby-on-rails-5 searchkick

我在我的应用中的事件模型上使用Searchkick。我正在尝试仅搜索状态为“已发布”的结果。我正在控制台中对此进行测试,到目前为止,我知道有一个带有“ Quiz”的事件。我得到一个结果。我可以查看显示状态的结果:“草稿”。但是,当我尝试将此查询写入结果以确认结果计数时,它只是没有考虑状态:“草稿”并将我带回1个结果。以下是我正在使用的代码以及搜索踢的结果。根据文档,我看不到我要去哪里。

results = Event.search "quiz"

响应:

=> #<Searchkick::Results:0x00007feb0a2d57d8 @klass=Event(id: integer, title: string, content: text, category_id: integer, user_id: integer, created_at: datetime, updated_at: datetime, slug: string, status: integer, featured: boolean, recommended: boolean, excerpt: text, sold_out: boolean, ticketsolve_url: string, meta_title: string, meta_description: text, ticketsolve_event_id: string, featured_video: string, show_video: boolean, author_name: string, expiry: date, manual_seo: boolean, price_details: text, remove_booking_button: boolean, tags: string), @response={"took"=>4, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "skipped"=>0, "failed"=>0}, "hits"=>{"total"=>1, "max_score"=>41.952984, "hits"=>[{"_index"=>"events_development_20181122112141928", "_type"=>"event", "_id"=>"3", "_score"=>41.952984}]}}, @options={:page=>1, :per_page=>10000, :padding=>0, :load=>true, :includes=>nil, :model_includes=>nil, :json=>false, :match_suffix=>"analyzed", :highlight=>nil, :highlighted_fields=>[], :misspellings=>true, :term=>"quiz", :scope_results=>nil, :index_name=>nil, :total_entries=>nil}>

results.total_count等于1,这是正确的。

结果。[0]显示此事件。

响应:

=> #<Event id: 3, title: "It's A Table Quiz! 2018", content: "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n</head>\r\n<body>\r\n...", category_id: 10, user_id: 2, created_at: "2018-09-20 12:57:01", updated_at: "2018-11-07 19:15:29", slug: "it-s-a-table-quiz-2018", status: "draft", featured: false, recommended: false, excerpt: "On October 4th get yourself and three of your smar...", sold_out: false, ticketsolve_url: "https://firstfortnight.ticketsolve.com/shows/87359...", meta_title: "First Fortnight Event Title", meta_description: "First Fortnight Event Description", ticketsolve_event_id: "1111111", featured_video: "", show_video: false, author_name: nil, expiry: "2019-01-18", manual_seo: false, price_details: nil, remove_booking_button: false, tags: nil>

我们可以看到状态为“草稿”。因此,现在我将尝试使用测验进行搜索,并且在状态等于“草稿”的情况下尝试获得1的结果。

results = Event.search "quiz", where: {status: "draft"}

结果计数等于0。

我还尝试了其他状态变化:1或状态::draft,但是这些没有带来任何结果。加上看到上面的结果,我认为正确的搜索是:{status:“ draft”}无论如何。

我将添加模型代码,以防万一我做了一些愚蠢的事情。任何帮助将非常感谢。

型号:

class Event < ApplicationRecord
    extend FriendlyId

    # Search kick with callback so that when data is committed search runs reindex
    searchkick callbacks: :async
    # Controlled data that is indexed. Allows the user to find posts by author.
    def search_data
    {
            title: title,
            stats: status,
            content: content,
            excerpt: excerpt,
            tags: tags,
      author_name: user.name
    }
  end

    enum status: {draft: 0, published: 1}
end

1 个答案:

答案 0 :(得分:0)

再次阅读并检查我没有生气时,我注意到模型中出现了血腥的错字。结果正常运行现在控制台。谢谢。