轮胎(ruby on rails)将我的搜索参数添加为索引,而不是查询(Tire (ruby on rails) is adding my search parms as the index, not the query)
我正在使用elasticsearch进行系统日志索引,并且想从rails应用程序查询elasticsearch,所以我正在尝试使用轮胎。
如果我从搜索框中搜索asa,结果如下。
[REQUEST FAILED] curl -X GET 'http://localhost:9200/asa/_search?pretty' -d '{}'
注意asa在索引点,如果我从asa更改为_all,我会得到一个搜索。
在index.html.erb上我有这个搜索框
<%= form_tag 'csdashboard', :method => :get do %> <%= text_field_tag :query, params[:query] %> <%= submit_tag 'Search logs..', name: nil %> <% end %>
这是我的控制器。
class CsdashboardController < ApplicationController def index if params[:query].present? @esearch = Tire.search(params[:query]) render 'layouts/search' else render 'layouts/index' end end end
这是我的search.html.erb
<%= @esearch.results.to_json do %> <% end %>
I'm using elasticsearch for syslog indexing, and would like to query elasticsearch from a rails app, so I'm trying to use tire.
Here are the results if I search for asa from my search box.
[REQUEST FAILED] curl -X GET 'http://localhost:9200/asa/_search?pretty' -d '{}'
notice the asa is in the index spot, if I change this from asa to _all, I get a search.
On index.html.erb I have this search box
<%= form_tag 'csdashboard', :method => :get do %> <%= text_field_tag :query, params[:query] %> <%= submit_tag 'Search logs..', name: nil %> <% end %>
Here is my controller.
class CsdashboardController < ApplicationController def index if params[:query].present? @esearch = Tire.search(params[:query]) render 'layouts/search' else render 'layouts/index' end end end
Here is my search.html.erb
<%= @esearch.results.to_json do %> <% end %>
原文:https://stackoverflow.com/questions/19586642
最满意答案
在你的
get
方法中,你调用YAML.load
,但在你的set方法中你使用.to_s
。 这意味着YAML解析器正在尝试读取任意字符串,就好像它是YAML一样。 对于对称性,应该在set
方法中使用YAML.dump
。我已经创建了一个带有更改的拉取请求。
In your
get
method you callYAML.load
, but in your set method you use.to_s
. This means that the YAML parser is trying to read an arbitrary string as if it were YAML. For symmetryYAML.dump
should be used in theset
method instead.I've created a pull request with the changes.
相关问答
更多-
发生这种情况是因为@data["feed"]["entry"]是哈希数组: puts @data["feed"]["entry"].class # => Array 此数组中的每个元素哈希都具有“id”,“category”,“title”等值。 要获取每个标题,请尝试使用以下代码段: @data["feed"]["entry"].each do |entry| puts entry["title"] end # => "TABE test adult basic education" " ...
-
你可以做 raw = open(filename).read() docs = [] for raw_doc in raw.split('\n---'): try: docs.append(yaml.load(raw_doc)) except SyntaxError: docs.append(raw_doc) 如果您无法控制原始文档的格式。 从PyYAML文档中, 双引号是最强大的风格和唯一可以表达任何标量值的风格 。 双引号标量允许转义。 使用转义序列\ ...
-
这看起来像是一个JSON编码对象。 尝试这个: require 'json' p JSON.load(resp) json是Ruby 1.9的一部分,如果你使用1.8(或另一个Ruby实现),你可能需要使用gem install json安装json gem。 This seems like a JSON encoded object. Try this: require 'json' p JSON.load(resp) json is part of Ruby 1.9, if you use 1.8 ...
-
JSON源自Ruby哈希这一事实无关紧要。 你必须JSON.parse JSON字符串 - 但是! 那么你还必须访问结果对象的.color属性 let json = '{"color":"#FFFFFF"}' let data = JSON.parse(json) console.log(data.color) // #FFFFFF The fact that the JSON originates from a Ruby hash is irrelevant. You have to JSON.pa ...
-
在你的get方法中,你调用YAML.load ,但在你的set方法中你使用.to_s 。 这意味着YAML解析器正在尝试读取任意字符串,就好像它是YAML一样。 对于对称性,应该在set方法中使用YAML.dump 。 我已经创建了一个带有更改的拉取请求。 In your get method you call YAML.load, but in your set method you use .to_s. This means that the YAML parser is trying to read ...
-
你不应该使用new两次,并且read_string之后不应该有-> (参考YAML :: Tiny的POD)。 更改: $yaml = YAML::Tiny->new->read_string->($stuff); 至: $yaml = YAML::Tiny->read_string($stuff); 这是一个完整的工作示例: use warnings; use strict; use YAML::Tiny; my $stuff = ' Cluster1: source_mou ...
-
用map替换each ,返回值而不是输出它,你就完成了。 def hash_string(hash) set_value = hash.map {|key, value| "#{key} = #{value}" } set_value.join(",") end Replace each with map, return the value instead of outputting it, and you're done. def hash_string(hash) set_value = ...
-
案例1: YAML允许不带引号的标量 ,或“裸”字符串,不包含在引号中。 与引用的字符串相比,它们不太灵活,因为您不能在不创建模糊语法的情况下使用某些字符,但Ruby解析器确实支持它们。 1.9.3-p448 > YAML::parse('test').to_ruby => "test" 案例2:正如您所猜测的那样,这是特定于Ruby的,因为YAML没有“符号”的概念。 将YAML 映射转换为Ruby哈希时,以冒号开头的标量键将被解释为符号而不是字符串。 案例3:根据YAML对映射的定义,键必须是唯一的, ...
-
是的,这看起来像是C syck库中的一个bug。 我使用PHP syck绑定(v 0.9.3)检查了它: http ://pecl.php.net/package/syck并且存在相同的错误,表明它是库中的错误而不是ruby yaml库或ruby-syck绑定: // phptestsyck.php 在cli上运行它会产生相同的SyckException: ...
-
您可以将字符串括在花括号{} ,然后使用json解析它 require 'json' json_str = "{#{ str }}" hash = JSON.parse(json_str) You can enclose the string inside curly braces {} and then parse it using json require 'json' json_str = "{#{ str }}" hash = JSON.parse(json_str)