Skip to content

Query Guide ​

Practical examples of the MygramDB query commands. The full grammar, limits, and error messages are in the Query Syntax Reference.

Allow your source IP

Before running queries, make sure the client IP falls inside network.allow_cidrs in the configuration. CIDR is a notation for an IP address range, such as 192.168.1.0/24. Without a matching entry, every connection is denied. See Network Security.

Connecting with mygram-cli ​

bash
mygram-cli -h localhost -p 11016

Once connected, you can execute queries interactively.

mygram
127.0.0.1:11016> SEARCH articles hello world
OK RESULTS 3 101 205 387

In a single-database configuration, articles resolves to that database's table. In a multi-database configuration, qualify the table as <database>.<table>:

mygram
127.0.0.1:11016> SEARCH app_db.articles hello world

Without search text ​

Leave the search text out to list rows by filter or count them. The query reads straight from the index, covering every indexed row narrowed by FILTER:

mygram
127.0.0.1:11016> SEARCH articles FILTER status = 1 LIMIT 20
127.0.0.1:11016> COUNT articles FILTER status = 1
OK COUNT 42

HIGHLIGHT, FUZZY, and SORT _score need search text and are refused without it. Queries without search text do not use the query cache. See Queries Without Search Text.

Boolean Operators ​

Reading the operators

AND means "contains both", OR means "contains any", and NOT means "exclude anything containing this". For anything complex, parentheses make the intent unambiguous.

AND - All terms must match ​

mygram
127.0.0.1:11016> SEARCH articles golang AND tutorial

OR - Any term matches ​

mygram
127.0.0.1:11016> SEARCH articles golang OR python OR rust

NOT - Exclude terms ​

mygram
127.0.0.1:11016> SEARCH articles tutorial NOT beginner
127.0.0.1:11016> SEARCH articles tutorial AND NOT beginner
127.0.0.1:11016> SEARCH articles required AND 'not'

AND NOT is equivalent to NOT here. It is accepted for clients that represent each additional condition with an AND prefix.

A quoted keyword is a literal term. The last query requires the word not; the quotes keep it from being parsed as the NOT operator.

A leading NOT is also accepted, but it produces a wide candidate set. Combine it with search terms or filters (see NOT Search).

Combined ​

mygram
127.0.0.1:11016> SEARCH articles (golang OR python) AND tutorial NOT beginner

Without parentheses, NOT binds tightest, then AND, then OR, so a OR b AND c means a OR (b AND c). See Operator Precedence.

Use quotes for exact phrases:

mygram
127.0.0.1:11016> SEARCH articles "machine learning"
127.0.0.1:11016> SEARCH articles 'web framework'

Combine with operators:

mygram
127.0.0.1:11016> SEARCH articles "web framework" AND (golang OR python)

Filtering ​

Filter by column values:

mygram
127.0.0.1:11016> SEARCH articles tech FILTER status = 1
127.0.0.1:11016> SEARCH articles tech FILTER views > 1000
127.0.0.1:11016> SEARCH articles tech FILTER created_at >= 1704067200
127.0.0.1:11016> SEARCH articles tech FILTER deleted_at IS NULL

Filter columns must be configured

FILTER works on columns declared under tables[*].filters or tables[*].required_filters. An undeclared column is not an error: it silently matches nothing or everything depending on the operator. See Filter Column Types.

Dates and times are numbers in a query: Unix epoch seconds for date, datetime, and timestamp, signed seconds for time. ISO 8601 strings from required_filters do not work here.

Multiple filters (AND logic):

mygram
127.0.0.1:11016> SEARCH articles tech FILTER status = 1 FILTER category_id = 5

Filter Operators ​

FILTER takes =, != / <>, >, >=, <, <=, IS NULL, and IS NOT NULL. To test for a missing value, write IS NULL; c = NULL compares with the string NULL. Aliases and per-type behavior are in Supported Operators.

Sorting ​

Sort by primary key:

mygram
127.0.0.1:11016> SEARCH articles golang SORT ASC
127.0.0.1:11016> SEARCH articles golang SORT DESC

Sort by column:

mygram
127.0.0.1:11016> SEARCH articles golang SORT created_at DESC
127.0.0.1:11016> SEARCH articles golang SORT BY created_at DESC
127.0.0.1:11016> SEARCH articles golang SORT _score ASC

BY is optional. SORT ASC and SORT DESC without a column are shorthand for primary-key ordering.

Relevance Sort (BM25) ​

Sort by BM25 relevance score using the reserved _score column:

mygram
127.0.0.1:11016> SEARCH articles "machine learning" SORT _score DESC LIMIT 10

SORT _score requires search text and bm25.enable: true. Scoring parameters are in BM25 Relevance Scoring.

Highlighting ​

Return text snippets with matched terms wrapped in tags:

mygram
127.0.0.1:11016> SEARCH articles "machine learning" HIGHLIGHT LIMIT 10
127.0.0.1:11016> SEARCH articles "golang" HIGHLIGHT TAG <strong> </strong> LIMIT 10
127.0.0.1:11016> SEARCH articles "database" HIGHLIGHT SNIPPET_LEN 200 MAX_FRAGMENTS 5 LIMIT 10

The tags default to <em> and </em>. SNIPPET_LEN caps each fragment's length and MAX_FRAGMENTS caps the fragment count; defaults and ranges are in Highlighting.

Match terms within a Levenshtein edit distance:

mygram
127.0.0.1:11016> SEARCH articles "machne" FUZZY LIMIT 10
127.0.0.1:11016> SEARCH articles "databse" FUZZY 2 LIMIT 10

Distance 1 (default) allows one insertion, deletion, or substitution; 2 allows two. The result always includes what the same query returns without FUZZY. See Fuzzy Search.

FACET Aggregation ​

Aggregate distinct filter-column values with document counts:

mygram
127.0.0.1:11016> FACET articles status
127.0.0.1:11016> FACET articles category "search text" FILTER status = 1 LIMIT 10

TCP response (sorted by count, descending):

mygram
OK FACET <returned_value_count> <total_distinct_values>
<value1>\t<count1>
<value2>\t<count2>

The first header count is the number of values returned on this page. The second is the total number of distinct values for the query, including values omitted by LIMIT or OFFSET. Each value and its document count are separated by a tab, and an empty line terminates the response. Equal-count values keep a stable order, so pages do not reshuffle ties between requests.

Without search text, the facet counts every indexed row narrowed by FILTER. To scope the aggregation to a query, use the AND, NOT, FILTER, LIMIT, and OFFSET clauses. SORT, HIGHLIGHT, and FUZZY are not accepted on FACET (ERROR 3000 FACET: Unknown clause: <token>).

FACET is subject to the same availability checks as SEARCH: ERROR 6028 while a dump is loading, and ERROR 6029 while the table is synchronizing or has no initial data yet (see Table Availability).

FACET is also exposed over HTTP at POST /tables/{identity}/facet. The HTTP body uses column, optional q, optional mode, optional filters, optional limit, and optional offset.

Synonym Expansion ​

When a synonym dictionary is configured (tables[*].synonyms.enable: true, see Configuration), search terms are transparently expanded to OR-groups of their synonyms at query time. Queries need no new syntax. A query for car also matches automobile and vehicle when the TSV dictionary groups them together.

Synonyms are configured per table under tables[*].synonyms. Fuzzy search takes a separate execution path and is not combined with synonym expansion for the same query.

Pagination ​

mygram
127.0.0.1:11016> SEARCH articles golang LIMIT 10
127.0.0.1:11016> SEARCH articles golang LIMIT 10 OFFSET 20

The total_count in an OK RESULTS response is the number of matches before pagination, and the IDs that follow are the current page. See Response Format. The LIMIT range and the cost of deep pages are covered under Pagination.

Count Query ​

Get count without IDs:

mygram
127.0.0.1:11016> COUNT articles golang AND tutorial
OK COUNT 42

The search text is optional, so COUNT articles FILTER status = 1 counts every indexed row that passes the filter. COUNT accepts only AND, NOT, and FILTER; see the COUNT command.

Complete Examples ​

Find recent Go tutorials ​

mygram
127.0.0.1:11016> SEARCH articles golang AND tutorial FILTER status = 1 SORT created_at DESC LIMIT 20
mygram
127.0.0.1:11016> SEARCH posts (mysql OR mariadb) AND performance FILTER views > 1000 SORT _score DESC LIMIT 10

Top relevance with highlighted snippets ​

mygram
127.0.0.1:11016> SEARCH articles "machine learning" SORT _score DESC HIGHLIGHT LIMIT 10

Count active users in category ​

mygram
127.0.0.1:11016> COUNT users tech FILTER status = 1 FILTER category_id = 5

HTTP API ​

SEARCH, COUNT, FACET, and document GET are also exposed over HTTP using the /tables/{identity}/... route shape. HTTP q is literal by default; add "mode": "boolean" only when q is an intentional boolean expression. q is optional on /search, /count, and /facet. Without it the query has no search text, whatever mode says (mode is still validated). A q that is empty or only whitespace returns 400 with error code 3000 and the message Field 'q' must be non-empty.

bash
curl -X POST http://localhost:8080/tables/articles/search \
  -H "Content-Type: application/json" \
  -d '{"q": "golang tutorial", "limit": 10}'

curl -X POST http://localhost:8080/tables/articles/count \
  -H "Content-Type: application/json" \
  -d '{"q": "golang"}'

curl -X POST http://localhost:8080/tables/articles/facet \
  -H "Content-Type: application/json" \
  -d '{"column": "category", "q": "golang", "limit": 10}'

curl -X POST http://localhost:8080/tables/articles/search \
  -H "Content-Type: application/json" \
  -d '{"q": "golang AND tutorial", "mode": "boolean", "limit": 10}'

curl -X POST http://localhost:8080/tables/articles/search \
  -H "Content-Type: application/json" \
  -d '{"filters": {"status": 1, "deleted_at": {"op": "IS NULL"}}, "limit": 20}'

filters takes {"col": value} for equality or {"col": {"op": "<OP>", "value": value}}. IS NULL and IS NOT NULL take no value. For any other operator, a missing value returns 3006 Missing filter value for column: <col>.

With two or more configured databases, use /tables/app_db.articles/.... Admin commands such as SET, SHOW VARIABLES, SYNC, and DUMP remain TCP/CLI-only.

Performance Tips ​

Keep LIMIT small, narrow with FILTER on configured columns, and prefer AND to OR. The reasons and more tips are in Performance Tips.

Next Steps ​