Searching WordPress blog from Rails app

I have a site-wide custom written search controller for my Rails 3 app and would like to include results from the site’s WordPress blog. What is the best way for me to perform a keyword search on posts from within my Rails app?

Related posts

Leave a Reply

6 comments

  1. If you share database then just use SQL query on it. This solution gives you speed of direct db query but you’ll need to construct that query properly in order to get all relevant data.

    If you don’t have access to the WP database from your Rails app then the best way will be to use curl, httparty, RestClient or any other file retrieval library.

    To do that, create WordPress page with custom template which will output search results in a format which is best for you to parse in Rails app (json, xml, csv, urlencoded, whatever).

    Then request that WP page from your Ruby app using curl/RestClient/httparty…

    This solution gives you the power of WP template tags and functions to get the results.

    Also instead of creating custom template from scratch you can just simply copy and tweak search.php from core template to provide the results in a format required by your Rails app.

    With this solution you are lacking the speed of direct access to db because all search result will have to be transferred through http pipe and you have to process the data twice (encode to the proper format in WP and decode in Rails app).

  2. Interesting problem. I think I would approach it like this:

    • Use RSS as the text transport from the blog to your rails app. This allows the flexibility to add more blogs in the future, change your blog engine, database host, etc. It also protects you from WordPress code updates. Given the security history of WordPress, I like to host them in a protected sandbox anyway. RSS is the native language for blogs, so it seems a natural fit for this kind of content integration.

    • Use the feedzirra gem to import RSS entries into a rails model.

    • Use Elasticsearch and tire for fuzzy text searching across both your rails app and your blog entries. See this Railscast for details.

  3. Option 1. is to use search engine for both sites, like elasticsearch, solr etc. So you populate the index from rails and wordpress.

    Option 2. You write script, that reads periodically your wordpress RSS and saves data in your rails app.

    At the end you should avoid to search from different sources, you should gather the data into one place and then search.

  4. I’d suggest using the WordPress JSON API and plugging that into your search using solr or something similar. You can index as posts are created and then call the articles via the sam JSON interface.