简体   繁体   English

无法使用pg_search gem从Postgres中摘录

[英]Unable to retrieve excerpt from Postgres using pg_search gem

Update 更新资料

First of all, there is no method "context". 首先,没有方法“上下文”。 That was a word my brain made up at some point and stuck with. 那是我的大脑在某个时候下定决心并坚持的一个词。 Obviously I should've been running .excerpt(). 显然我应该一直在运行.excerpt()。 Second, I was running the command against the returned array, not an individual instance of the PG_Search::Document 其次,我正在针对返回的数组而不是PG_Search :: Document的单个实例运行命令

Double mistakes, but yes the code does in fact work. 双重错误,但实际上代码确实有效。

End Update 结束更新

First some system info: 首先一些系统信息:

  • Ruby 1.9.3p194 红宝石1.9.3p194
  • Rails 3.2.13 导轨3.2.13
  • pg_search 0.5.7 pg_search 0.5.7
  • Postgres 9.2.3 (with unaccent enabled) Postgres 9.2.3(启用不重音)

I'm trying to follow the progress made in this thread: ( How to show excerpts from pg-search multisearch results ) 我正在尝试跟踪此线程中取得的进展:( 如何显示pg-search multisearch结果的摘录

Okay so assuming the use of the following query cribbed from that post: 好的,假设使用该帖子中的以下查询:

@query = params[:query]
PgSearch.multisearch(@query).select("ts_headline(pg_search_documents.content, plainto_tsquery('english', ''' ' || unaccent('#{@query}') || ' ''' || ':*')) AS excerpt")

returns: 返回:

=> [#<PgSearch::Document id: 7, content: "1 <p>You think water moves fast? You should see ice...", searchable_id: 2, searchable_type: "Release", created_at: "2013-03-27 18:58:52", updated_at: "2013-03-27 18:58:52">]

It successfully returns some search results but they don't have the context method at all. 它成功返回了一些搜索结果,但根本没有context方法。 It's as if I just called multisearch without the select method. 就像我只是在没有select方法的情况下调用multisearch

I'm a newbie when it comes to SQL and Postgres so I'm not exactly sure where to start in debugging that snippet. 我是SQL和Postgres的新手,所以我不确定在哪里调试该代码片段。 I would love some help debugging or getting an explanation of what is happening. 我希望获得一些帮助,以进行调试或对正在发生的事情进行解释。

Also, an aside that I think is important, I want to thank anyone who works on pg_search or responds to questions like these. 另外,我认为很重要的一点是,我要感谢从事pg_search或回答此类问题的任何人。 You make the world a better place. 您使世界变得更美好。

You have to select the other columns you need as well. 您还必须选择其他列。

For example 例如

sanitized = ActionController::Base.helpers.sanitize(params[:q])
@results = PgSearch.multisearch(params[:q])
                  .select(:id, :content, :searchable_id, :searchable_type)
                  .select(["ts_headline(pg_search_documents.content, plainto_tsquery('english', ''' ' || '#{sanitized}' || ' ''' || ':*')) AS excerpt"])

Will return the id, content, searchable_id, searchable_type, excerpt 将返回ID,内容,searchable_id,searchable_type,摘录

Also notice the sanitizing. 还要注意消毒。 Don't want to suffer from an sql injection attack. 不想遭受SQL注入攻击。 :] :]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM