Message Boards

Liferay 7.2 - How to make a search query between dates?

Fabio Carvalho, modified 3 Years ago.

Liferay 7.2 - How to make a search query between dates?

Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts

Hello,

I have been using DynamicQuery to get some JournalArticles. I need to get the JournalArticles between some dates, and for that I am doing something like this:

DynamicQuery journalArticleQuery = JournalArticleLocalServiceUtil.dynamicQuery();
journalArticleQuery.add(PropertyFactoryUtil.forName(Field.GROUP_ID).eq(myGroup.getGroupId()));
journalArticleQuery.add(PropertyFactoryUtil.forName(Field.STATUS).eq(WorkflowConstants.STATUS_APPROVED));
journalArticleQuery.add(PropertyFactoryUtil.forName("DDMStructureKey").eq("MY_STRUCTURE_KEY"));
journalArticleQuery.add(PropertyFactoryUtil.forName(Field.DISPLAY_DATE).ge(new Timestamp(startDate)));
journalArticleQuery.add(PropertyFactoryUtil.forName(Field.DISPLAY_DATE).le(new Timestamp(endDate)));
journalArticleQuery.addOrder(OrderFactoryUtil.desc(Field.DISPLAY_DATE));

List<JournalArticle> journalArticles = JournalArticleLocalServiceUtil.dynamicQuery(journalArticleQuery);

This works. But it returns also all the versions os the same JournalArticle. I iterate the List and remove all the duplicates to have only one version.

I was expecting to remove this later iteration, and I know that I can use theĀ JournalArticleLocalServiceUtil search method to get a Hits with all the JournalArticles referring to that query. I know that this only returns the last version of the JournalArticle, so I was hoping to change my DynamicQuery with the Hits. The problem is, I don't know how to filter my JournalArticles based on date with the Hits query.

By checking the method, I can see that the method accepts a param called "params"

@param params the finder parameters (optionally <code>null</code>)

But can I somehow use this "params" to only get JournalArticles between a date? I have been searching for a while, but can not find anything.