Prabhakaran
Prabhakaran

Reputation: 4013

solr sunpost interaction between two application

I have developed an application with rails and solr sunspot full text search.

Here users can post a topic. And the user need to wait for approval of the post from the administrator.

Eg: If a user post a topic in domain.com and the approval is made from admin.domain.com

@search = Post.where(status: 1).search do
      fulltext params[:search] do
        minimum_match 1
      end
      with :status, 1
      paginate page: params[:page], per_page: 15
    end
    @posts = @search.results

Unless admin approve the post status will be 2. In administrator if I change the status from 2 to 1 from admin I could not find the result. How can I interlink with it.

How can I handle this kind of problem. Can any one help

Edit-1

I am going to make this as a bounty question. I am facing lot of difficulty with this. Can any one solve this with better solution else anyone know any other way of performing such a search

Upvotes: 1

Views: 203

Answers (1)

Yann VERY
Yann VERY

Reputation: 1819

If you have 2 applications, I presume they used same database but only your main app use sunspot.

The problem is when an admin update an object this object is not reindexed in sunspot/solr.

Option 1 Add sunspot on your admin app with same searchable block for concerned models, and used same solr server configuration.

Option 2 Create a custom controller method ( like an api ) on your main app. This method forces reindex of an object like Sunspot.index Post.find(params[:id]) After each update on a Post on your admin app, you need to call reindex post method of your main app.

Option 3 (easiest) Remove your scope on :status like this :

@search = Post.where(status: 1).search do
  fulltext params[:search] do
    minimum_match 1
  end
  paginate page: params[:page], per_page: 15
end
@posts = @search.results

Edit

This update add more informations for use option 1 :

I create an application with one model : Article

class Article < ActiveRecord::Base
  searchable do
    text :title, :boost => 5
    text :content
    integer :status
  end
end

I want to show all articles on my admin app and only articles with status=1 on my user app. Both applications use same database and same solr server.

Configuration for both applications

I use sunspot_rails(https://github.com/sunspot/sunspot) and sunspot_solr for this test.

#Gemfile 
gem 'sunspot_rails'

group :development do
  gem 'sunspot_solr'
end

#config/database.yml
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

#config/sunspot.yml use same host and same port for both applications
development:
  solr:
    hostname: localhost
    port: 8982
    log_level: INFO
    path: /solr/development

Note : I launch solr only one time on user app with this command

bundle exec rake sunspot:solr:start

Article controller for admin app

I want to show all articles on my admin app like this :

def index
  @search = Article.search do
    fulltext params[:search]
  end
  @articles = @search.results
end

Article controller for user app

In my user app, I just want to see articles with statuts == 1

def index
  @search = Article.search do
    fulltext params[:search]
    with :status,1
  end
  @articles = @search.results
end

Now when I use my admin app, and I update an article's status solr is updated simultaneously. And if I refresh my articles index page on my main app, this article appears.

I hope you have more informations now.

Upvotes: 2

Related Questions