You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey! I'm trying to decode elastic URL queries from elastic's search-ui package. I've written a lot of manual code to convert a query string to an app search compatible filter/query combo. I based most of it off of the urlToState in URLManager from search-ui
I was wondering if the team would be open to including an official version of this parsing logic in this library, so that we can parse search-ui query strings as app search queries from ruby
Code is below – if you think it's a good idea, I can make a PR to incorporate this into the codebase, or if one of y'all would like to tackle this from the ground up, that would work too – thanks!
# typed: truemoduleElasticServiceclassUtilextendT::Sig# TODO: Probably need to support nested filters?sig{params(query_string: String).returns(T.untyped)}defself.query_string_to_app_search_query(query_string)query_object_raw=raw_nested_query_to_elastic_query(Rack::Utils.parse_nested_query(query_string.delete_prefix("?")))cleaned_filters=query_object_raw['filters'].mapdo |filter_entry|
{"any"=>filter_entry["values"].mapdo |sub_filter|
cleaned_sub_filter=sub_filterifcleaned_sub_filter.class == Hash# Elastic gets angry if we add extra fields (:cleaned_sub_filter=cleaned_sub_filter.except("name")end{filter_entry["field"]=>cleaned_sub_filter}end}endfilters={"all"=>cleaned_filters}{"query"=>query_object_raw['q'] || '',"filters"=>filters,}enddefself.parse_escaped_string(value_raw)value=CGI.unescape(value_raw)if/^n_.*_n$/.match(value)# Remove first and last 2 characters from stringreturnvalue.delete_prefix("n_").delete_suffix("_n").to_fendif/^b_.*_b$/.match(value)# Remove first and last 2 characters from stringreturnvalue.delete_prefix("b_").delete_suffix("_b").to_boolend# Need rfc3339 for elastic to be happy# but JSON stringifies to iso8601returnDate.iso8601(value).rfc3339rescuevalueend# Rack's parse_nested_query doesn't convert array-like things to arrays!# This handles that, so that we can manipulate the list of filters without# introducing "holes" into our filter array# Also handles elastic primitive encoding (i.e. "n_123_n") and converts date strings properlydefself.raw_nested_query_to_elastic_query(nested_query,key_context=nil)ifnested_query.class == Hashnested_query=T.let(nested_query,Hash)# If every key is an int, convert to arrayifnested_query.keys.all?{|k| T.let(Integer(k,exception: false),T.nilable(Integer)) && k.to_i >= 0}max_index=nested_query.keys.map(&:to_i).maxbacking_array=Array.new(max_index + 1)nested_query.eachdo |k,v|
backing_array[k.to_i]=vend# Recurse now that it's been transformed to an arrayreturnraw_nested_query_to_elastic_query(backing_array)elsereturnnested_query.mapdo |k,v|
[k,raw_nested_query_to_elastic_query(v,k)]end.to_hendelsifnested_query.class == Arrayreturnnested_query.map{|el| raw_nested_query_to_elastic_query(el)}elsifnested_query.class == Stringreturnparse_escaped_string(nested_query)elsereturnnested_queryendendendend
The text was updated successfully, but these errors were encountered:
Hey! I'm trying to decode elastic URL queries from elastic's search-ui package. I've written a lot of manual code to convert a query string to an app search compatible filter/query combo. I based most of it off of the urlToState in URLManager from search-ui
I was wondering if the team would be open to including an official version of this parsing logic in this library, so that we can parse search-ui query strings as app search queries from ruby
Code is below – if you think it's a good idea, I can make a PR to incorporate this into the codebase, or if one of y'all would like to tackle this from the ground up, that would work too – thanks!
The text was updated successfully, but these errors were encountered: