Kentico filter duplicates in search

橙三吉。 提交于 2020-07-16 09:40:52

问题


I have the following script to return search results of linked pages. I need to filter out the duplicates of the alias pages, as well as sort the results alphabetically. Currently, it returns both main page and alias page and does not sort them.

<script runat="server">
    bool hasDegree;
    bool hasCertificate;
    bool hasLetter;
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        hasDegree = ValidationHelper.GetBoolean(CMS.DocumentEngine.DocumentHelper
           .GetDocuments("FCC.Credential").Path(Eval<string>("NodeAliasPath"), CMS.DocumentEngine.PathTypeEnum.Children)
           .Published().Where("CredentialType = 'D'").Count > 0, false);
           
        hasCertificate = ValidationHelper.GetBoolean(CMS.DocumentEngine.DocumentHelper
           .GetDocuments("FCC.Credential").Path(Eval<string>("NodeAliasPath"), CMS.DocumentEngine.PathTypeEnum.Children)
           .Published().Where("CredentialType = 'C'").Count > 0, false);
           
        hasLetter = ValidationHelper.GetBoolean(CMS.DocumentEngine.DocumentHelper
           .GetDocuments("FCC.Credential").Path(Eval<string>("NodeAliasPath"), CMS.DocumentEngine.PathTypeEnum.Children)
           .Published().Where("CredentialType = 'L'").Count > 0, false);         
    }

    protected string imageFilePath(string cssClass, string baseImageName)
    {
        string fileResolution = (cssClass == "desktop") ? "320x200" : "180x180";

        if (baseImageName.Trim().ToLower().EndsWith(".mp4"))
        {
            string baseName = baseImageName.Trim();
            baseName = baseName.Substring(0, baseName.Length - 4);
            return "/FCC/media/ProgramsOfStudy/Video/" + baseName + "_" + fileResolution  + ".jpg";
        }

        return "/FCC/media/ProgramsOfStudy/" + fileResolution + "_" + baseImageName;
    }
</script>

Also found this on page template called: RepeaterWithCustomQuery

WITH FOLDER_QUERY AS
(
    SELECT NODEID, NODEPARENTID FROM VIEW_CMS_TREE_JOINED
    WHERE CLASSNAME = 'FCC.CREDENTIALFOLDER'
),
CREDENTIAL_QUERY AS
(
    SELECT CREDENTIALTYPE, NODEPARENTID FROM FCC_CREDENTIAL
    JOIN VIEW_CMS_TREE_JOINED
        ON [CREDENTIALID] = DOCUMENTFOREIGNKEYVALUE
    WHERE CLASSNAME = 'FCC.CREDENTIAL'
    -- IsPublished 
    AND ([DocumentCanBePublished] = 1 AND ([DocumentPublishFrom] IS NULL OR [DocumentPublishFrom] <= GETDATE()) AND ([DocumentPublishTo] IS NULL OR [DocumentPublishTo] >= GETDATE()))
),
CP_QUERY AS
(
  SELECT CREDENTIALTYPE, FOLDER_QUERY.NODEPARENTID
  FROM FOLDER_QUERY 
  JOIN CREDENTIAL_QUERY 
    ON FOLDER_QUERY.NODEID = CREDENTIAL_QUERY.NODEPARENTID
),
PROGRAM_QUERY AS
(
    SELECT
    (SELECT COUNT(*) FROM CP_QUERY WHERE CP_QUERY.NODEPARENTID = NODEID AND CP_QUERY.CREDENTIALTYPE = 'C') as Certificate,
    (SELECT COUNT(*) FROM CP_QUERY WHERE CP_QUERY.NODEPARENTID = NODEID AND CP_QUERY.CREDENTIALTYPE = 'L') as Letter,
    (SELECT COUNT(*) FROM CP_QUERY WHERE CP_QUERY.NODEPARENTID = NODEID AND (CP_QUERY.CREDENTIALTYPE = 'C' OR CP_QUERY.CREDENTIALTYPE = 'L')) as CertAndLetter,
    (SELECT COUNT(*) FROM CP_QUERY WHERE CP_QUERY.NODEPARENTID = NODEID AND CP_QUERY.CREDENTIALTYPE = 'D') as Degree,
    0 as CEWD,
    NodeParentID,
    DocumentName,
    ClassName,
    BaseImageName,
    ImageAltText,
    NodeAliasPath,
    DocumentUrlPath,
    DocumentCulture,
    Description
    FROM [FCC_PROGRAM] JOIN VIEW_CMS_TREE_JOINED
        ON [PROGRAMID] = DOCUMENTFOREIGNKEYVALUE
    WHERE CLASSNAME = 'FCC.PROGRAM'
        -- IsPublished 
    AND ([DocumentCanBePublished] = 1 AND ([DocumentPublishFrom] IS NULL OR [DocumentPublishFrom] <= GETDATE()) AND ([DocumentPublishTo] IS NULL OR [DocumentPublishTo] >= GETDATE()))
),
CEWD_QUERY AS
(
    SELECT
        0 AS Certificate,
        0 AS Letter,
        0 AS CertAndLetter,
        0 AS Degree,
        1 AS CEWD,
        NodeParentID,
        DocumentName,
        ClassName,
        BaseImageName,
        ImageAltText,
        NodeAliasPath,
        DocumentUrlPath,
        DocumentCulture,
        Description
    FROM [FCC_CEWDPROGRAM] JOIN VIEW_CMS_TREE_JOINED
        ON [CEWDPROGRAMID] = DOCUMENTFOREIGNKEYVALUE
    WHERE CLASSNAME = 'FCC.CEWDPROGRAM'
        -- IsPublished 
    AND ([DocumentCanBePublished] = 1 AND ([DocumentPublishFrom] IS NULL OR [DocumentPublishFrom] <= GETDATE()) AND ([DocumentPublishTo] IS NULL OR [DocumentPublishTo] >= GETDATE()))
),
COMBINED_QUERY as
(
    SELECT * FROM PROGRAM_QUERY
    UNION 
    SELECT * FROM CEWD_QUERY
)

SELECT * FROM COMBINED_QUERY
WHERE ##WHERE##

回答1:


In your DocumentQuery, add .FilterDuplicates() and it should remove those duplicate records for you. Keep in mind it will NOT bring back Linked Documents, only the main document.

*** UPDATE *** Based on updated question, the query that you updated your question with looks like a Kentico generated query. I'm not sure why it was created but I'd set that repeaters visibility to false and add a new repeater on the page and set the properties as you see fit. Then update the WHERE conditions as needed.



来源:https://stackoverflow.com/questions/62800289/kentico-filter-duplicates-in-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!