2

I am trying to setup solr to use with postgres db which I use via flask sqlalchemy orm. I found the library pysolr for the purpose but it is not clear how to setup hooks within the sqlalchemy models to update solr index. Are there any examples?

pysolr suggests inserting documents manually, via solr.add, but it's not clear how you would separate indices for different database tables.

after doing some research I came up with the following approach, I am wondering if this is right way to go:

  1. in the ORM models, hook after_insert, after_update, after_remove and after_commit and insert/update/remove the object data in solr in these events.

  2. to segregate data of different models use the table name as prefix in the "id" field of solr documents. solr_id = db_table_name + db_id

  3. when you do a search, get all the results, filter manually those matching the db table required, extract the ids, lookup the db against those ids and use those db results.

is there a better way to about doing this? thanks.

vrtx54234
  • 2,196
  • 3
  • 30
  • 53

1 Answers1

0

SQLAlchemy and Solr are different structure. I think a better solution is implement a script to synchronize data. Run the script to update maybe 30 minutes or a hour for new data.

Binding insert/update/remove/commit mechanisms in model isn't good way. Because if your Solr services have any problems, your website (about access database) will be affected. Keep difference services independent.

Puffin GDI
  • 1,702
  • 5
  • 27
  • 37
  • I was planning on triggering celery tasks on model events, to keep the two services a bit away. I can try using a separate script too. can you comment on segregating data belonging to different tables, in solr? – vrtx54234 Feb 18 '14 at 08:40
  • When data be added to Solr, it's become the document structure for search. I think you can reach any data from different tables for your requirement if it's need to be search. – Puffin GDI Feb 18 '14 at 08:52