Any good sources for Tridion Taxonomy query examples
Asked Answered
F

1

13

I have a scenario where I have several thousand Component Presentations published that are classified with a specific category/keyword, but the components also have Custom Metadata fields pertaining to define the top n. that will be displayed. Essentially, I need to filter a set of components based on keywords, sort them based on custom metadata and only select the top N.

I understand how to list the items and create basic filters (from the list of filter items in the API) but I need to filter on a metadata field (e.g. the three items with the latest metadata "priorityDate").

From what I'm reading I'd be expected to grab all records from the taxonomy search (using TaxonomyKeywordCriteria) and then manually (SQL executed against QueryRunner) retrieve all the components' metadata to filter/sort.

I must have missed something surely (it doesn't feel right have 1/2 API call and then in page SQL script)... and if not would the QueryRunner query be cached alongside the results of the Taxonomy Query.executeQuery()

Example code:

ItemTypeCriteria isComponent = new ItemTypeCriteria(16);
TaxonomyKeywordCriteria taxonomyKeywordCriteria = new TaxonomyKeywordCriteria(taxURI, taxKeywordURI, true);
Criteria[] allCriteria = {isComponent, isTDIPublication, isArticle, taxonomyKeywordCriteria}; 
AndCriteria andCriteria = CriteriaFactory.And(allCriteria);
Query query = new Query();         
query.setCriteria(andCriteria);
Flagler answered 25/4, 2012 at 21:10 Comment(3)
Should we assume that your Metadata is not Keyword driven? I have designed my Schemas to make maximum use of Categories and Keyword so you can add the keyword together in a large AND query. You may also want to think about standard Broker Queries where you can use Custom Meta Queries. They may suit you better than the Taxonomy APIDuress
From you profile, it does not look like you have joined the initiative for a SDL Tridion based Stack Exchange site on Area 51 - Please consider commiting to it at area51.stackexchange.com/proposals/38335/…Duress
Cheers Chris. I've been looking at the standard Broker Queries and have a gut feeling we're heading this way - certainly when we get to a point where we are half using a taxonomy query and then throwing a SQL Select based on what the API returns... are there any caching implications - I presume Tridion object caching will take care of this for us. In terms of the metadata, in this instance it is not keyword driven (what's the significance of that out of interest?).Flagler
M
12

Using SQL inside your JSP doesn't seem appropriate, since it's data layer logic and should not be present on the presentation. Also, if Tridion update the DB structure with a new release, then there is a risk your custom SQL could stop working. You're better off using the Broker API and not rely on custom SQL statements into the Broker DB.

I think your best bet is to try and minimize the number of database hits required. The API allows filtering by keywords (you can filter by custom meta as well, but that won't help you, since you mentioned you want to sort by a given custom meta field). You have two options to get a list of CPs by keywords which you can then sort:

  1. Query.execureQuery(). This returns a list of tcm URIs, meaning that a separate DB hit has to be made to retrieve the CustomMetadata objects and the CPs (ComponentPresentationFactory.GetComponentPresetation(tcmUri)). This could (and probably will) get heavy until everything is sufficiently cached.
  2. ComponentPresentationFactory.getTaxonomyComponentPresentations(). You can pass an array of keywords to this method and get back a larger set of CPs. This means one quick DB query. On the CM side, in the component template add rendering of the Custom Meta field value as an HTML comment. Then back on the delivery side simply parse out that value from each CP of the returned set and do your sorting on the application server (rather than the DB). This will be faster than doing many DB queries.

Your question topic asks for some examples of Taxonomy query examples. Have a look at the sdllivecontent portal at the 2011 documentation. The API is pretty much the same as for 2009 (there are only subtle differences, however, there are some examples on constructing filters provided there: http://sdllivecontent.sdl.com/LiveContent/web/pub.xql?action=home&pub=SDL_Tridion_2011&lang=en-US#addHistory=true&filename=DevelopingAFilter.xml&docid=concept_0AB6D192D7AB4EC18892631F519EF1DD&inner_id=&tid=&query=&scope=&resource=&eventType=lcContent.loadDocconcept_0AB6D192D7AB4EC18892631F519EF1DD

The sdltridionworld forum is another good place if you browse through the topics. Here are a couple of threads with some example code:

  1. https://forum.sdltridion.com/topic.asp?TOPIC_ID=6690&SearchTerms=taxonomy,query
  2. https://forum.sdltridion.com/topic.asp?TOPIC_ID=5619&SearchTerms=taxonomy,query
Mcclure answered 26/4, 2012 at 0:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.