Alphabetical Sort on Lucene Query

26 views
Skip to first unread message

Rafay Mudassar

unread,
Dec 1, 2022, 4:19:29 PM12/1/22
to dotCMS User Group
I am trying to sort a list of titles alphabetically using this query:

#foreach($bach in $dotcontent.pull("+contentType:ProgramsOfStudy +(ProgramsOfStudy.type:*UG*)",0, "modDate desc"))
#if($bach.academicdepartment.title == $department.title)

<a href="/${bach.printurl}">$bach.title</a> <br>
#end
#end

I can sort the titles using "modDate desc" but when I try to sort it using "ProgramsOfStudy.title desc" I do not get the expected result.

Any ideas on what I could be doing wrong? Is the sort not applicable to data-type String?

Thanks,
Rafay

Nathan Keiter

unread,
Dec 1, 2022, 4:29:15 PM12/1/22
to dot...@googlegroups.com
I believe for sort functions to work, the field in question must be indexed.


This is a checkbox on the content type field definition.

Nathan I. Keiter | Network Applications Programmer
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993
https://www.gettysburg.edu<https://www.gettysburg.edu/>
________________________________
From: dot...@googlegroups.com <dot...@googlegroups.com> on behalf of Rafay Mudassar <armud...@maryu.marywood.edu>
Sent: Thursday, December 1, 2022 3:14 PM
To: dotCMS User Group
Subject: [dotcms] Alphabetical Sort on Lucene Query

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
________________________________
--
http://dotcms.com<https://linkprotect.cudasvc.com/url?a=http%3a%2f%2fdotcms.com&c=E,1,7PuNL0_RHEtN53ciOqduaqI9viiIcP8UA13teUfH6NFyq3YMxw21j6yBKOluvYOeoTFXtmzFGvNrosr2v1cwqlSMLDAkWthY6VU5sgazN8g,&typo=1> - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com<mailto:dotcms+un...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/5387c47b-aa94-465f-b9fb-99c3fe2cc114n%40googlegroups.com<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fgroups.google.com%2fd%2fmsgid%2fdotcms%2f5387c47b-aa94-465f-b9fb-99c3fe2cc114n%2540googlegroups.com%3futm_medium%3demail%26utm_source%3dfooter&c=E,1,BYD_0JkcK9VyrRzKv-_oSDrTvv7PQiD7JVa7OI04WjSB-UXd6twOa-1j-9BIXgrY9u1lODL8uTZkSRkoLTQNHO4DfJpzDz1Rjnx-2CKyRmww6oq0JugDVCJEFxR4&typo=1>.

Jameson Mauro

unread,
Dec 1, 2022, 4:52:42 PM12/1/22
to dotCMS User Group
Some relevant details here: https://www.dotcms.com/docs/latest/content-search-syntax#SystemFields

title is similar to moddate in that they're always considered system properties in all Content Types, and therefore should always be automatically indexed. They can also be referenced without the Content Type variable prefix.

I.e., instead of ProgramsOfStudy.title desc, you should be able to use title desc on its own.

Lastly, sorting by an indexed field can sometimes yield unpredictable results. In that case, the recommended solution is to use the raw field, instead.

So, see if using title_dotraw desc as your sort argument produces the desired results.

Mark Pitely

unread,
Dec 5, 2022, 10:41:29 AM12/5/22
to dot...@googlegroups.com
You have to be very careful with the sorting field, if for some reason there is a typo or error, it just defaults to moddate. So it may seem as if you are sorting but not. 
It does not throw an error. 
The field you are sorting on must have the 'indexed' option set in the structure definition - and then you MUST reindex that before you will see any results. 
Lastly, the sort is clumsy. It is not case insensitive, which is why Jameson suggested you use the raw field. 
Basically, if you can't get it to work as you need it to, you should use the $sorter.sort tool...
Which will give you fine control over your sorting (and does not require the index field being set, it just works with the raw content).
You would do something like:
#set ($bachunsorted= $dotcontent.pull("+contentType:ProgramsOfStudy +(ProgramsOfStudy.type:*UG*)",0, "modDate desc"))
#foreach ($bach in $sorter.sort($bachunsorted,"title"))
...
#end




Mark Pitely
Albright College

--
http://dotcms.com - Open Source Java Content Management

---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/ba09ddf8-4b9b-429b-815b-ce90ccff544dn%40googlegroups.com.

Rafay Mudassar

unread,
Dec 7, 2022, 2:55:36 PM12/7/22
to dotCMS User Group
Thank you all for your help.
Mark, your suggestion worked. 

Best,
Rafay

Reply all
Reply to author
Forward
0 new messages