Search by taxonomy in CrafterCMS

166 views Asked by At

CrafterCMS editorial blueprint shows the use of taxonomy for content targeting. Using the article's categories_o field, I can assign 0 to 4 categories. The Elasticsearch index for that field looks like this:

      "categories_o" : {
        "item" : [
          {
            "key" : "style",
            "value_smv" : "Style"
          },
          {
            "key" : "technology",
            "value_smv" : "Technology"
          }
        ]
      },

How do I search articles with one or more categories?

The built-in categorization framework of Liferay creates an Elasticsearch index for a similar field like this:

    "assetCategoryIds" : [ "644879", "644884", "644889", "6207544", "6207546", "6207550" ],

Then you can use the Elasticsearch query construct "terms" to search documents belongs to multiple (not just one) categories:

"query": { "bool": {
  "must": { "match_all": {} },
  "filter": {
    "bool": {
      "must": [
        {"terms": {"assetCategoryIds": ["644889","6207550"]}},
...

I am looking for a way to do the same search in CrafterCMS.

2

There are 2 answers

0
Jose Ross On

You can use the same query, using the full path of the field:

"terms": {"categories_o.item.key": [...]}
0
Michael Chen On

I tested it, and this query works:

alias jcurl='curl -H "Content-Type: application/json"'
jcurl -sXPOST "localhost:9201/sample2-preview_v1/_search?pretty" -d '
{"size":1,"query":{"bool":{"must":[
{"terms":{"categories_o.item.key":["technology","style"]}}]}}}'