# How company search matching works

> How CNPJá company search finds results: words, accents and stems, free text search, relevance order and how long it takes for a new CNPJ to appear in results.

CNPJá company search finds [establishments](https://cnpja.com/en-us/docs/glossary#estabelecimento--establishment) by name, address, contacts and filters such as activity, municipality and registration status, without you having to know the CNPJ. It compares words regardless of case and accents, sorts results by relevance and reads only the CNPJá database, where a CNPJ that just arrived can be found within a few minutes. The rules apply to the search in the app and in the API, except where this page points out a difference.

## Each result is an establishment

Each result is a full CNPJ: the [head office](https://cnpja.com/en-us/docs/glossary#matriz--head-office) or a [branch](https://cnpja.com/en-us/docs/glossary#filial--branch) of a [company](https://cnpja.com/en-us/docs/glossary#empresa--company). Company data, such as legal name, share capital, legal nature, size, tax regime and partners, is the same for all of its establishments, as explained in [What belongs to the company and what to the establishment](https://cnpja.com/en-us/docs/data/company-and-establishment#what-belongs-where). For this reason, a filter on the company, such as size or share capital, returns every establishment of that company that matches the other filters. To get one result per company, also filter for head offices: in the app, **Type** > **Head office**.

Search reads only the CNPJá database, the [cached data](https://cnpja.com/en-us/docs/data/freshness#cached), and does not query the sources at that moment. A value in the results may be out of date: for the current registration of a CNPJ you found, open its record and update the sources, as explained in [Data freshness](https://cnpja.com/en-us/docs/data/freshness).

## Words, accents and punctuation

Free text search and the word filters on names compare words, not the exact text:

- they ignore case and accents: `São José` matches `SAO JOSE`;
- they remove punctuation without splitting words: `S/A` matches `SA`;
- they compare words by their Portuguese stem, always as whole words: `mercado` matches `mercados`, but not `supermercado`.

The other filters compare exact values: codes, such as CNAE, municipality and CEP, phones, emails and email domains, each as a whole.

The street, number, address details and district filters compare the whole value, not separate words, in the app and in the API, with the same case, accent, punctuation and stem rules: `Rua São José` matches `RUA SAO JOSE`, but not `RUA SAO JOSE DOS CAMPOS`. In the app, they are the **Street**, **Number**, **Address details** and **District** filters, described in [All filters](https://cnpja.com/en-us/docs/search/filters#reference); in the API, such as `address.street.in`, they accept only `in` and `nin`, not `some` or `none`.

## Include and exclude words

In a word filter, each word counts on its own, even when you type several together: including `pao de queijo` returns any name with `pao`, with `de` or with `queijo`. Excluding words removes from the results every name that has any of them. Free text search works the other way around: it returns only results that have every word typed, in any order.

Use free text search to find a specific company, and the word filters to gather a segment by alternative words, such as `padaria` and `confeitaria`.

Filters combine like this:

- within a filter, a result needs to match only one of the included values, and a result with any excluded value is left out;
- different filters add up, and a result must match all of them at once;
- free text search adds to the filters.

How to include and exclude values in the app is in [Include and exclude](https://cnpja.com/en-us/docs/search/filters#exclude).

## Free text search

Free text search, the search's text box, looks for the words in all of these at once:

| Data | How it is matched |
|---|---|
| CNPJ | In full or by its beginning, from 3 characters, with or without punctuation. The [root](https://cnpja.com/en-us/docs/glossary#cnpj-raiz--cnpj-root), such as `98.765.432`, returns every establishment of the company |
| Legal name and trade name | By their words, with the rules in [Words, accents and punctuation](#text-matching) |
| Phones | By the full number, with or without the country code `55` and the area code, without spaces: `5511900000000`, `11900000000` or `900000000` |
| Emails | By the full address, such as `contato@empresa.example`, or by the domain, such as `empresa.example` |
| Address | Street, number, details, district, municipality name, state and CEP |

## Result order

Unless another order is chosen, results come by relevance. In a free text search with more than one word, results that have the words as an exact phrase come first. Then relevance favors head offices, active establishments and larger companies. Tied results follow the CNPJ in ascending order, so the order is the same on every page.

The search in the app always uses relevance, and the table columns do not reorder the results. In the API, you can also sort by CNPJ, opening date, share capital or legal name, as shown in [In the API](#api).

## Results are not fixed

Each page of results is read from the database when you open it. If the data changes while you go through the pages, an establishment can appear on two pages or on none, a page can come back short before the end, and the count can change from one read to the next.

To keep a fixed set of CNPJs, save the results to a [list](https://cnpja.com/en-us/docs/glossary#lista--list), which only changes when you add or remove CNPJs. A view keeps the search and the filters, and its results follow the database. See [View or list?](https://cnpja.com/en-us/docs/search/views#view-or-list).

## When a new CNPJ appears

Search uses an index updated periodically from the CNPJá database. A CNPJ that enters the database, such as one opened after the latest [CNPJ open data](https://cnpja.com/en-us/docs/glossary#dados-abertos-do-cnpj--cnpj-open-data) release and queried live, can be found within a few minutes. Updates to a CNPJ already in the database reach the filters within a few hours.

- A CNPJ opened after the latest release and never queried live is not in the database, so it does not appear in search, as explained in [What is in the cache](https://cnpja.com/en-us/docs/data/freshness#cached).
- Each result shows the database values at the time of the search, which can be more recent than those the filters used. Right after an update, a result can show a value that no longer matches the applied filter.
- A list can hold CNPJs that are not in search yet. They only appear in results filtered by the list after they enter the database and the index.

## What cannot be filtered

- *State registration and SUFRAMA:* search does not filter by state registration or SUFRAMA registration, and the results do not carry this data, which depends on live queries. Find the establishments by state or municipality and look up each CNPJ, as explained in [State registration and CCC](https://cnpja.com/en-us/docs/data/state-registrations) and [SUFRAMA and Free Trade Zone incentives](https://cnpja.com/en-us/docs/data/suframa).
- *Municipality by name:* the municipality filters use the [IBGE code](https://cnpja.com/en-us/docs/data/postal-codes#ibge-code). In the app, the **City** filter looks the name up in the list and applies the code. The municipality name is only compared as text in free text search.
- *Partners:* company search has no partner filter. To find a person's companies, use the [partner search](https://cnpja.com/en-us/docs/partners/search).

## Where it appears in the app

- *Search CNPJ:* the **Search a CNPJ, name or address** box is the free text search, and the filters sit on the bar above the table. See [Search CNPJs](https://cnpja.com/en-us/docs/search/companies) and [Search filters](https://cnpja.com/en-us/docs/search/filters).
- *Views and lists:* the page of a view or of a list shows its results with the same rules. See [Views](https://cnpja.com/en-us/docs/search/views) and [Create and manage lists](https://cnpja.com/en-us/docs/lists/create).
- *Global search:* the global search (`Ctrl+K`, or `⌘K` on a Mac) looks for companies with the free text search rules. See [Global search and keyboard shortcuts](https://cnpja.com/en-us/docs/app/global-search).

## In the API

Search is the Search CNPJ operation. Free text search goes in `search`, and filters take the form `field.operator`, such as `names.some` to include words in the names and `names.none` to exclude them. The order can be chosen with `sort` and `order`, and the following pages are read with a cursor. The parameters, the fields and the list operations are in [CNPJ search and lists in the API](https://cnpja.com/en-us/docs/api/cnpj-search).
