Create an effective tag tree to optimize content retrieving

[tab:Actions]

Creating an effective tag tree is an essential condition to allow THRON maximizing the value of your content and moreover to allow you a faster retrieving of content through the available search methods. This article is meant to provide you with a few guidelines to create an effective tag tree.

Let's suppose you have a company producing different types of footwear, and among them you have both open and closed sandals. Let's compare these two tag trees:

If you build the first tag tree, you will be able to operate with all the search methods illustrated in this article; this kind of classification is the most effective since each tag uniquely identifies each type of product: there are no repetitions of words between tags of the same node.

 

The second tag tree might be used when you want to create your classification starting from the different types of your products instead of starting from the characteristics of your content. This type of classification is not optimal because there is a concept (Sandals) which is nested both in a parent and in a child tag. This problem will emerge more clearly when you perform a text search using the "EXACT_MATCH" criteria: if you search for the keyword "sandals" you will retrieve both the content tagged "Sandals" and those tagged "Closed sandals"; you will have no chance to perform a text search to retrieve only those content about "Sandals".

 

This happens because the text search is built to perform on texts, where typically you don't know a priori what you are going to obtain. To obviate this problem we recommend you to use the search method provided in the code samples of this article. Otherwise, we suggest you to use the searchkey metadata, specifically built to uniquely identify different "categories" of content. Further information can be found in this article.

 

CONFIGURATION OF THE SCRIPT

Information to be included within the script are:

  • Parameters to be used to search on tags:
    • $tokenID = ""; token to be used to perform the search (use login methods to obtain it).
    • $client_id = ""; domain name used to access your THRON.
    • $idClassification = ""; classificationId used to perform the search.
    • $numberOfresults=50; number of results you want to retrieve.
    • $offset=0;
    • $lang ="IT"; language to be used to perform the search.
    • $textSearch=""; the key word to be used to perform the search
  • Parameters to be used to search on content:
    • $itagOperation="OR"; used to retrieve content with at least one of the provided tags.
  • Functions used by the script:
    • do_dump; used to print results on screen.
    • contentFindByProperties; used to perform the search on the platform.
    • getListItagTHRON used to retrieve itags from platform.
  • Main actions:
    • search for the itags related to the request classification and containing the provided keyword.
    • verification and storage of the ids of the itags with the name that matches the keyword.
    • search for the content with the identified itags.
  • Output of the results:
    Print to video of the first 50 results matching the request search.[/tab]

[tab:Code Samples]

This is a script that can be used to retrieve all content linked to a specific tag:

 

<?php
error_reporting(E_ALL);
/*#########################*/
//CONFIGURATION
/*#########################*/
ini_set('display_errors', 'On');
// extract the token form the params
$tokenID          = "";
// setup client_id
$client_id        = '';
//id classification THRON
$idClassification = '';
// number of results is the number of contents    
$numberOfresults  = 50;
$offset           = 0;
$lang             = "IT";
$textSearch       = "<TEXT>";

// param for search content 
$itagOperation = "OR"; //AND or OR  1

