What are differences between 'Attributes' and 'Metadata' of the 'fields' key in elgg_search options?

I wanted to be able to search a list of entities by metadata name 'brief' in this case. But

$query_string = 'string wanted to search for';

$options = array(

        'type' => 'object',

        'subtype' => 'post',

        'query' => $query_string,

        'fields' => [

            'metadata' => ['title', 'description'],

            'attributes' => ['brief'],

        ],

    );

return elgg_list_entities($options, 'elgg_search');

When I search for a string of text where matadata name = "brief", it does not work for me. So my question is what the differences between 'attributes' and 'metadata' in the 'fields' of $option array.

 

  • Attributes are properties EVERY Elgg entity has in any case (see vendor/elgg/elgg/engine/classes/ElggEntity.php). The're columns in the entties database table corresponding to the attributes and they are necessary to handle the entities in the database (e.g. unique guid, owner etc.). Everything else is handled by metadata. Different kind of entities (depending on type and subtype) can have additionally different kind of metadata (though some entities might not have all metadata it could have, e.g. if a user has not filled up all profile fields the user entity has not all corresponding metadata).

    When using the elgg_search function as getter function in elgg_list_entities you don't need to provide values for the attribute parameter if you don't want to search for the search term in attributes or the metadata you want to search in is not an attribute.

    I think it should work with

    $query_string = 'string wanted to search for';
     
    $options = [
        'type' => 'object',
        'subtype' => 'post',
        'query' => $query_string,
        'fields' => [
            'metadata' => ['brief'],
        ],
     ];
     
    return elgg_list_entities($options, 'elgg_search');
  • Thanks a lot iionly for the explanation!! 

    It still hasn't return the result I expected. I have ~10 metadata for this type/subtype. When I search for a term in 'title' (one of the metadata name), it works beautifully. But it returns nothing when searching for a string in 'brief' metadata. Do I have to manually join 'entities' table with 'metadata' table? 

  • I've read the documentation included in the bundled search plugin of Elgg 2.x and the problem is probably that Elgg does only search in the "title" and "description" metadata fields and does NOT search any other metadata fields by default intentionally. The documentation says that if you want to search other metadata fields you would have to provide your own search hook function.

    Maybe the other way would be to use elgg_list_entities_from_metadata() instead (or if you are using Elgg 3 it would be elgg_list_entities as this function now accepts all arguments formerly available only in the different elgg_get/list_entities functions). It would look like

    $query_string = 'string wanted to search for';
     
    $options = [
        'type' => 'object',
        'subtype' => 'post',
        'metadata_name_value_pairs' => [
            [
                'name' => 'brief',
                'value' => '%' . $query_string . '%',
                'operand' => 'LIKE',
            ],
        ],
    ];
     
    return elgg_list_entities_from_metadata($options);

    The search might be less flexible with regards to the search term appearance in the metadata. But if you would write your own search hook for including the "brief" metadata I think it would be basically the same query code necessary (or in other words the same effort to write the query - I've not written that many custom mysql queries myself so far so I'm not sure).

  • Maybe helpful: you could look into start.php of the FAQ plugin (https://elgg.org/plugins/869600). There's the faq_search_hook() plugin hook function that checks some metadata of FAQ entities for matches of the search tags.  I guess you could take this function as a starting point and modify it according to your needs. Also look at the init function in start.php  to see how the plugin hook function is registered and how the entity with subtype "faq" (in this case) is registed to be included in the search.

  • Thanks for the information and code examples!

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking