.../articles/
Sharpening Our Contentful Skills to Get the Most Out of It [Search and Filtering]

Sharpening Our Contentful Skills to Get the Most Out of It [Search and Filtering]

2019.08.10

Tagging and Filtering by Tag

First, let's cover how to create tags. In Content, create a Tag content type separate from your blog posts. It works the same way as Author. Then edit the Content model for your Blog Post and add a References field. This can point to a single tag or multiple tags.

Create a few tags in Content and link them to a post (the same way you'd select an Author). That alone is enough to have the post tagged.

All that's left is to filter by the target tag when displaying blog posts. As shown in the example below, the content you created will show up under fields, so you just need to specify the target ID there and it will return the matching content.

  async fetch() {
    const limit = 10
    const skip = 0
    const client = contentful.createClient(config)
    const posts = await client.getEntries({
      content_type: process.env.CTF_BLOG_POST_TYPE_ID,
      limit,
      skip,
      order: '-sys.createdAt',
      'fields.tags.sys.id': 'ID of the tag to filter by',
    })
  },

Reference Links to a specific item If you want to retrieve all items linked to a specific entry, the query URL should filter entries on their specific content_type, linking_field (field to link items) and entry_id from the target entry. Contentful Document links/links-to-a-specific-item

Exclusion

Say you want to pull other posts with the same tag as related articles for a given post. In that case, you'll likely want to exclude the current post itself from the related-articles list. For that, you can use [ne].

In the example below, we're fetching blog posts in order of newest first, excluding the post with a given slug.

  async fetch() {
    const limit = 5
    const client = contentful.createClient(config)
    const posts = await client.getEntries({
      content_type: process.env.CTF_BLOG_POST_TYPE_ID,
      limit,
      order: '-sys.createdAt',
      'fields.slug[ne]': 'slug',
    })
  },

Reference Inequality operator Uses the [ne] parameter to exclude items matching a certain pattern. Contentful Document search-parameters/inequality-operator

Full-Text Search

Contentful even provides full-text search out of the box! Just add query to getEntries and it will run a full-text search across your content. Talk about a complete feature set!

  async fetch() {
    const limit = 10
    const skip = 0
    const client = contentful.createClient(config)
    const posts = await client.getEntries({
      content_type: process.env.CTF_BLOG_POST_TYPE_ID,
      limit,
      skip,
      order: '-sys.createdAt',
      query: 'text to search for',
    })
  },

Reference Full-text search It's possible to perform a full-text search across all text and symbol fields with the query parameter. Note: Full-text search is case insensitive and might return more results than expected. A query will only take values with more than 1 character. Contentful Document search-parameters/full-text-search

Incidentally, it's also possible to search on specific fields. For example, if you want to search within article titles, it might look like this:

  async fetch() {
    const limit = 10
    const skip = 0
    const client = contentful.createClient(config)
    const posts = await client.getEntries({
      content_type: process.env.CTF_BLOG_POST_TYPE_ID,
      limit,
      skip,
      order: '-sys.createdAt',
      'fields.title[match]': 'Nuxt', // Search for posts of Content Type BLOG_POST whose title contains "Nuxt"
    })
  },

Reference Full-text search on a field You can perform a full-text search on a specific field with the [match] operator. Note: Full-text search is case insensitive and might return more results than expected. A query will only take values with more than 1 character. Contentful Document search-parameters/full-text-search-on-a-field

Summary

Reading through the documentation, you keep finding yourself thinking "oh, you can do that too? 👀" There's a ton to explore, and it would be fun to try building out an MVP using all these different features.

We'll share more useful features as we discover them going forward!

written by

.../article/

Articles

All articles

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

We moved our corporate site's hosting and CMS, and made it bilingual along the way. The constraints we only found by running against real data were more useful than the migration itself, so this post focuses on where we got stuck.

Can't Read POST Data with Firebase Functions × Remix?

Can't Read POST Data with Firebase Functions × Remix?

How to read POST data from a Remix action when running on Firebase Functions.

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Building a structure where executives and leaders themselves can identify issues and evaluate solutions using generative AI. We introduce how combining this with our hands-on support dramatically improves both the quality and speed of digital transformation.

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Notes on the obstacles we hit while deploying a Next.js app managed in a monorepo to AWS Amplify.

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Many production companies have adopted remote work as a result of the pandemic, and we are one of them.

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

There is no single formula for e-commerce design that sells. Driving revenue requires a solid concept, and getting to that concept requires thorough research.

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

With remote work becoming the norm during the COVID-19 pandemic, our team now works from home most days of the week.

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We hope this helps web designers who work in Figma. Read on for how to use it.

How to Build an E-Commerce Site, and Which Platforms We Recommend

How to Build an E-Commerce Site, and Which Platforms We Recommend

Shopping online for fashion, appliances, and even groceries is now routine. With the pandemic accelerating the shift, we receive a steady stream of questions about which platform to use and how much it costs.

Generating FastAPI Schema Classes from OpenAPI

Generating FastAPI Schema Classes from OpenAPI

We chose FastAPI, a relatively modern framework, for a Python API project. FastAPI can generate an OpenAPI definition from your backend code, but here we do the opposite: generating FastAPI schema classes from an OpenAPI definition prepared in advance.

View all articles

Contact us