cancel
Showing results for 
Search instead for 
Did you mean: 

How to show the last N modified documents?

jzulu2000
Champ in-the-making
Champ in-the-making
Hi

I have a lucene search that returns about 15 elements, but I only want to show the last 3 modifieds from that list.
In a script I coded as follows
var totalResults = search.luceneSearch("(TEXT:***)");
model.resultset = totalResults;
The freemarker template does:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<#list resultset as doc>
<item>${doc.name}</item>
</#list>
</list>

I know how to sort by name, using <#list resultset?sort_by("name") as doc>, but it seems like the resultset objects doesn't contain the modified date field to sort by.

Is there any way to sort the resultset by modified date?

After ordering by modified date, I will do a reverse and take only 3 elements.

Thanks
5 REPLIES 5

jzulu2000
Champ in-the-making
Champ in-the-making
I've investigated deeply, and I think there's no easy way of doing this.

The first I tried was to sort by modified field using <#list resultset?sort_by('modified') as doc>, but it didn't work; I think this is because org.alfresco.repo.template.TemplateNode doesn't contain the 'modified' property; I made a debug to the FreeMarker engine, and I realized that it uses java.beans.PropertyDescriptor to find out what method it has to execute when the sort_by parameter is sent, and there's no PropertyDescriptor for this field.. obviously!

Another solution that came to me, was to use <#list resultset?sort_by('aspects.auditable.modified') as doc>, because the modified date is in the auditable aspect of the document, but FreeMarker sort_by doesn't support this. I think it should be a great functionallity to add to the FreeMarker template engine; allow FreeMarker to sort by nesting values.

I thing the only way of making alfresco allow sorting by modified date, is adding the 'modified' field (or just a getter and setter bridge to the ) to the TemplateNode to get the modified date.

Maybe making a custom TemplateNode object and making alfresco to use this one solves my problem… but I have to leave this like this. Maybe someone tries to solve this problem and contributes.

Any other choices?

Thanks

cbosdonnat
Champ in-the-making
Champ in-the-making
Hi,

I think that you could sort much easier in the Javascript code. Here is the start of your javascript sorting the results… I've let you the pleasure to write the sorting algo.


function insertByDate(sortedResults, toAdd) {

    // insert the element in the array at the right place
}

var totalResults = search.luceneSearch("(TEXT:***)");

var sortedResults = [];

for (i = 0; i < totalResults.length; i++) {
    var result = totalResults[i];
    sortedResults = insertByDate(sortedResults, result);
}

model.resultset = sortedResults;

HTH

jzulu2000
Champ in-the-making
Champ in-the-making
It was easier than I though…

I just replaced
var totalResults = search.luceneSearch("(TEXT:***)");
model.resultset = sortedResults;
by
var totalResults = search.luceneSearch("(TEXT:***)");
model.resultset = totalResults.sort(sortModifiedDesc);

function sortModifiedDesc(thisObject, thatObject) {
   var thisValue = thisObject.properties["modified"];
   var thatValue = thatObject.properties["modified"];

   if (thisValue > thatValue) {
      return -1;
   } else if (thisValue < thatValue) {
      return 1;
   }
   return 0;
}

The sortModifiedDesc "handler" sent to the javascript sort method implements the comparisson, and it can access to object variables, including modified date!.

bruna
Champ in-the-making
Champ in-the-making
Hi,
to sort by modified property try ?sort_by(["properties","modified"]).

Sample:

<#list documents?sort_by(["properties","modified"]) as child>
  {
    "name" : "${child.name}",
    "link" : "${url.context}${child.url}",
    "icon" : "${child.icon16}",
    "modified" : "${child.properties.modified?string("yyyy-MM-dd")}"
  }
  <#if child_has_next>,</#if>
</#list>

The problem is that you cannot decide if the sort is ascending or descending.

Byez

mikeh
Star Contributor
Star Contributor
Sure you can - append ?reverse to the statement with ?sort_by in.

Mike