使用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
- 属性,但没有运气。
答案 0 :(得分:4)
您可以使用:
function plot_test2()
clear;
savefile = 1;
scrsz = get(0,'ScreenSize');
figure('Position',[1 1 0.9 * scrsz(3) 0.9 * scrsz(4)]);
hold on;
box on;
x=(0:1:10);
y1=x;
y2=x.^2;
[hAx, hLine1, hLine2] = plotyy(x,y1,x,y2);
%Axis label
xlabel(hAx(1),'XLabel', 'FontSize', 20, 'Interpreter', 'latex', 'Color', 'k');
ylabel(hAx(1),'YLabel', 'FontSize', 20, 'Interpreter', 'latex', 'Color', 'k');
ylabel(hAx(2),'Second YLabel', 'FontSize', 20, 'Interpreter', 'latex');
set(hAx,{'ycolor'},{'k';'k'})
set(hAx,{'FontSize'},{20;20}, {'LineWidth'}, {3;3})
set(hLine1,'LineWidth', 3)
set(hLine2,'LineWidth', 3)
set(hLine1,'Color', 'r')
set(hLine2,'Color', 'b')
hl=legend([hLine1 hLine2],{'First Line','Second Line'});
set(hl,'FontSize',15,'Location','Northwest', 'Orientation','Vertical')
%Save pdf
if savefile
% Backup previous settings
prePaperType = get(gcf,'PaperType');
prePaperUnits = get(gcf,'PaperUnits');
preUnits = get(gcf,'Units');
prePaperPosition = get(gcf,'PaperPosition');
prePaperSize = get(gcf,'PaperSize');
% Make changing paper type possible
set(gcf,'PaperType','<custom>');
% Set units to all be the same
set(gcf,'PaperUnits','inches');
set(gcf,'Units','inches');
% Save the pdf
print -dpdf Test.pdf;
% Restore the previous settings
set(gcf,'PaperType',prePaperType);
set(gcf,'PaperUnits',prePaperUnits);
set(gcf,'Units',preUnits);
set(gcf,'PaperPosition',prePaperPosition);
set(gcf,'PaperSize',prePaperSize);
end
答案 1 :(得分:1)
对于Elasticsearch版本5.6.3
和searchkick gem版本2.1.1
来说,此方法有效:select: ["languages", "categories", "level", "id"], load: false
仅返回这些字段
答案 2 :(得分:-1)
所以基本上我找到了解决方法。当我在回复中回到所有字段时,我只是在将它们发送到客户端之前对其进行过滤:
search = Movie.search(params[:query], autocomplete: true, limit: 10, fields: [:title])
search = search.results.map do |m|
{ title: m.title }
end
render json: search
我的查询返回所有值,但我只是发送我想要的那些。让查询返回我想要的字段会更好,但现在这可以作为解决方案:)