Question

I need to append a string with special charters from a query to elasticsearch in rails. I try using .html_safe, and raw() it doesn't break anything, but it still passes the charters as hex or something.

these all pass the same thing to elasticsearch

<%= @esearch.search q:params[:query] + '&pretty=true' do %>

<%= @esearch.search q:params[:query] + '&pretty=true'.html_safe do %>

<%= @esearch.search q:params[:query] + raw('&pretty=true') do %>

I'm getting this:

q=asa%26pretty%3Dtrue

I'm hoping to get this:

q=asa&pretty=true
Was it helpful?

Solution

You are not running into an html-escaping issue. raw and html_safe are only useful when you don't want output escape when printed directly to a user's browser. For instance, the params would output q=asa&amp;pretty=true if you were running into an html-escaping problem, but you are not.

What you are seeing is the results of CGI.escape which outputs url-safe values.

It looks to me (based on your current use of the search method), that the hash options get translated to query params. Have you tried this:

@esearch.search q: params[:query], pretty: true

I'm not sure what search is doing, but this seems like an odd method to be using directly in your view. Of course I could be wrong on that since I don't know exactly what it's doing.

UPDATE:

Check out the documentation here: https://github.com/elasticsearch/elasticsearch-ruby/blob/master/elasticsearch-api/lib/elasticsearch/api/actions/search.rb#L15

There's two ways of sending query params, it looks like. One is with that q: 'title:text' format which is what you're sending your params[:query] (but it looks like your query is just 'asa' so I'm not sure you're even using the right format there?) with, and the other is through the body hash:

@esearch.search body: {
  query: { match: { title: 'test' } },
  facets: { tags: { terms: { field: 'tags' } } }
}

You should look through that documentation including the links to the elastic search options documentation and figure out what you need to send it. I don't see that pretty: true option in the docs so not sure what that's for. But there's a lot of docs and I didn't really look at them extensively, I'll leave that up to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top