Searching an index

Your documents are in OpenSearch, and now you want to find them again: by words in a title, by an exact author, by a range of years, or by all of those at once, one page at a time. And you want Gleam values back, not a JSON string. This guide builds a small search module for a book catalog, from a first query to a combined, paged and sorted search, with tests.

Table of Contents

What you’ll build

In this guide you’ll:

Before you start

Add the package, and gleam_json to build queries and read responses:

gleam add dream_opensearch gleam_json

The examples search an index named books, with this mapping:

FieldTypeExample
titletext"Programming Gleam"
authorkeyword"Jo Lee"
yearinteger2024
in_stockbooleantrue

The types matter for search, and this guide says why at each step. Managing indexes creates this index, and Loading documents in bulk fills it.

Every example in this guide is tested against OpenSearch 2.19.1 in Docker, in an index with this mapping. Getting started shows the Docker service, and docker compose up -d --wait opensearch starts it. The responses you see here are bodies that the cluster really sent. took and the scores change with the documents in the index and from run to run.

How a search works

A search is one request. You send a JSON query to POST /<index>/_search, and OpenSearch answers with the matching documents. In this package:

The builders cover the common single queries. For anything more, you write the query JSON yourself with gleam/json, as Step 4 shows.

Which query to use

Your situationUseWhy
Words that a person types, in any case and any order, in a text fieldquery.match(field, text)OpenSearch analyzes the text first, so gleam finds Programming Gleam
One exact value: a status, a tag, an ID, an author in a keyword fieldquery.term(field, value)No analysis. The value must equal the stored value exactly
Values between two bounds, both included: years, prices, datesquery.range(field, gte, lte)Matches gte <= value <= lte. Both bounds are strings
Every document, for example to browse or to countquery.match_all()Matches all documents, with the same score
Several conditions, an open-ended range, a page, a sortYour own JSON, built with gleam/jsonThe builders return complete bodies, so you cannot put one inside another

Step 1: Run a query and read the hits

Start with the part every search needs: send a query and turn the response into Gleam values. Write it once, and every search in the module uses it:

Search the books index and read the hits Tested snippet

import dream_opensearch/client.{type Client}
import dream_opensearch/document
import dream_opensearch/query
import gleam/dynamic/decode
import gleam/int
import gleam/json
import gleam/result

pub type Book {
  Book(id: String, title: String, author: String, year: Int, in_stock: Bool)
}

pub type SearchResult {
  /// `total` counts every match, `books` holds one page of them.
  SearchResult(total: Int, books: List(Book))
}

/// Sends `query_json` to `index` and reads the hits.
pub fn search_books(
  opensearch: Client,
  index: String,
  query_json: String,
) -> Result(SearchResult, String) {
  use body <- result.try(document.search(opensearch, index, query_json))
  search_result_from_response(body)
}

/// Reads the total and the books out of the body of a search response.
pub fn search_result_from_response(
  body: String,
) -> Result(SearchResult, String) {
  json.parse(body, search_result_decoder())
  |> result.replace_error("Unexpected search response: " <> body)
}

fn search_result_decoder() -> decode.Decoder(SearchResult) {
  use total <- decode.subfield(["hits", "total", "value"], decode.int)
  use books <- decode.subfield(["hits", "hits"], decode.list(book_hit()))
  decode.success(SearchResult(total:, books:))
}

fn book_hit() -> decode.Decoder(Book) {
  use id <- decode.field("_id", decode.string)
  use title <- decode.subfield(["_source", "title"], decode.string)
  use author <- decode.subfield(["_source", "author"], decode.string)
  use year <- decode.subfield(["_source", "year"], decode.int)
  use in_stock <- decode.subfield(["_source", "in_stock"], decode.bool)
  decode.success(Book(id:, title:, author:, year:, in_stock:))
}

/// Full-text search in the titles.
pub fn books_about(
  opensearch: Client,
  index: String,
  words: String,
) -> Result(SearchResult, String) {
  search_books(opensearch, index, query.match("title", words))
}

