If you can’t find it, did it really happen? Contentful phrase-based, full-text search

It doesn’t matter if something exists if you can’t find it. For large data sets, that means you need a search function that can get you what you need. Getting zero results and getting one thousand results amounts to the same thing: frustration. Zero results means you need to try again. One thousand results means you need to spend hours searching through irrelevant results, hoping to find what you’re looking for. No one has time for that.

That’s why we improved the search function in Contentful. We’re eager to announce the addition of phrase-based search to our full-text search engine. It’ll help you do precise searches that limit the number of "false positives" both in the content APIs (Delivery API, Preview API, Management API and GraphQL Delivery API) and in the web app.

Why you need phrase-based, full-text search

In the past, searching for something like how many ounces are in a gallon would produce a result that contains every entry in the environment with fields in any locale that contains any word in the query. 

This search strategy works well when you don’t know the exact content that you’re looking for or the order of the words in it. However, this can also lead to results that are not relevant to your search. 

Depending on your data set, you might end up with results about many ice cream cones or how to build a house or gallons of oil. None of these results help you answer your question — despite the fact that you are 100% sure that your colleague once wrote a blog entry about how to convert ounces to gallons. 

{
  fields: {
    title: {
      "en-US": "Ounces and bounces"
    },
    body: {
      "en-US": "...how many people..."
    },
    remarks: {
      "en-GB": "...in a container with five gallon...",
    }
  }
}

For example, an entry like the one above would match the search how many ounces in a gallon because ounces was found in the American English title field, how many in the title body field for the same locale and in a ...gallon in the remark field in the British locale. This might not be a problem if there are only a few matches like this. But it can be problematic when there are hundreds or thousands to deal with.

Phrase-based, full-text search is accurate and fast

We’ve extended the query and match full text search operators to support phrase search. This means that if you wrap the full-text search query with double quotes, the API will only return the entries if the words in the search query are together, in the exact order in any single field and locale. To illustrate this, let’s look at an example where we search again for "how many ounces in a gallon":

// First entry
{
  fields: {
    question: {
      "en-US": "in a gallon how many ounces?"
    },
    answer: {
      "en-US": "..."
    },
  }
}

// Second entry
{
  fields: {
    question: {
      "en-US": "how many ounces in a gallon"
    },
    answer: {
      "en-US": "..."
    },
  }
}

// Third entry
{
  fields: {
    title: {
      "en-US": "..."
    },
    answer: {
      "en-US": "...you can also check our article titled 'how many ounces in a gallon'..."
    },
  }
}

The first entry won't be included in the result. Even though it has all of the right words, they are not in the same order as in the search query. The latter two entries, on the other hand, will be included in the result set. They include all the words in the search query in just one field and locale, and they appear in the same order as in the query.

The phrase search is not only better at finding what you are looking for, it’s also faster. In fact, it’s much faster than the regular full-text search. When you choose to use phrase-based search, Contentful can apply a different search strategy that queries the data in a more efficient way.

Phrase search works in the web app, too!

Phrase search is not restricted to the content APIs. You and your team can also benefit from it when working in the web app. This is how phrase search looks like in the web app interface:

Image of the search bar

We continue to improve the Contentful you love

We hope that this improvement makes using Contentful even more awesome, and that it helps you be more productive. You can read more about full-text search in the documentation.

About the author

Don't miss the latest

Get updates in your inbox
A monthly newsletter to help you build better digital experiences with Contentful.
add-circle arrow-right remove style-two-pin-marker subtract-circle