cancel
Showing results for 
Search instead for 
Did you mean: 

Find documents whose title contains specific String

huijzer
Champ in-the-making
Champ in-the-making
Hi,

I am writing a Freemarker template that will show all documents whose title contains a specific string, for instance 'xyz'.

I tried several things, but have not had any succes. My current code is:


<table cellpadding="3">

    <tr>
        <th align="left">Name</th>
        <th align="left">Title</th>
        <th align="left">Size</th>
        <th align="left">Display Path</th>
    </tr>

    <#list companyhome.childrenByXPath[".//*[contains('xyz')]"] as child>
        <#if child.isDocument>
            <tr>
                <td>${child.properties.name}</td>
                <td>${child.properties.title}</td>
                <td>${child.size} bytes</td>
                <td>${child.displayPath}</td>
            </tr>
        </#if>
    </#list>

</table>

This not only returns documents with 'xyz' in the title, but also with that string in it's name or contents.

Does anyone know how to retrieve only documents with 'xyz' in its title in a Freemarker template?

Thanks in advance,

Arjan Huijzer
4 REPLIES 4

kevinr
Star Contributor
Star Contributor
The XPath syntax for searching attributes is not correct, but anyway I suggest you use a Lucene search to find the documents you want - otherwise the XPath engine will have to walk ALL nodes in the repo one by one to match them! That would not be good for performance! Smiley Wink You can use lucene searches which are much faster as they use the index files, like this:

companyhome.childrenByLuceneSearch["TEXT:alfresco"]
will find all documents with the text 'alfresco' in them. Since you want to search on the title attribute, you want something like this:

companyhome.childrenByLuceneSearch["@cm\\:title:xyz"]

See here for info on Lucene search syntax:
http://wiki.alfresco.com/wiki/Search#Finding_nodes_by_text_property_values

Note the extra escaping required to escape the '\' character for FreeMarker.

Hope this helps,

Kevin

huijzer
Champ in-the-making
Champ in-the-making
Thanks, that worked!!

I just ran into another small problem. If my search string is not 'xyz' but a string containing an underscore, the wildcard functionality does not work correctly.

So the following does not seem to work:


companyhome.childrenByLuceneSearch["@cm\\:title:*test_report*"]

Thanks for your help!

Arjan Huijzer

kevinr
Star Contributor
Star Contributor
Good to hear that worked. The underscore character will have been tokenized out by the Lucene parser - and converted into two words 'test' and 'report' - so you need to do the following phrase style search using quotes:

companyhome.childrenByLuceneSearch["@cm\\:title:\"test_report\""]
Note that you cannot currently use wildcards in combination with phrase searches - that will be added in a later version.

Thanks,

Kevin

huijzer
Champ in-the-making
Champ in-the-making
Kevin,

Thanks for your explanation. It's all clear to me now.
Looking forward to those wildcards with search phrases  Smiley Happy

Arjan