/*#########################*/
//UTILITY FUNCTION
/*#########################*/
function do_dump(&$var, $var_name = NULL, $indent = NULL, $reference = NULL)
{
    $do_dump_indent = "<span style='color:#666666;'>|</span> &nbsp;&nbsp; ";
    $reference      = $reference . $var_name;
    $keyvar         = 'the_do_dump_recursion_protection_scheme';
    $keyname        = 'referenced_object_name';
    
    // So this is always visible and always left justified and readable
    echo "<div style='text-align:left; background-color:white; font: 100% monospace; color:black;'>";
    
    if (is_array($var) && isset($var[$keyvar])) {
        $real_var =& $var[$keyvar];
        $real_name =& $var[$keyname];
        $type = ucfirst(gettype($real_var));
        echo "$indent$var_name <span style='color:#666666'>$type</span> = <span style='color:#e87800;'>&amp;$real_name</span><br>";
    } else {
        $var = array(
            $keyvar => $var,
            $keyname => $reference
        );
        $avar =& $var[$keyvar];
        
        $type = ucfirst(gettype($avar));
        if ($type == "String")
            $type_color = "<span style='color:green'>";
        elseif ($type == "Integer")
            $type_color = "<span style='color:red'>";
        elseif ($type == "Double") {
            $type_color = "<span style='color:#0099c5'>";
            $type       = "Float";
        } elseif ($type == "Boolean")
            $type_color = "<span style='color:#92008d'>";
        elseif ($type == "NULL")
            $type_color = "<span style='color:black'>";
        
        if (is_array($avar)) {
            $count = count($avar);
            echo "$indent" . ($var_name ? "$var_name => " : "") . "<span style='color:#666666'>$type ($count)</span><br>$indent(<br>";
            $keys = array_keys($avar);
            foreach ($keys as $name) {
                $value =& $avar[$name];
                do_dump($value, "['$name']", $indent . $do_dump_indent, $reference);
            }
            echo "$indent)<br>";
        } elseif (is_object($avar)) {
            echo "$indent$var_name <span style='color:#666666'>$type</span><br>$indent(<br>";
            foreach ($avar as $name => $value)
                do_dump($value, "$name", $indent . $do_dump_indent, $reference);
            echo "$indent)<br>";
        } elseif (is_int($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . strlen($avar) . ")</span> $type_color" . htmlentities($avar) . "</span><br>";
        elseif (is_string($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . strlen($avar) . ")</span> $type_color\"" . htmlentities($avar) . "\"</span><br>";
        elseif (is_float($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . strlen($avar) . ")</span> $type_color" . htmlentities($avar) . "</span><br>";
        elseif (is_bool($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . strlen($avar) . ")</span> $type_color" . ($avar == 1 ? "TRUE" : "FALSE") . "</span><br>";
        elseif (is_null($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . strlen($avar) . ")</span> {$type_color}NULL</span><br>";
        else
            echo "$indent$var_name = <span style='color:#666666'>$type(" . strlen($avar) . ")</span> " . htmlentities($avar) . "<br>";
        
        $var = $var[$keyvar];
    }
    
    echo "</div>";
}
/*
find contente by Itag
*/
function contentFindByProperties($clientidTHRON, $tokenIDTHRON, $itagOperationTHRON, $arrayItagsClassificationTHRON, $offsetTHRON, $numberOfresultsTHRON)
{
    // HTTP endpoint
    $find_endpoint = 'http://' . $clientidTHRON . '-view.thron.com/api/xcontents/resources/content/findByProperties';
    // Execute http calls with PHP curl
    $request       = curl_init($find_endpoint);
    
    // setup some configuration for curl
    $parameters = array(
        CURLOPT_URL => $find_endpoint,
        CURLOPT_VERBOSE => true,
        CURLOPT_POST => true,
        CURLOPT_HEADER => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLINFO_HEADER_OUT => true
    );
    
    // apply them on the request
    curl_setopt_array($request, $parameters);
    
    // define the request body
    // see the documentation for more information (JContent)
    $body = array(
        // an usually present key
        "client" => array(
            "clientId" => $clientidTHRON
        ),
        "criteria" => array(
            "itagOp" => array(
                "operation" => $itagOperationTHRON,
                "itags" => $arrayItagsClassificationTHRON
            )
        ),
        // pagination attributes:
        // offset is the pointer of the
        // startin point of the window 
        "offset" => $offsetTHRON,
        //return itag id
        "contentFieldOption" => array(
            "returnItags" => true
            
        ),
        // number of results is the number of contents
        // foreach window requested
        "numberOfresults" => $numberOfresultsTHRON
    );
    
    // apply the body to the request
    curl_setopt($request, CURLOPT_POSTFIELDS, json_encode($body));
    // apply the body to the request
    curl_setopt($request, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json",
        'X-TOKENID: ' . $tokenIDTHRON
    ));
    
    // send the request
    $response = curl_exec($request);
    
    // extract the response body
    $header_size = curl_getinfo($request, CURLINFO_HEADER_SIZE);
    $header      = substr($response, 0, $header_size);
    $body        = substr($response, $header_size);
    
    // $response = curl_exec($request);
    $request_info = curl_getinfo($request);
    $headers      = substr($response, 0, $request_info['header_size']);
    $resBody      = substr($response, $request_info['header_size']);
    $response     = json_decode($resBody);
    // do_dump($resBody);
    return $response;
}
/*
get list ITAG THRON
*/
function getListItagTHRON($clientidTHRON, $tokenIDTHRON, $idClassificationTHRON, $langTHRON, $textSearchTHRON, $numberOfresultsTHRON, $offsetTHRON)
{
    $url = "http://" . $clientidTHRON . "-view.thron.com/api/xintelligence/resources/itagdefinition/listGet/
" . $clientidTHRON . "/" . $idClassificationTHRON;
    $url .= "?limit=" . $numberOfresultsTHRON . "&offset=" . $offsetTHRON . "&text=" . urlencode($textSearchTHRON) . "&lang=" . $langTHRON;
    
    $header = array(
        'X-TOKENID: ' . $tokenIDTHRON,
        "Content-Type: application/json"
    );
    
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    // curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
    
    $curlRes     = curl_exec($curl);
    //echo $curlRes;
    $curlResInfo = curl_getinfo($curl);
    curl_close($curl);
    
    $resHeadersString = substr($curlRes, 0, $curlResInfo['header_size']);
    $resBody          = substr($curlRes, $curlResInfo['header_size']);
    $response         = json_decode($resBody);
    // do_dump($resBody);
    return $response;
}

/*#########################*/
//main
/*#########################*/
//arra    
$arrayIDItagSearch = array();
do {
    // Get list itag
    $ris = getListItagTHRON($client_id, $tokenID, $idClassification, $lang, $textSearch, $numberOfresults, $offset);
    // search for Id tag
    for ($i = 0; $i < count($ris->items); $i++)
        for ($iname = 0; $iname < count($ris->items[$i]->names); $iname++) {
            if (($ris->items[$i]->names[$iname]->lang == $lang) && (strtolower(trim($ris->items[$i]->names[$iname]->label)) == strtolower(trim($textSearch)))) //search for lang and text 
                {
                // found ITAG
                $arrayIDItagSearch[] = array(
                    "id" => $ris->items[$i]->id,
                    "classificationId" => $idClassification
                );
                $iname               = count($ris->items[$i]->names);
            }
            
        }
    
    $offset += $numberOfresults;
    
} while ($offset < $ris->totalResults);
//search content 
// execute search  for tagID with find
$risContet = contentFindByProperties($client_id, $tokenID, $itagOperation, $arrayIDItagSearch, 0, 30);


?>

<!DOCTYPE html>
    <head>
        <meta charset="utf-8">
        <title>THRON API example with PHP</title>

    </head>
    <body>
        
        <h1>Example</h1>   
       <h2>List itag found :<?php
echo $textSearch;
?> </h2>    
       <p><b><pre><?php
do_dump($arrayIDItagSearch);
?></pre></b></p>
        <h2>List Content found : </h2>    
       <p><b><pre><?php
do_dump($risContet);
?></pre></b></p>

    </body>
</html>

[/tab]

Was this article helpful?
0 out of 0 found this helpful

Have any question?

Open a ticket
Comments