Search code examples
javascriptrubyruby-on-rails-4amazon-web-servicesamazon-cloudsearch

AWS Cloudsearch Ruby SDK errors on providing expressions: Found invalid value where JavaScript Object was expected


I am trying to use AWS cloudsearch from rails console.

I am trying to provide expressions to AWS cloudsearch searchRequest query. It gives me error : "Found invalid value where JavaScript Object was expected"

I am able to provide a simple query string, a filter query and a query parser.

terminal :

Aws.config.update({region: 'us-west-2',credentials: Aws::Credentials.new(‘key_id', ’secret_key'), })
csd = Aws::CloudSearchDomain::Client.new(region: 'us-east-1', endpoint: 'http://search-endpoint.us-east-1.cloudsearch.amazonaws.com/')

1.9.3-p551 :340 >   
1.9.3-p551 :341 >   fq
 => "location:['40.628611,-100.694152','25.621966,-66.686706']" 
1.9.3-p551 :342 > csd
 => #<Aws::CloudSearchDomain::Client> 
1.9.3-p551 :343 > csd.search(query:'user*', query_parser:'simple', filter_query: fq)
 => #<struct Aws::CloudSearchDomain::Types::SearchResponse status=#<struct Aws::CloudSearchDomain::Types::SearchStatus timems=11, rid="5/L54eYpoqkBCluMDg==">, hits=#<struct Aws::CloudSearchDomain::Types::Hits found=0, start=0, cursor=nil, hit=[]>, facets=nil> 
1.9.3-p551 :344 > csd.search(query:'user*', query_parser:'simple', filter_query: fq, expr:'1')
Aws::CloudSearchDomain::Errors::SearchException: Invalid syntax in 'expr' parameter: Found invalid value where JavaScript Object was expected
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/aws-sdk-core-2.1.2/lib/seahorse/client/plugins/raise_response_errors.rb:15:in `call'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/aws-sdk-core-2.1.2/lib/aws-sdk-core/plugins/param_converter.rb:21:in `call'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/aws-sdk-core-2.1.2/lib/aws-sdk-core/plugins/response_paging.rb:26:in `call'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/aws-sdk-core-2.1.2/lib/seahorse/client/plugins/response_target.rb:18:in `call'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/aws-sdk-core-2.1.2/lib/seahorse/client/request.rb:70:in `send_request'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/aws-sdk-core-2.1.2/lib/seahorse/client/base.rb:207:in `block (2 levels) in define_operation_methods'
        from (irb):344
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `block in require'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
        from /Users/anshulgoyal/workspace/adamantium/backend/truckfinder_service/bin/rails:8:in `<top (required)>'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `block in load'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /Users/anshulgoyal/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
        from /Users/anshulgoyal/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /Users/anshulgoyal/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require'
        from -e:1:in `<main>'1.9.3-p551 :345 > 

Any help here will be greatly appreciated.

Thanks,
Anshul


Solution

  • Error faced here is similar to the one asked in AWS forums : https://forums.aws.amazon.com/thread.jspa?messageID=579640

    Solution is to create a map for expressions, where each |key,value| denotes expressions name and its formula(value). And pass the JsonEncoded string of this map to the search method of cloudSearchDomain.

    e.g.

    1.9.3-p551 :520 > expr_map2 
    
    => {:geo=>"haversin(38.958687,-77.343149,location.latitude,location.longitude)"} 
    
    1.9.3-p551 :521 > csd.search(query:'user*', query_parser:'simple', filter_query: fq, expr:ActiveSupport::JSON.encode(expr_map2)) 
    
    => #<struct Aws::CloudSearchDomain::Types::SearchResponse status=#<struct Aws::CloudSearchDomain::Types::SearchStatus timems=45, rid="wPzUhOcp7rIBCluMDg==">, hits=#<struct Aws::CloudSearchDomain::Types::Hits found=0, start=0, cursor=nil, hit=[]>, facets=nil> –