[solved] adding search to a module

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
markS
Forum Members
Forum Members
Posts: 58
Joined: Wed Aug 20, 2008 3:04 pm

[solved] adding search to a module

Post by markS »

Hello,

I've been creating modules for a little while now and I'd like to try and enhance them by integrating them into the cms search functionality. My questions is, is there documentation on the calls that need to be made to make the contents of my module available to the search system?

I thought the news module would be a good place to start and I've found the SearchReindex function, which seems reasonably understandable. Would that be all I need to add to my modules, is the correct application of that function enough to make all my module content searchable? It appears to me to handle reindexing all existing content, but what about a newly created item?

I can see that there's a function to handle removal of the words from the index:

Code: Select all

//Update search index
$module =& $this->GetModuleInstance('Search');
if ($module != FALSE)
{
    $module->DeleteWords($this->GetName(), $articleid, 'article');
   } 
But there doesn't appear to be a corresponding update of the search index when adding content?

Are there any docs on this or could anyone suggest a good place to start learning about how to do this?

I'd really appreciate any pointers people can give me.

Thanks,
Mark.
Last edited by markS on Wed Sep 07, 2011 10:13 am, edited 1 time in total.
Duketown

Re: adding search to a module

Post by Duketown »

Mark,

To add an index entry (this is version 1.10 coding, so you might have to rewrite it a little bit to get it up and running in 1.9.x or earlier versions):

Code: Select all

		// Add to search index
		$modops = cmsms()->GetModuleOperations();
		$searchmodule = $modops->get_module_instance('Search');
		if ($searchmodule != FALSE)
		{
			$text = $product['name'].' '.$product['description'];
			$searchmodule->AddWords($this->GetName(), $product['product_id'], 'product', $text);
		}
In this case the name and the description of a newly added product are added to the search index. The $product['product_id'] is the key to the detailed information.

If the name or the description of the product is changed, you will have to undo/remove the old index entries and add the new one as follows:

Code: Select all

		// Update search index (AddWords first deletes the old ones)
		$modops = cmsms()->GetModuleOperations();
		$searchmodule = $modops->get_module_instance('Search');
		if ($searchmodule != FALSE)
		{
			if ($product['active'] == 0) {
				$searchmodule->DeleteWords($this->GetName(), $product['product_id']);
			}
			else {
				$text = $product['name'].' '.$product['pdescription'];
				$searchmodule->AddWords($this->GetName(), $product['product_id'], 'product', $text);
			}
		}
If your handy with php, do look at Search.module.php in the Search module to find out that there are more handy functions that you can use.

Duketown

PS. Do remember to remove the search index entries when you perform an uninstall of your module(s). You can do this with:

Code: Select all

// Remove all the search index entries
$modops = cmsms()->GetModuleOperations();
$searchmodule = $modops->get_module_instance('Search');
$searchmodule->DeleteWords($this->GetName());
PPS. Are any of the modules available for the community?
markS
Forum Members
Forum Members
Posts: 58
Joined: Wed Aug 20, 2008 3:04 pm

Re: adding search to a module

Post by markS »

Hello,

That is excellent, thank you very much for that!

You have clearly explained what to do when adding, updating and deleting items from the module and how to handle the uninstall.

I have been looking through the Search.module.php functions too which has proved to be instructive.

Most of the modules I've created so far have been for specific organisations and haven't been public. However I am writing a couple at the moment, where this functionality will be included, and I hope to make them available to the community.

Thanks once again for your extremely informative tutorial on this.

Regards,
Mark.
Post Reply

Return to “Developers Discussion”