search_books takes the index name and any query string. It sends the query with document.search, and gives the body to search_result_from_response. The other functions of the module take the index name too, so a test can use an index of its own.

books_about(opensearch, "books", "gleam") sends this request:

The request of books_about Tested snippet

POST /books/_search
{"query":{"match":{"title":"gleam"}}}

For an index that holds these two books, OpenSearch answers with this body:

The answer to the match query Tested snippet

{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":0.21110919,"hits":[{"_index":"books","_id":"1","_score":0.21110919,"_source":{"title":"Programming Gleam","author":"Jo Lee","year":2024,"in_stock":true}},{"_index":"books","_id":"2","_score":0.160443,"_source":{"title":"Gleam for Erlang Developers","author":"Sam Park","year":2022,"in_stock":false}}]}}

The shorter title gets the higher score, so Programming Gleam comes first.

The decoder reads two things from it:

So books_about returns:

The result of books_about Tested snippet

Ok(SearchResult(total: 2, books: [Book(id: "1", title: "Programming Gleam", author: "Jo Lee", year: 2024, in_stock: True), Book(id: "2", title: "Gleam for Erlang Developers", author: "Sam Park", year: 2022, in_stock: False)]))

When nothing matches, the request still succeeds. hits.hits is an empty list, and the function returns Ok(SearchResult(total: 0, books: [])). An empty result is not an error.

Keep total and books apart in your head. OpenSearch returns 10 hits unless you ask for another page size, so total can be much larger than the length of books. Step 4 sets the page.

Step 2: Find exact values with term

A match query is right for words a person types. For a value that must be exact, such as an author’s name from a list, use a term query. Add this to the module:

Find the books of one author Tested snippet

/// The books of one author, spelled exactly as stored.
pub fn books_by(
  opensearch: Client,
  index: String,
  author: String,
) -> Result(SearchResult, String) {
  search_books(opensearch, index, query.term("author", author))
}

books_by(opensearch, "books", "Jo Lee") sends:

The request of books_by Tested snippet

POST /books/_search
{"query":{"term":{"author":"Jo Lee"}}}

A term query does not analyze the value. It finds the documents whose field holds exactly Jo Lee, in that case: books_by(opensearch, "books", "jo lee") finds nothing. That works because author is a keyword field, which stores the value as one unit.

A text field is different. OpenSearch splits it into lower-case words when it stores it, so the title Programming Gleam is stored as programming and gleam. A term query for Programming Gleam on title looks for that whole string among those words, and finds nothing. Use match on text fields and term on keyword, number and boolean fields.

query.term always sends the value as a JSON string. For a number or a boolean field, OpenSearch converts the string, so query.term("in_stock", "false") finds the books that are not in stock. To send a real JSON true or 2024, build the query yourself, as in Step 4.

Step 3: Find a range of values

To find the books from a span of years, use a range query:

Find the books published between two years Tested snippet

/// The books published from `first_year` to `last_year`, both included.
pub fn books_published_between(
  opensearch: Client,
  index: String,
  first_year: Int,
  last_year: Int,
) -> Result(SearchResult, String) {
  let years =
    query.range("year", int.to_string(first_year), int.to_string(last_year))
  search_books(opensearch, index, years)
}

books_published_between(opensearch, "books", 2020, 2024) sends:

The request of books_published_between Tested snippet

POST /books/_search
{"query":{"range":{"year":{"gte":"2020","lte":"2024"}}}}

Both bounds are included: gte means greater than or equal, and lte means less than or equal. query.range takes both bounds as strings, so the function converts the years with int.to_string, and OpenSearch converts them back to the type of the field. The same works for dates, with strings such as "2024-01-01".

query.range always sends both bounds. For “2020 or later”, with no upper bound, or for a bound that is not included (gt, lt), write the range yourself, as the next step does.

Step 4: Combine conditions, then page and sort

A real search screen asks for more than one thing at once: books about Gleam, in stock, from 2020 on, newest first, ten to a page. That is a bool query with paging and a sort. The builders return complete bodies, so you cannot nest one in another. Build the whole body with gleam/json instead:

Build a bool query with paging and sorting Tested snippet

import gleam/json

/// Books in stock whose title matches `words`, published in `from_year` or
/// later, newest first, one page at a time. `page` counts from 1.
pub fn in_stock_query(
  words: String,
  from_year: Int,
  page: Int,
  per_page: Int,
) -> String {
  let bool_query =
    json.object([
      #(
        "bool",
        json.object([
          // `must` clauses decide which books match, and how well
          #(
            "must",
            json.preprocessed_array([match("title", json.string(words))]),
          ),
          // `filter` clauses only include or exclude, with no score
          #(
            "filter",
            json.preprocessed_array([
              term("in_stock", json.bool(True)),
              at_least("year", json.int(from_year)),
            ]),
          ),
        ]),
      ),
    ])
  json.object([
    #("query", bool_query),
    #("from", json.int({ page - 1 } * per_page)),
    #("size", json.int(per_page)),
    #("sort", json.preprocessed_array([newest_first()])),
  ])
  |> json.to_string
}

