Reputation: 1460
I built a search engine on apache solr for my Question/Answer website which is based in pyramid python framework. I am using DataImportHandler to get the data for solr documents from mysql db. But now I need to instantaneously make the document in python itself as soon as a Question or Answer is posted i.e I should make a document in json format and send to Solr?
I have gone through documentation of various APIs but only partially understood the implementation. Can somebody explain how to do this? Also is what I intend to do the same as pushing a document into solr?
Thanks in advance
Upvotes: 1
Views: 1593
Reputation: 52832
Yes.
If you're using an ORM (and I'm guessing SQLAlchemy), hook into the (before|after)_insert
and (before|after)_update
events (and probably delete as well), so that you can push content to Solr when the local content is updated. Depending on how you want to set it up in pyramid, there's several options - you can add it do the configuration with a @reify
decorator to allow it to live through requests, or you can probably bind it directly to SQLAlchemy when setting it up.
You should use an existing Solr library for python to simplify talking to Solr, my own experience is good with mysolr
(as it doesn't require special handling for each Solr type available), although there are quite a few possible libraries out there. Do a search on Google and you'll find a selection of different libraries.
Pushing content to Solr with mysolr is straight forward:
import solr
client = solr.Solr(url)
doc = {
'url': '...',
'store': '...',
'name': '...',
...
}
solr.add(doc)
# or if you have many documents
solr.add_many([doc])
Upvotes: 2