Searching WordPress blog from Rails app - ruby-on-rails

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?

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).

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.

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.

You don't have to stuck with wordpress. You can use Google search APIs. Web search api has been deprecated but still working. Its replacement is Custom Search API. You may need to pay if you query over the limit.
Alternatively you can leverage other search engine APIs like Bing Search API.

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.

Use Tire and wp-elasticsearch with ElasticSearch.

Related

How to connect Rails app to a website's API?

I know I'm going to get downvotes for even asking but I honestly have no clue how to do this and any assistance would be appreciated as I have never done this.
I have a client that migrated their blog to HubSpot and would like to have their blog posts displayed on their website. So how do I connect to HubSpot's API to display the blog in their website that I have in a Rails app?
Do I create MVC just for Blog API? Where do I put the URL to connect to?
If you want to connect to an other API the best way to do this is to create service objects. These are plain old Ruby objects (aka PORO) that represent the API as a Ruby object. You are free to use any location you want, but I would stick them in lib/services or app/services. If the provided API uses the Rails (REST) standards than you might be able to use ActiveResource.
You could also look for gems that provide these service objects. A quick RubyGem search finds multiple (unofficial) gems. From witch hubspot-ruby seems the most used and active.
If the gems don't do what you want you could look through the code to find some inspiration and create you own objects. Another option is to fork a project and add the functionality you want.
Here is a link to answer the broader question you're asking: What is the proper "Rails Way" to consume a RESTful web service on another domain?

Rails for mobile API

I want to create a service with many posts, which have many attributes.
Users enter data (age, gender, category, etc.) and get some posts.
Which gem use for search by categories?
Tell me please the best way to create API for mobile phones.
There are many options for implementing search, depending on what you need. Rails has easy integration with elasticsearch which provides full text search capabilities. For, search by categories, you can even use plain ActiveRecord queries to get this done to some extent. Again, depending on your need.
You can use active_model_serializers or jbuilder for building JSON API using Rails and then your mobile clients can consume these APIs.
There are lots of resources about how to build a JSON API in Rails using AMS or jbuilder. You just need to google it.

Rails 4 search options. What options are available to combine a full HTML site search with an ActiveRecord search?

I'm looking for a solution that combines the power of Google Site Search with Elasticsearch for a Rails 4 app preferably as a Rails gem.
As far as I can tell the most popular search solutions, ThinkingSphinx, ElasticSearch with the tire gem, Sunspot Solr and lastly PostreSQL searching functionality, all seem to only handle database searching and do not have page/html template searching functionality.
If I have this wrong then please correct me and I will happily pick one of the above.
The site contains mainly static HTML so the Google Site Search api is the obvious solution but there are some ActiveRecord results that should also be included in the results of a search.
If there really isn't a simple combined solution then I would appreciate any pointers as to how to achieve the merging of an elasticsearch (my preffered AR search solution) with a google site search where the results happen to include the same pages so page results are not duplicated.
To rephrase your question, you want to combine results of Google Site Search with custom search results provided by Sphinx, Solr or Elasticsearch?
First, you cannot really customize the content of Google Site Search results easily. You can customize the design, and could employ some JavaScript tricks to "merge" its results with another data source, but I'd say the approach is not something maintainable and, more importantly, usable.
Notice, that you can display the search results from Elasticsearch with Tire in the same way as ActiveRecord instances, all the usual Rails helpers such as url_for etc. work. The easiest way to evaluate the integration is to generate the example application with the Rails template.
If you want to combine the results from ActiveRecord data and the results from any static pages you might have on your site/application, it wouldn't be hard to write a simple crawler which would retrieve, parse and index the content of static pages and store it as ActiveModel-compatible documents in Elasticsearch.

How do I create a search engine or modify an existing one for searches only within a website?

I am creating a website using Ruby on Rails, HTML (of course), CSS (of course), JavaScript, Sqlite 3, and Ajax while running an Apache server.
I want to have a search feature that only searches for content on the website: people, places, ideas, topics, etc.
How would I go about getting such a feature on my website and what search "programs" would you advise?
P.S. I already am aware of the Google search thingy.
This question feels like you haven't even bothered to investigate it by yourself.
I guess all your content is coming from your database? You have to connect your database with your searchengine and search for your data.
You should probably search for a tutorial on SQLite Databases.

Are there any Ruby on Rail Gems or Plug-ins that performs web queries

I need build in web query capability similar to what you can do in Excel for a project that I'm working on. Has anyone come across a gem, plug-in or application with this capability.
Clarification: To clarify, the excel web query features that I'm trying to create is the ability to extract data from specified tables on a web page. A good example would be if given a particular ticker symbol the web query would be able to submit the ticker symbol and then extract and display output table.
I've looked at fork on project like nokogiri, celerity and other screen scraping apps but have not come across what I'm looking for yet.
Thanks for your insights, ideas and suggestions.
If you are interested in possible screen scraping gems/plugins checkout: regexp_crawler, spidr, scrAPI or mechanize

Resources