7

我在从代理加载动态组件演示文稿时遇到问题,该查询基于如下所示的相当简单的查询,我试图根据使用特定关键字标记来加载组件:

    private string GetComponentPresentations()
    {
        Logger.Log.Info("Entered GetComponentPresentations");
        var publicationCriteria = new PublicationCriteria(_publicationId);

        int schemaId = int.Parse(SchemaId.Split('-')[1]);

        // Is it the correct content type (Schema)
        var isSpecifedSchema = new ItemSchemaCriteria(schemaId);

        // Type of the item is 16 (Component).
        var isComponent = new ItemTypeCriteria(16);

        // All of the above conditions must be true
        Criteria isCorrectComponent = CriteriaFactory.And(isSpecifedSchema, isComponent);

        var publicationAndIsComponent = CriteriaFactory.And(publicationCriteria, isCorrectComponent);

        //Only get components tagged with the specified keyword
        var keywordCriteria = new KeywordCriteria(_productsCategoryTcmId, ProductFilter, Criteria.Equal);

        //Only get Components of the correct type from the correct publication
        Criteria fullCriteria = CriteriaFactory.And(publicationAndIsComponent, keywordCriteria);


        using (var query = new Query(fullCriteria))
        {
            string[] results = query.ExecuteQuery();
            using (var cpf = new ComponentPresentationFactory(_publicationId))
            {
                if(results != null)
                {
                    var resultString = new StringBuilder();

                    foreach (string componentTcmId in results)
                    {
                        Logger.Log.Info("Looping over results");

                        int componentId = int.Parse(componentTcmId.Split('-')[1]);

                        int templateId = int.Parse(TemplateId.Split('-')[1]);

                        ComponentPresentation cp = cpf.GetComponentPresentation(componentId, templateId);

                        if (cp != null && !string.IsNullOrEmpty(cp.Content))
                        {
                            resultString.Append(cp.Content);
                            Logger.Log.InfoFormat("Appended Content {0}",cp.Content);
                        }
                    }

                    Logger.Log.Info("Returning");
                    return resultString.ToString();
                }

                Logger.Log.Info("Results was null.");
                return string.Empty;
            }
        }

    }

我可以在 Broker 数据库的 ITEMS_CATEGORIES_AND_KEYWORDS 表中看到带有我期望的关键字的项目,如果我注释掉查询并硬编码 TCM ID,我可以手动加载 CP。

我已确保已发布类别并且所有变量的值都是正确的。

我已确保关键字有一个值和一个设置为适当值的键。

我还能检查什么?

4

5 回答 5

4

I'd suggest removing each of the criteria from the query one by one, and checking what results get returned for each.

Another thing to check is that you are using the API that you think you are. Tridion has two very similar looking APIs for Broker queries. Double check that you are linking to the correct assemblies.

于 2012-05-24T20:39:29.663 回答
2

您是否尝试过在查询中使用 SetCriteria 方法?例如:

query.SetCriteria(multipleCombinedFacetCriteria);
String[] itemURIS = query.ExecuteQuery();
于 2012-05-24T13:34:20.357 回答
2

在查看 Java API 时,我可以看到这个重载:

KeywordCriteria(java.lang.String categoryName, java.lang.String keyword, FieldOperator operator) 

Does _productsCategoryTcmId maybe just need to be the name of Category instead of the URI?

于 2012-05-24T20:21:00.600 回答
2

I have managed to get this working using the following code:

    private string GetComponentPresentationsUsingFilter()
    {
        //RSL: Had to use the obsolete filtering API because could not get anything back from the Broker.
        var filter = new SearchFilter("tcm:0-" + _publicationId + "-1");
        var query = new Query();

        string schemaId = SchemaId.Split('-')[1];
        query.AddCriteria("schema", "=", schemaId);
        query.AddCustomMetaQuery(string.Format("KEY_NAME = 'product' AND CAST(KEY_STRING_VALUE as nvarchar(100))  = '{0}'", ProductFilter));
        string[] results = filter.Match(query, new Sorting("title=asc"), MaxItems);

        if (results == null)
        {
            Logger.Log.Info("Results was null.");
            return string.Empty;
        }

        using (var cpf = new ComponentPresentationFactory(_publicationId))
        {
            var resultString = new StringBuilder();
            Logger.Log.InfoFormat("Got {0} Results", results.Length);

            foreach (string componentTcmId in results)
            {

                int componentId = int.Parse(componentTcmId.Split('-')[1]);
                Logger.Log.InfoFormat("Got componentId as {0}", componentId);

                int templateId = int.Parse(TemplateId.Split('-')[1]);
                Logger.Log.InfoFormat("Got templateId as {0}", templateId);

                ComponentPresentation cp = cpf.GetComponentPresentation(componentId, templateId);

                if (cp != null && !string.IsNullOrEmpty(cp.Content))
                {
                    resultString.Append(cp.Content);
                    Logger.Log.InfoFormat("Appended Content {0}", cp.Content);
                }
            }

            return resultString.ToString();
        }
    }

No idea why I can get results this way but nothing using the Criteria api?

于 2012-05-29T07:51:02.320 回答
2

Have you checked that the Category that you are querying on is published? You will need to do this if you are using the newer 'criteria' mechanism. It always gets me that one!

Thanks, Jonathan

于 2012-06-12T17:50:07.240 回答