6

Following problem: I use magento enterprise edition with solr search. That works great! Now I'd like to do a search with my own query string programmatically. How can I achieve that?

To be a bit more specific: How can I use my own query string and get the collection out of the solr search within my block?

Thanks for any help and/or suggestions!

4

Have a look on \Enterprise_Search_Model_Client_Solr::searchSuggestions and use this method for searching.

| improve this answer | |
3

Old question, but answering for posterity: The following snippet will run a standard search for $term using whatever search engine is configured within Magento (fulltext, Solr, etc.). The resulting $collection is an extension of the standard product collection, and will be sorted by relevance.

$searchEngine   = Mage::getResourceModel( Mage::getStoreConfig('catalog/search/engine') );
$collection = $searchEngine->getResultCollection();
$collection->addSearchFilter( $term );
$collection->load();
| improve this answer | |
  • I am using this snippet, and despite Magento being set to and using Solr, it doesn't seem to be hitting Solr at all (adding debugging checks to see if the classes related to Solr are called). Moreover the results I obtain from this search aren't relevant to the term in my case. Perhaps there is some context I'm missing for this snippet, but it seems to not obtain relevant results. Using Magento 1.14.1 EE – JPMC Jul 13 '15 at 14:39
  • @JPMC This is the entirety of the snippet in production. Maybe the explicit load() makes an impact, but I wouldn't expect so. Check the class type of $searchEngine and $collection, maybe something else on your system is interfering. $searchEngine = Mage::getResourceModel( Mage::getStoreConfig('catalog/search/engine') ); $collection = $searchEngine->getResultCollection(); $collection->addSearchFilter( $term ); $collection->addAttributeToSelect('sku'); $collection->setPageSize( $limit ); $collection->load(); – Ryan Hoerr Jul 13 '15 at 16:10
  • It would seem it is indeed the load() Before that I tried manually instantiating a new Enterprise_Search_Model_Resource_Collection and passed it the Solr config settings as an array in the constructor. But upon going back and changing it back to the way you had it, it seems it is working now without the additional setup. Thanks again for this snippet, I wish I had seen it a week ago! – JPMC Jul 13 '15 at 17:27
2

Solr has REST-like HTTP/XML and JSON APIs. If you want to get any information from solr, you can get it via HTTP request. For fast and straight-forward you can use direct HTTP calls. For more complex logic you can use libraries like http://www.solarium-project.org/

| improve this answer | |
2

As Fabian mentioned searchSuggestions is a good start, if you want to see it from Magento side.

Do you want to search in the products core or create your own new core with new data? Anyway I can suggest the wiki at http://wiki.apache.org/solr. It's one of the better documentes Open Source projects.

To just get started it's also very helpful by just executing the query in your browser and after that stacking everything in the Magento code.

For example a simple query to just return all ids from your main core matching with the search termin "touch" you can start with:

http://you.solrhost.com:8080/solr/select/?q=touch&fl=id

Add &insdent=on to get a well formatted human readable output. Or set &wtj=json to return json instead of XML, what Magento is also doing when quering data.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.