Alfresco / Aikau

Aikau UI framework
GNU Lesser General Public License v3.0
81 stars 61 forks source link

Show tag property in search results #850

Closed rderooij closed 8 years ago

rderooij commented 8 years ago

Hi Dave,

Could you please explain me the following. I would like to show the tags in the faceted search result page. I am referring to your blogpost: https://www.alfresco.com/blogs/developer/2015/04/01/adding-views-to-filtered-search/.

I did first a test. This works:

var fctSearchRes = widgetUtils.findObject(model.jsonModel, "id", "FCTSRCH_SEARCH_RESULT");
fctSearchRes.config.widgetsBelow = [ {
    name : "alfresco/renderers/Property",
    config : {
        propertyToRender : "description"
    }
}];

Now I want to add the tags and I did the following:

var fctSearchRes = widgetUtils.findObject(model.jsonModel, "id", "FCTSRCH_SEARCH_RESULT");
fctSearchRes.config.widgetsBelow = [ {
    name : " alfresco/renderers/ReadOnlyTag"
}];

Nothing. Then I did the following:

var fctSearchRes = widgetUtils.findObject(model.jsonModel, "id", "FCTSRCH_SEARCH_RESULT");
fctSearchRes.config.widgetsBelow = [ {
    name : "alfresco/renderers/Property",
    config : {
        propertyToRender : "tags"
    }
}];

I get a number, so I looked at the getRenderedProperty function in alfresco/renderers/Property.js. The following explains why I get a number back.

 } else if (ObjectTypeUtils.isArray(property)) {
            value = property.length;
} 

Is this a bug? Is it not better to add something like the following:

for(var i=0; i<property.length; i++){
   var object = property[i];
   value = value + this.getRenderedProperty(object) + ",";
}

Or is there another way to show the tags properly?

Rick

draperd commented 8 years ago

Hi Rick,

I think the issue here is that the Search API only returns a subset of the full Node metadata for each result (if you inspect the XHR tab in the developer tools for your browser you'll see what I mean). Unlike in the Document Library where all permissions, properties, aspects, actions, etc are returned, the search API only returns enough information to display the data required for the original (non-Aikau search page used prior to 5.0).

When you click on the actions drop-down for a search result you should note that it needs to make an additional XHR request to obtain the full metadata for the node in order to be able to display the actions.

Part of the problem here is that the Tag widget expects to work with tag data as found in the full Node metadata (i.e. node.properties.cm:taggable) and the search API doesn't return this data (just an array tags).

To resolve this issue you will need to update the Search API to be returning full node metadata - or altenatively, raising a feature request for us to create or update the existing Tags widget to make the XHR requests necessary to work in the context of search.

Regards, Dave

shyam2016 commented 8 years ago

@rderooij were you able solve this issue? i also got the same kind of situation where i need to display the category for search results contents. please let me know what you have done, that could help me . Thanks !

rderooij commented 8 years ago

Hi shyam2016,

I did not manage to edit the existing Tags widget. I used the property renderer for displaying the tags.

{
name: "alfresco/renderers/Property",
config: {
   label: msg.get("label.tags"),
   propertyToRender: "tags"
}

Only the property renderer cannot handle arrays correctly. For this to work, I overwrite the getRenderedProperty: function. I have added the following:

} else if (ObjectTypeUtils.isArray(property)) { 
  for(var i=0; i<property.length; i++){
    var object = property[i];
    value = value + this.getRenderedProperty(object) + ",";
  }
  value = value.slice(",",-1);
}

But in your case, you need to make sure that the categories are provided by the search API. Because this is not the case by default.

Hope this is helpful.

Rick

shyam2016 commented 8 years ago

Thank you @rderooij for your response. Could you please help me or provide an example how can I include category property in alfresco search API ?

rderooij commented 8 years ago

Hi shyam2016,

You have to extend the search.get.js. You do this by copying the original file and place this in the following directory.

/extension/templates/webscripts/org/alfresco/slingshot/search/search.get.js

In this script you see the line: model.data = getSearchResults(params);

What you need to do is to enrich the JSON before it will be passed to the model. You can do this by creating a function. For example:

function addMoreItemsToResults(data){ 
    var results = [];
    for (i=0;i<data.items.length;i++) {
        var item = data.items[i];

       // Get the node
        var node = search.findNode(item.nodeRef);

        item["categories"] = "Here you can put your categories";
        item["customProp"] = "Some custom prop";

        results.push(item);
       }
       return ({
           paging: {
               totalRecords: results.length,
               totalRecordsUpper: results.length - failed,
               startIndex: params.startIndex
           },items: results});
} 

Important here is that you need to get the Object node (var node = search.findNode(item.nodeRef)). With the node you can get any value you want. For categories you have to use node.properties["cm:categories"].

Finally, you add your function:

model.data = addMoreItemsToResults(getSearchResults(params));

Good luck!

Rick

VijiSaran commented 8 years ago

Hi rderooij,

I am working in include category property in alfresco search result page. I have followed your steps and now i am getting array length (number). I tried to override the getRenderedProperty method of Properties.js, so i have copied the old code and added the for loop in new file with the following location: "webapps\share\js\aikau\com\pearson\renderers\Properties.js" or "\webapps\share\js\aikau\alfresco\renderers\Properties.js"

But the old file is not getting overridden. Could you please help on this..