Search code examples
ruby-on-railselasticsearchrubygemschewy-gem

How to find the corresponding ES query for a Chewy query?


I use use chewy in my rails app to query ElasticSearch.

Autocomplete is my ElasticSearch Index.

AutocompleteIndex.suggest(
      suggest: {
        text: cleanup_query('query'),
        completion: {
          field: 'search_suggest',
          size: 10,
          contexts: {
            entity: @contexts
          },
          fuzzy: { fuzziness: 'AUTO' }
        }
      }
    ).suggest

this is the chewy query I use to query Autocomplete data. This, in turn, queries like this,

{:body=>{:suggest=>{:suggest=>{:text=>["9"], :completion=>{:field=>"search_suggest", :size=>10, :contexts=>{:entity=>[{:context=>"All"}]}, :fuzzy=>{:fuzziness=>"AUTO"}}}}}, :index=>["autocomplete"], :type=>[]}

How can I get the corresponding ElasticSearch Query for the above?


Solution

  • To enable ES log, set Chewy.logger.level = :debug in initializers/chewy.rb

    {:body=>{:suggest=>{:suggest=>{:text=>["9"], :completion=>{:field=>"search_suggest", :size=>10, :contexts=>{:entity=>[{:context=>"All"}]}, :fuzzy=>{:fuzziness=>"AUTO"}}}}}, :index=>["autocomplete"], :type=>[]}
    

    The above is the formatted elastic search query. The corresponding ES query for the above would be:

    curl -X GET "localhost:9200/autocomplete/_search?pretty" -H 'Content-Type: application/json' -d'
    {
            "suggest" : {
                "suggest" : {
                    "text" : ["9"],
                    "completion": {
                        "field": "search_suggest",
                        "size": 10,
                        "contexts": {
                            "entity":[{"context": "All"}]
                        }
                    }
                }
            } 
    }
    '