search title

With the default Elgg search engine, both title and description are searched. I'm wondering if there's a way I can only search the keyword in title ? Thanks.

  • Look at the search hooks. Specifically you could unregister search_objects_hook (from hook search, object) and register your own function in its place. In your function, remove "description" from $fields.

  • Hi Steve,

    I directly change the code inside /mod/search/search_hooks.php.

    //      $fields = array('title', 'description');
            $fields = array('title');

    However, I got this error when I search it at elgg webpage. Anything that I missed ?

    Can't find FULLTEXT index matching the column list

    QUERY: SELECT count(DISTINCT e.guid) as total FROM entities e JOIN objects_entity oe ON e.guid = oe.guid WHERE (MATCH (oe.title) AGAINST ('bonita' )) AND ((e.type = 'object' AND e.subtype IN (4))) AND (e.site_guid IN (1)) AND ( (1 = 1) and e.enabled='yes')

  • @Liang Cheng Don’t Modify Core

    Use in your own plugin:

    elgg_unregister_plugin_hook_handler('search', 'object', 'search_objects_hook');

    elgg_register_plugin_hook_handler('search', 'object', 'search_objects_my_hook');

    function search_objects_my_hook($hook, $type, $value, $params) {
    
    	$db_prefix = elgg_get_config('dbprefix');
    
    	$join = "JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid";
    	$params['joins'] = array($join);
    	$fields = array('title');
    
    	$where = search_get_where_sql('oe', $fields, $params);
    
    	$params['wheres'] = array($where);
    	$params['count'] = TRUE;
    	$count = elgg_get_entities($params);
    	
    	// no need to continue if nothing here.
    	if (!$count) {
    		return array('entities' => array(), 'count' => $count);
    	}
    	
    	$params['count'] = FALSE;
    	$params['order_by'] = search_get_order_by_sql('e', 'oe', $params['sort'], $params['order']);
    	$params['preload_owners'] = true;
    	$entities = elgg_get_entities($params);
    
    	// add the volatile data for why these entities have been returned.
    	foreach ($entities as $entity) {
    		$title = search_get_highlighted_relevant_substrings($entity->title, $params['query']);
    		$entity->setVolatileData('search_matched_title', $title);
    	}
    
    	return array(
    		'entities' => $entities,
    		'count' => $count,
    	);
    }
  • @RvR, thank you. But still I have the issues I reported earlier. I can't get search result by either "title" and "description" individually. I can only get search result when I have both defined in "$fields". What I did is I copied your code to my plugin, then I deactivate my plugin and activate it again. Did I miss anything ?

  • Did you upgrade to initiate the code changes?

  • Yes I did the upgrade. Still I got the same errors. Here I copied more text from my screen.

     

    Fatal Error.

    Can't find FULLTEXT index matching the column list

    QUERY: SELECT count(DISTINCT e.guid) as total FROM entities e JOIN objects_entity oe ON e.guid = oe.guid WHERE (MATCH (oe.title) AGAINST ('test' )) AND ((e.type = 'object' AND e.subtype IN (4))) AND (e.site_guid IN (1)) AND ( (1 = 1) and e.enabled='yes')

    DatabaseException Object
    (
    [message:protected] => Can't find FULLTEXT index matching the column list

    QUERY: SELECT count(DISTINCT e.guid) as total FROM entities e JOIN objects_entity oe ON e.guid = oe.guid WHERE (MATCH (oe.title) AGAINST ('test' )) AND ((e.type = 'object' AND e.subtype IN (4))) AND (e.site_guid IN (1)) AND ( (1 = 1) and e.enabled='yes')
    [string:Exception:private] => exception 'DatabaseException' with message 'Can't find FULLTEXT index matching the column list

    QUERY: SELECT count(DISTINCT e.guid) as total FROM entities e JOIN objects_entity oe ON e.guid = oe.guid WHERE (MATCH (oe.title) AGAINST ('test' )) AND ((e.type = 'object' AND e.subtype IN (4))) AND (e.site_guid IN (1)) AND ( (1 = 1) and e.enabled='yes')' in /var/www/html/engine/lib/database.php:274
    Stack trace:
    #0 /var/www/html/engine/lib/database.php(416): execute_query('SELECT count(DI...', Resource id #3)
    #1 /var/www/html/engine/lib/database.php(379): elgg_query_runner('SELECT count(DI...', '', true)
    #2 /var/www/html/engine/lib/entities.php(1090): get_data_row('SELECT count(DI...')
    #3 /var/www/html/mod/web_services/start.php(23): elgg_get_entities(Array)
    #4 [internal function]: search_objects_my_hook('search', 'object', Array, Array)
    #5 /var/www/html/engine/lib/elgglib.php(989): call_user_func_array('search_objects_...', Array)
    #6 /var/www/html/mod/search/pages/search/index.php(186): elgg_trigger_plugin_hook('search', 'object', Array, Array)
    #7 /var/www/html/mod/search/start.php(74): include_once('/var/www/html/m...')
    #8 [internal function]: search_page_handler(Array, 'search')
    #9 /var/www/html/engine/lib/pagehandler.php(53): call_user_func('search_page_han...', Array, 'search')
    #10 /var/www/html/engine/handlers/page_handler.php(46): page_handler('search', NULL)
    #11 {main}
    [code:protected] => 0
    [file:protected] => /var/www/html/engine/lib/database.php
    [line:protected] => 274

Performance and Scalability

Performance and Scalability

If you've got a need for speed, this group is for you.