Surprisingly few people ask me for relationship advice, and this week is no exception. Instead, this week “B.” in Springfield writes:
Dear Feed Doctor,
My items’ categories look like this: Top Level>Mid Level>Bottom Level. How would I write a rule to append the top-level of the category to my keywords?
Hopefully now my post title makes more sense; B. needs to break up her category hierarchy into its pieces, using the “>” character as a guide. If you think about it, this sounds like the opposite of what last week’s JOIN function does, and of course we have a function that does just what we need.
GETPART
Remember that JOIN will take a bunch of pieces of text and join them, using another piece of text that we call the delimiter as the “glue” between each piece. The GETPART function, which I said does the opposite, has three inputs:
- The text to break into parts
- The delimiter, which determines the breaking points
- The index of the part that you want
Wait…what’s that last one? Well, really you can only do something with one of the pieces at a given time, so GETPART only gives you one of them. The index tells GETPART which one. So, taking B’s example, GETPART("Top Level>Mid Level>Bottom Level",">",1) would output Top Level, and so her rule would be:
CONCATENATE($keywords," ",GETPART($merchantcategory,">",1))
Now suppose that B. doesn’t want the top level category; she wants the bottom level. No sweat, you say; just change that 1 to a 3. Ok, well suppose that not all categories have exactly 3 levels; suppose there are categories like this:
- General>Misc
- Home>Appliances>Kitchen>Countertop
- Toys>Video Games>Console>Games>Sports>Football>Former Head Coach, Commentator, and Hardware Store Spokesman 2006
Fortunately, we’ve thought of that. If you make the index a negative number, it counts from the other end. So if you want the bottom level category, use a -1. If you want the next-to-bottom level, use -2, and so on.
That’s all for this week. Be sure and send me your questions, about feeds, or relationships, or whatever.
Share This