Searchkick 仅 return 某些值,并非全部
Searchkick only return certain values, not all
当使用 searchkick 执行搜索时,我得到了所有字段。我想将响应限制为仅输出某些字段,例如 title
.
# movie.rb
class Movie < ApplicationRecord
searchkick autocomplete: ['title']
end
# search_controller.rb
def autocomplete
render json: Movie.search(params[:query], autocomplete: true, limit: 10, fields: [:title])
end
我的回复还是
[{"id":1,"title":"Megamind","director":"Tom McGrath","genre":"Animation | Action | Comedy","description":"The supervillain Megamind finally defeats his nemesis, the superhero Metro Man. But without a hero, he loses all purpose and must find new meaning to his life.","length":95,"year":2010,"imdb_id":"tt1001526","imdb_rating":"7.3","created_at":"2016-04-16T19:50:24.893Z","updated_at":"2016-04-16T19:50:24.893Z"}]
我只想找回标题。阅读不同的问题,我认为它应该与我的搜索中的 fields
有关,但这并没有改变响应。我也尝试使用 _source
属性,但没有成功。
所以基本上我找到了解决方法。当我在回复中返回所有字段时,我只是在将它们发送给客户端之前过滤它们:
search = Movie.search(params[:query], autocomplete: true, limit: 10, fields: [:title])
search = search.results.map do |m|
{ title: m.title }
end
render json: search
我的查询 return 包含所有值,但我只是发回了我想要的值。只查询 return 我想要的字段会更好,但这现在可以作为解决方案:)
您可以使用:
select_v2: ["title"], load: false
对于 Elasticsearch 版本 5.6.3
和 searchkick gem 版本 2.1.1
这有效:select: ["languages", "categories", "level", "id"], load: false
到 return 只有这些字段
当使用 searchkick 执行搜索时,我得到了所有字段。我想将响应限制为仅输出某些字段,例如 title
.
# movie.rb
class Movie < ApplicationRecord
searchkick autocomplete: ['title']
end
# search_controller.rb
def autocomplete
render json: Movie.search(params[:query], autocomplete: true, limit: 10, fields: [:title])
end
我的回复还是
[{"id":1,"title":"Megamind","director":"Tom McGrath","genre":"Animation | Action | Comedy","description":"The supervillain Megamind finally defeats his nemesis, the superhero Metro Man. But without a hero, he loses all purpose and must find new meaning to his life.","length":95,"year":2010,"imdb_id":"tt1001526","imdb_rating":"7.3","created_at":"2016-04-16T19:50:24.893Z","updated_at":"2016-04-16T19:50:24.893Z"}]
我只想找回标题。阅读不同的问题,我认为它应该与我的搜索中的 fields
有关,但这并没有改变响应。我也尝试使用 _source
属性,但没有成功。
所以基本上我找到了解决方法。当我在回复中返回所有字段时,我只是在将它们发送给客户端之前过滤它们:
search = Movie.search(params[:query], autocomplete: true, limit: 10, fields: [:title])
search = search.results.map do |m|
{ title: m.title }
end
render json: search
我的查询 return 包含所有值,但我只是发回了我想要的值。只查询 return 我想要的字段会更好,但这现在可以作为解决方案:)
您可以使用:
select_v2: ["title"], load: false
对于 Elasticsearch 版本 5.6.3
和 searchkick gem 版本 2.1.1
这有效:select: ["languages", "categories", "level", "id"], load: false
到 return 只有这些字段