fn match(field: String, value: json.Json) -> json.Json {
  json.object([#("match", json.object([#(field, value)]))])
}

fn term(field: String, value: json.Json) -> json.Json {
  json.object([#("term", json.object([#(field, value)]))])
}

fn at_least(field: String, value: json.Json) -> json.Json {
  json.object([
    #("range", json.object([#(field, json.object([#("gte", value)]))])),
  ])
}

fn newest_first() -> json.Json {
  json.object([#("year", json.object([#("order", json.string("desc"))]))])
}

The parts of the body:

in_stock_query("gleam", 2020, 1, 10) returns this body:

The body of in_stock_query Tested snippet

{"query":{"bool":{"must":[{"match":{"title":"gleam"}}],"filter":[{"term":{"in_stock":true}},{"range":{"year":{"gte":2020}}}]}},"from":0,"size":10,"sort":[{"year":{"order":"desc"}}]}

Send it with the function from Step 1, as search_books(opensearch, "books", in_stock_query("gleam", 2020, 1, 10)), and you get a SearchResult back as before. For the two books above, that is Ok(SearchResult(total: 1, books: [Book(id: "1", title: "Programming Gleam", author: "Jo Lee", year: 2024, in_stock: True)])): book 2 is about Gleam, but it is not in stock. total still counts every match, so you can work out the number of pages from it.

Note that in_stock_query sends no request. It is a plain function from your parameters to a string, so you can test it with no cluster, as Test your code does.

When a search fails

search_books returns Error(String) in three kinds of cases.

The index does not exist. OpenSearch answers with status 404, and the error is the response body:

The answer for a missing index Tested snippet

{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index [books]","index":"books","resource.id":"books","resource.type":"index_or_alias","index_uuid":"_na_"}],"type":"index_not_found_exception","reason":"no such index [books]","index":"books","resource.id":"books","resource.type":"index_or_alias","index_uuid":"_na_"},"status":404}

OpenSearch cannot read the query. A misspelled query type, such as matches for match, gets status 400 and a body that says where the problem is:

The answer to a misspelled query Tested snippet

{"error":{"root_cause":[{"type":"parsing_exception","reason":"unknown query [matches] did you mean any of [match, match_all]?","line":1,"col":21}],"type":"parsing_exception","reason":"unknown query [matches] did you mean any of [match, match_all]?","line":1,"col":21,"caused_by":{"type":"named_object_not_found_exception","reason":"[1:21] unknown field [matches]"}},"status":400}

The builders always write valid queries. This error comes from JSON that you build yourself, so test your query builders, as the next section shows.

The response does not fit your decoder. The request worked, but a document lacks a field the decoder needs. For example, a book with no year makes the whole page fail with Error("Unexpected search response: <the body>"). If some documents can lack a field, read it with decode.optional_field and make the field an Option in your type.

A refused connection, a wrong URL and the other errors of the client come back as messages. Handling errors lists them, and shows how to tell an OpenSearch error from a connection error in code.

Test your code

Test the searches themselves against a real cluster: start the Docker service from Getting started with docker compose up -d --wait opensearch, create an index with a name of its own for each test, load a few books, refresh the index, search, and delete the index at the end. That is how the examples of this guide are tested.

The search module also has two parts that you can test with no cluster at all:

Add dream_test as a dev dependency, with the version range that this package tests with:

The dev dependency on dream_test Tested snippet

[dev-dependencies]
dream_test = ">= 3.0.0 and < 4.0.0"

Here is a test module for both parts:

Test a query builder and the response reader Tested snippet

import dream_test/matchers.{be_equal, or_fail_with, should}
import dream_test/suite.{type TestSuite}
import dream_test/unit.{describe, it}
import snippets/guide_searching_books.{
  Book, SearchResult, search_result_from_response,
}
import snippets/guide_searching_bool.{in_stock_query}

pub fn tests() -> TestSuite {
  describe("book search", [
    it("asks for the second page of ten", fn() {
      // Act
      let query_json = in_stock_query("gleam", 2020, 2, 10)

      // Assert
      query_json
      |> should
      |> be_equal(
        "{\"query\":{\"bool\":{\"must\":[{\"match\":{\"title\":\"gleam\"}}],"
        <> "\"filter\":[{\"term\":{\"in_stock\":true}},"
        <> "{\"range\":{\"year\":{\"gte\":2020}}}]}},"
        <> "\"from\":10,\"size\":10,\"sort\":[{\"year\":{\"order\":\"desc\"}}]}",
      )
      |> or_fail_with("Page 2 of 10 should start at hit 10")
    }),
    it("reads the total and the books of a response", fn() {
      // Arrange
      let body =
        "{\"hits\":{\"total\":{\"value\":12,\"relation\":\"eq\"},\"hits\":["
        <> "{\"_id\":\"7\",\"_source\":{\"title\":\"Gleam in Action\","
        <> "\"author\":\"Jo Lee\",\"year\":2024,\"in_stock\":true}}]}}"

      // Act
      let result = search_result_from_response(body)

      // Assert
      result
      |> should
      |> be_equal(
        Ok(
          SearchResult(total: 12, books: [
            Book(
              id: "7",
              title: "Gleam in Action",
              author: "Jo Lee",
              year: 2024,
              in_stock: True,
            ),
          ]),
        ),
      )
      |> or_fail_with("Expected 12 matches and one book on this page")
    }),
  ])
}

The first test pins down the paging: page 2 of 10 starts at hit 10. The second test reads a response where total is 12 and the page holds one book, which is the case a paging screen must get right.

Use real responses from your cluster in these tests where you can. Run the query once against a development cluster, copy the body into the test, and you test the decoder against what your cluster really sends.

Common pitfalls

A term query that finds nothing. Symptom: query.term("title", "Programming Gleam") returns no hits, but the book is there. Fix: title is a text field, which OpenSearch stores as lower-case words. Use query.match on text fields, and query.term on keyword, number and boolean fields. If you need both, map the field as text with a keyword sub-field.

A builder inside a bool query. Symptom: a parsing_exception from a bool query that holds the output of query.match. Fix: each builder returns a whole body, {"query":{...}}, and a body cannot go inside another query. Build the clauses with gleam/json, as in Step 4.

Only ten results. Symptom: total says 37, but books holds 10. Fix: OpenSearch returns 10 hits unless the body has size. Set from and size for the page you want.

No hits right after a write. Symptom: a test indexes a book and searches at once, and finds nothing. Fix: OpenSearch makes new documents visible at the next refresh, about once a second by default. Refresh the index before you search in tests, with client.send_request(opensearch, http.Post, "/books/_refresh", "").

One bad document fails the whole page. Symptom: Unexpected search response for a search that worked yesterday. Fix: a document without a field your decoder needs makes decode.list fail. Make that field optional in the decoder, or make sure every document has it, for example with a strict mapping. See Managing indexes.

A range with numbers in it. Symptom: a compile error when you pass 2020 to query.range. Fix: the bounds are strings. Convert numbers with int.to_string, or write the range with json.int yourself.

Where to go next

✨ Search Document