diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/ajax/ContentletAjax.java b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/ajax/ContentletAjax.java index 9e8f7c86687b..f7c9454fbbf5 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/ajax/ContentletAjax.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/contentlet/ajax/ContentletAjax.java @@ -10,6 +10,7 @@ import com.dotcms.contenttype.model.type.PageContentType; import com.dotcms.enterprise.FormAJAXProxy; import com.dotcms.keyvalue.model.KeyValue; +import com.dotcms.repackage.com.google.common.base.Preconditions; import com.dotcms.repackage.org.directwebremoting.WebContextFactory; import com.dotcms.util.LogTime; import com.dotmarketing.beans.Host; @@ -130,6 +131,8 @@ */ public class ContentletAjax { + private static String VELOCITY_CODE_TEMPLATE = "#foreach($con in $dotcontent.pull(\"%s\",10,\"%s\"))
%s
#end"; + private static final String CONTENT_TYPES_INODE_SEPARATOR = ","; private java.text.DateFormat modDateFormat = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.SHORT, @@ -578,6 +581,7 @@ public List searchContentletsByUser(List types, String structur Map lastSearchMap = new HashMap(); List relatedIdentifiers = new ArrayList(); + List relationshipFields = new ArrayList(); final StringBuilder relatedQueryByChild = new StringBuilder(); if (UtilMethods.isSet(sess)) { @@ -692,6 +696,7 @@ public List searchContentletsByUser(List types, String structur Collectors.toList()); } + relationshipFields.add(new RelationshipFieldData(fieldName, fieldValue)); continue; } } @@ -970,18 +975,17 @@ else if( fieldbcontentname.startsWith("date") ){ luceneQuery.append(" +working:true"); final String luceneQueryToShow= luceneQuery.toString().replaceAll("\\s+", " "); - //filter related content - if (!relatedIdentifiers.isEmpty()) { - luceneQuery.append(" +identifier:(") - .append(String.join(" OR ", relatedIdentifiers)).append(") "); - } //Executing the query long before = System.currentTimeMillis(); PaginatedArrayList hits = new PaginatedArrayList <>(); long totalHits=0; try{ - hits =(PaginatedArrayList)conAPI.searchIndex(luceneQuery.toString(), perPage, offset, finalSort, currentUser, false); + final String luceneQueryTOElasticSearch = relatedIdentifiers.isEmpty() ? + luceneQuery.toString() : + appendRelatedIdentifierToQuery(luceneQuery.toString(), relatedIdentifiers); + + hits =(PaginatedArrayList)conAPI.searchIndex(luceneQueryTOElasticSearch, perPage, offset, finalSort, currentUser, false); totalHits = hits.getTotalResults(); }catch (Exception pe) { @@ -1055,7 +1059,14 @@ else if( fieldbcontentname.startsWith("date") ){ .replaceAll("\\+working:[a-zA-Z]*","").replaceAll("\\s+", " ").trim(); counters.put("luceneQueryRaw", luceneQueryToShow); - counters.put("luceneQueryFrontend", luceneQueryToShow2.replace("\"","\\${esc.quote}")); + + final String luceneQueryFormatted = luceneQueryToShow2.replace("\"","\\${esc.quote}"); + final String velocityCode = String.format(VELOCITY_CODE_TEMPLATE, + luceneQueryFormatted, + finalSort, + UtilMethods.isSet(relationshipFields) ? getRelationshipVelocityCode(relationshipFields) : "..."); + + counters.put("velocityCode", velocityCode); counters.put("relatedQueryByChild", relatedQueryByChild.length() > 0 ? relatedQueryByChild.toString() : null); counters.put("sortByUF", finalSort); @@ -1087,6 +1098,40 @@ else if( fieldbcontentname.startsWith("date") ){ return results; } + private String appendRelatedIdentifierToQuery(final String luceneQuery, + final List relatedIdentifiers) { + + Preconditions.checkArgument(UtilMethods.isSet(relatedIdentifiers), "relatedIdentifiers can not be empty or null"); + final StringBuffer result = new StringBuffer(luceneQuery); + + return result.append(" +identifier:(") + .append(String.join(" OR ", relatedIdentifiers)).append(") ") + .toString(); + } + + private String getRelationshipVelocityCode(final List relationshipFields) { + final String setCodeTemplate = "" + + "#set( $related_%s = $dotcontent.pullRelatedField( \"$con.identifier\", \"%s\", \"+identifier:%s\") )" + + ""; + final String conditionCodeTemplate = "!$related_%s.isEmpty()"; + final String velocityCodeTemplate = "#if (%s)
" + + "...
" + + "#end"; + + final String setCode = relationshipFields.stream() + .map(relationshipField -> String.format(setCodeTemplate, + relationshipField.fieldName.replaceAll("\\.", StringPool.BLANK), + relationshipField.fieldName, + relationshipField.fieldValue)) + .collect(Collectors.joining("\n")); + + final String conditionCode = relationshipFields.stream() + .map(relationshipField -> String.format(conditionCodeTemplate, relationshipField.fieldName.replaceAll("\\.", StringPool.BLANK))) + .collect(Collectors.joining(" && ")); + + return setCode + "
" + String.format(velocityCodeTemplate, conditionCode); + } + private void addContentMapsToResults(String structureInode, int perPage, User currentUser, Map fieldsMapping, PaginatedArrayList hits, List results, List expiredInodes, final boolean exporting) { @@ -2613,5 +2658,14 @@ private List> findAllLangContentlets(final String contentlet return builder.build(); } + private class RelationshipFieldData { + String fieldName; + String fieldValue; + + public RelationshipFieldData(String fieldName, String fieldValue) { + this.fieldName = fieldName; + this.fieldValue = fieldValue; + } + } } diff --git a/dotCMS/src/main/webapp/html/portlet/ext/contentlet/view_contentlets_js_inc.jsp b/dotCMS/src/main/webapp/html/portlet/ext/contentlet/view_contentlets_js_inc.jsp index 242daa717082..ee449e4c1fea 100644 --- a/dotCMS/src/main/webapp/html/portlet/ext/contentlet/view_contentlets_js_inc.jsp +++ b/dotCMS/src/main/webapp/html/portlet/ext/contentlet/view_contentlets_js_inc.jsp @@ -2554,10 +2554,7 @@ final String calendarEventInode = null!=calendarEventSt ? calendarEventSt.inode( function fillQuery (counters) { <% - String restBaseUrl="http://"+ - APILocator.getHostAPI().find((String)session.getAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID), user, false).getHostname()+ - ((request.getLocalPort()!=80) ? ":"+request.getLocalPort() : "")+ - "/api/content/render/false"; + String restBaseUrl= "/api/content/render/false"; String restBasePostUrl="http://"+ APILocator.getHostAPI().find((String)session.getAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID), user, false).getHostname()+ @@ -2570,7 +2567,7 @@ final String calendarEventInode = null!=calendarEventSt ? calendarEventSt.inode( var encodedQueryRaw = queryRaw.replace(/'/g, "%27").replace(/"/g, "%22"); var queryfield=document.getElementById("luceneQuery"); queryfield.value=queryRaw; - var queryFrontend = counters["luceneQueryFrontend"]; + var velocityCode = counters["velocityCode"]; var relatedQueryByChild = counters["relatedQueryByChild"]; var sortBy = counters["sortByUF"]; var div = document.getElementById("queryResults"); @@ -2587,7 +2584,7 @@ final String calendarEventInode = null!=calendarEventSt ? calendarEventSt.inode( div.innerHTML = "
" + "
<%= LanguageUtil.get(pageContext, "frontend-query") %>
"+ - "
#foreach($con in $dotcontent.pull(\"" + queryFrontend + "\",10,\"" + sortBy + "\"))
...
#end
"; + "
" + velocityCode + "
"; if (relatedQueryByChild == null){ div.innerHTML += "
<%= LanguageUtil.get(pageContext, "The-actual-query-") %>
"+ @@ -2623,13 +2620,6 @@ final String calendarEventInode = null!=calendarEventSt ? calendarEventSt.inode( "
<%= LanguageUtil.get(pageContext, "rest-api-call-urlencoded") %>
"+ "
"+apicall_urlencode+"
"+ - "
REST API: " + - "', '" + encodedQueryRaw + "', '" + sortBy + "')\">API"+ - - "
"+ - - "<%= LanguageUtil.get(pageContext, "Ordered-by") %>: " + sortBy + "
  • <%= LanguageUtil.get(pageContext, "message.contentlet.hint2") %> " + "
  • <%= LanguageUtil.get(pageContext, "message.contentlet.hint3") %> " +