Search code examples
thinking-sphinx

ThinkingSphinx: How to get the sphinxql SQL expression?


pancakes = Article.search 'pancakes'

How would I then get the resultant underlying sphinxql expression? Is it possible to just retrieve the expression without performing the query?


Solution

  • Answered by Pat on github:

    To find the SphinxQL statement, you can do the following:

    pancakes = Article.search 'pancakes'
    pancakes.populate
    pancakes.context[:sphinxql].to_sql
    

    However, if you want to get that without actually sending the query to Sphinx, you'll want to send through a much more minimal middleware stack:

    middleware = ::Middleware::Builder.new do
      use ThinkingSphinx::Middlewares::SphinxQL
    end
    
    pancakes = Article.search 'pancakes', :middleware => middleware
    pancakes.populate
    pancakes.context[:sphinxql].to_sql
    

    You can also send the :populate => true option through as part of the search options instead of calling populate on the search object, to save a line of code. TS search results are lazily loaded, hence the need for that when accessing the context data.