Searchable data from external database

Permalink 2 users found helpful
Hey Folks-

I've created a custom block that is pulling data from an external database and displaying it on the page. This block is located in a 'Main' area and I was wondering if there is a way to have the data I'm displaying from the external database show up in a search.

I was hoping that because I placed the block in the Main area the indexer would crawl the rendered page for indexing purposes, but I guess not.

Any help is appreciated.

cartersch
 
wguggen replied on at Permalink Reply
wguggen
Hi,

I have the same problem.
I guess, you must enter the function getSearchableContent() in the controler.php from your custom block.

public function getSearchableContent() {   return $this->content;}


I found this some where in the documentation, but it does not work for me.

Look at:http://www.concrete5.org/documentation/general-topics/search...
jgarcia replied on at Permalink Best Answer Reply
jgarcia
That's partly right - but you have to make sure that getSearchableContent is actually returning the right thing. Most likely $this->content is not equal to the actual content rendered by your block. Essentially what you need to do is get all of your block's contents into a variable within the getSearchableContent function and then return that variable.
wguggen replied on at Permalink Reply
wguggen
Right, I have used a own functionality to get the whole text from my ccustom block into a variable und return it on getSearchableContent().

I also debug the code and saw the function was called if I update the search index.

But when I use the search block, I do not get any results back and I haave no idea or solution at the moment.
cartersch replied on at Permalink Reply
cartersch
I was able to get it work by using the getSearchableContent() function in conjunction with the a custom save() function in my controller.

It's a little messy, but this is how I got it to work:

function save($data) {
  $args['block_name'] = $data['block_name'];
    $db=Loader::db('server','user','password', 'database', true);
  $external_info = $db->Execute("SELECT field1, field2 FROM external_db_table WHERE field1 = blah");
$db=Loader::db(null,null,null,null,true);
  $searchable_info = $args['block_name'];
  foreach($external_info as $info){
    $searchable_info .= $info['field1'].' '.$info['field2'];
  }
  $args['seachable_info'] = $searchable_info;
  parent::save($args);
}
function getSearchableContent(){
   return $this->searchable_info;
}


I guess you could put all the db connection/query stuff within the getSearchableContent() without the writing it to the block table, but my first attempt on that failed. But in theory it should work.

After adding my custom block to a page and publishing it, I then went to the dashboard and ran the indexer and now the data from the external database shows up in the search results!

just as an fyi, it also seems that anything you return in the getSearchableContent() function should show up in the pagesearchindex table in the 'content' field.