cancel
Showing results for 
Search instead for 
Did you mean: 

Alfresco lucene query using Javascript

informatica4plu
Champ in-the-making
Champ in-the-making

Hello,

I'm trying to create a job in Alfresco that search documents created between a range of dates and send it automatically.

From schedule-action-services-context.xml, I'm calling this Javascript file that tries to do a lucene query and send results using mail but it doesn't work. In Alfresco log, does not appear any error and no email is send.

var nombredoc = document.properties["{http://www.alfresco.org/model/content/1.0}name"];

var finicio = "2017\-08\-01T00:00:00";
var ffin = "2017\-08\-31T00:00:00";
var query = "@cm\\:created:[" + finicio + " TO " + ffin +"]";

var documentos = search.luceneSearch(query);
var log = "";
for (var i=0; i<documentos.length; i++)
{
   log += "Nombre: " + documentos[i].name + "\tRuta: " + documentos[i].displayPath + "\r\n";
}

var mail = actions.create("mail");
mail.parameters.html = "html";
mail.parameters.from = "informatica@mydomain.com";
mail.parameters.to = "informatica@mydomain.com";
mail.parameters.subject = "Documento: " + nombredoc + " - " + log;
mail.execute(document);

I've tried with simple query like "TEXT:contracts"and works fine, but with date ranges I've got a lot of problems.

Regards

1 REPLY 1

afaust
Legendary Innovator
Legendary Innovator

Generally speaking you should avoid making Lucene search queries. Lucene is a low-level query language that was sort-of standard in old versions of Alfresco, but since then (at least for last 4-5 years) it has switched over to becoming a legacy language only kept for backwards compatibility. At the moment, only Alfresco FTS and CMIS query languages are fully endorsed as reliable / standard query languages.

On the JavaScript API this means you should no longer call search.luceneSearch() and instead default to search.query() (where you can specify the query language to be used in the parameter object, by passing "fts-alfresco" as the language property).

With reagards to your range query, it looks like you are missing the timezone indicator in the ISO8601 date format, as well as the milliseconds segment and (potentially) the escaping of the colon characters. You are also only using \- when escaping the dash in the date expression where it should probably be \\- (like your escaping of the colon in the Lucene field selector). With Alfresco FTS I usually simply enclose the entire ISO8601 date expression in double quotes and thus do not have to deal with escaping special characters.