Tridion 2009 SP1 Broker not returning results
Asked Answered
L

5

7

I am having trouble loading a Dynamic Component Presentation from the Broker based on a fairly simple query like the below, where I am trying to load the Component based on being tagged with a specific Keyword:

    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;
            }
        }

    }

I can see the item in the ITEMS_CATEGORIES_AND_KEYWORDS table in the Broker database with the keyword I expect and I can load the CP manually if I comment out the query and hardcode the TCM ID in.

I have made sure the Category is published and that all the variables' values are correct.

I have ensured the Keyword has a value and a key set to the appropriate value.

What else can I check?

Liquidate answered 24/5, 2012 at 13:0 Comment(0)
L
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?

Liquidate answered 29/5, 2012 at 7:51 Comment(0)
J
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.

Juror answered 24/5, 2012 at 20:39 Comment(1)
I tried this and discovered I wasn't hitting the database at all. I think I am referencing the wrong DLLs but can't figure out which are the right ones.. I have got it working with the obsolete filtering API but not happy.Liquidate
K
2

Have you tried using the SetCriteria method on the query? For example:

query.SetCriteria(multipleCombinedFacetCriteria);
String[] itemURIS = query.ExecuteQuery();
Krongold answered 24/5, 2012 at 13:34 Comment(0)
B
2

When looking in Java API I can see this overload:

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?

Bathos answered 24/5, 2012 at 20:21 Comment(4)
I did wonder this as well but it didn't help. After some testing and SQL Profiling with the Technical Director we figured out the code is not evening hitting the database. I've had to use the Obsolete filter API to get any results at all.Liquidate
Have you migrated from an older version of Tridion? Maybe there are still some old Jar files located in the lib directories?Bathos
No it's a fresh install of 2009 SP1 with the Content Delivery cumulative hotfix appliedLiquidate
Can you get Broker items based on schema_ID, or something else basic? So, what happens when you remove the taxonomyFilterBathos
L
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?

Liquidate answered 29/5, 2012 at 7:51 Comment(0)
S
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

Stroud answered 12/6, 2012 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.