API version: 0.3.0

---

# Writing an Elasticsearch query

The [Elasticsearch query](/reference/search/elasticsearch-query-dsl-mixin.md) step lets you run
[Query DSL](https://www.elastic.co/docs/explore-analyze/query-filter/languages/querydsl) queries against the internal
Elasticsearch database. This guide walks through building and running a query; see the
[reference page](/reference/search/elasticsearch-query-dsl-mixin.md) for the full list of supported features and security
constraints.

## A first query

The following request searches all elements which are accessible to the user for the word `lily` in the property
`description`, and hydrates the results into elements:

```bash
curl -X POST \
  -H 'Authorization: Bearer secret-token:PIPeJGUt7c00ENn8a5uDlc' \
  -H 'Content-Type: application/json' \
  -d '{
        "debug": true,
        "steps": [
          {
            "type": "elasticsearch-query-dsl-mixin",
            "query": {
              "match": {
                "description": {
                  "query": "lily"
                }
              }
            }
          },
          {
            "type": "element-hydration"
          }
        ]
      }' \
  https://api.example.com/search
```

##### Response Body

```json
{
  "type": "_SearchResultResponse",
  "results": [
    {
      "type": "Taxon",
      "id": "02IiNr0GOoKiPMLHurXARbcQUHxEJVbY9UJPL",
      "data": {
        "name": "Gloriosa",
        "path": "/Plantae/Tracheophytes/Angiosperms/Monocots/Liliales/Colchicaceae/Gloriosa",
        "scenario": "general.botanicExample",
        "link": "https://en.wikipedia.org/wiki/Gloriosa_(plant)",
        "description": "Gloriosa is a genus of 12 species in the plant family Colchicaceae, and includes the formerly recogn...",
        "createdAt": "2026-12-05T12:16:05+00:00",
        "updatedAt": "2026-12-05T12:16:05+00:00"
      },
      "metadata": {
        "score": 4.5698752
      }
    },
    {
      "type": "Plant",
      "id": "02IiNr0GOoKiPMLPDlPbKrlR4vnBQsvSBSkU5",
      "data": {
        "images": [
          {
            "description": "",
            "imageLinkFull": "https://upload.wikimedia.org/wikipedia/commons/f/fe/Flame_lily.png",
            "imageLinkThumbnail": "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Flame_lily.png/500px-Flame_lily.png",
            "link": "https://en.wikipedia.org/wiki/File:Flame_lily.png"
          },
          {
            "description": "Gloriosa superba at Mabvuku, Harare, Zimbabwe",
            "imageLinkFull": "https://upload.wikimedia.org/wikipedia/commons/b/bc/Gloriosa_superba_1.jpg",
            "imageLinkThumbnail": "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Gloriosa_superba_1.jpg/960px-Gloriosa_supe...",
            "link": "https://en.wikipedia.org/wiki/File:Gloriosa_superba_1.jpg"
          }
        ],
        "hasFile": true,
        "name": "Flame Lily",
        "scenario": "general.botanicExample",
        "scientificName": "G. superba",
        "link": "https://en.wikipedia.org/wiki/Gloriosa_superba",
        "description": "Gloriosa superba is a species of flowering plant in the family Colchicaceae. Common names include fl...",
        "createdAt": "2026-12-05T12:16:05+00:00",
        "updatedAt": "2026-12-05T12:16:05+00:00"
      },
      "file": {
        "contentLength": 90681,
        "extension": "jpg",
        "mimeType": "image/jpeg",
        "hash": {
          "sha256": "72e3e3770db795b2f04f0b5bb0be43e74df70de4faa0bf83a706bbde29e4ea0b"
        }
      },
      "metadata": {
        "score": 4.001134
      }
    },
    {
      "type": "Taxon",
      "id": "02IiNr0GOoKiPMLMTxO45AnoTbb6AIdILXae9",
      "data": {
        "name": "Lilieae",
        "path": "/Plantae/Tracheophytes/Angiosperms/Monocots/Liliales/Liliaceae/Lilioideae/Lilieae",
        "scenario": "general.botanicExample",
        "link": "https://en.wikipedia.org/wiki/Lilieae",
        "description": "The Lilieae are a monophyletic tribe of monocotyledon perennial, herbaceous mainly bulbous flowering...",
        "createdAt": "2026-12-05T12:16:01+00:00",
        "updatedAt": "2026-12-05T12:16:01+00:00"
      },
      "metadata": {
        "score": 3.7523305
      }
    },
    "... 17 more items omitted (shapes seen: type:Taxon, type:Plant) ..."
  ],
  "debug": [
    {
      "input": {
        "type": "elasticsearch-query-dsl-mixin",
        "query": {
          "match": {
            "description": {
              "query": "lily"
            }
          }
        },
        "parameters": {
          "stepResults": []
        }
      },
      "step-handler": "elasticsearch-query-dsl-mixin (App\\EventSystem\\SearchStep\\EventListener\\ElasticsearchQueryDslMixinSe...",
      "start": 1796473118.512843,
      "indices": "*",
      "combinedQuery": {
        "_source": [
          "_id"
        ],
        "from": 0,
        "size": 25,
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "description": {
                    "query": "lily"
                  }
                }
              }
            ],
            "filter": [
              {
                "bool": {
                  "should": [
                    {
                      "terms": {
                        "_groupsWithSearchAccess.keyword": []
                      }
                    },
                    {
                      "term": {
                        "_usersWithSearchAccess.keyword": {
                          "value": "02IiNr0GOoKiPMM5A0eq9jHk2upaJezsVtWjL"
                        }
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        }
      },
      "end": 1796473118.522505,
      "duration": 0.009662
    },
    {
      "input": {
        "type": "element-hydration",
        "query": null,
        "parameters": {
          "stepResults": [
            {
              "elements": [
                {
                  "id": "02IiNr0GOoKiPMLHurXARbcQUHxEJVbY9UJPL",
                  "type": "Taxon",
                  "metadata": {
                    "score": 4.5698752
                  }
                },
                {
                  "id": "02IiNr0GOoKiPMLPDlPbKrlR4vnBQsvSBSkU5",
                  "type": "Plant",
                  "metadata": {
                    "score": 4.001134
                  }
                },
                {
                  "id": "02IiNr0GOoKiPMLMTxO45AnoTbb6AIdILXae9",
                  "type": "Taxon",
                  "metadata": {
                    "score": 3.7523305
                  }
                },
                "... 17 more items omitted (shapes seen: type:Taxon, type:Plant) ..."
              ],
              "totalHits": 20,
              "maxScore": 4.5698752
            }
          ]
        }
      },
      "step-handler": "element-hydration (App\\EventSystem\\SearchStep\\EventListener\\ElementHydrationSearchStepEventListener)",
      "start": 1796473118.522813,
      "accessibleElementIds": [
        "02IiNr0GOoKiPMLHurXARbcQUHxEJVbY9UJPL",
        "02IiNr0GOoKiPMLPDlPbKrlR4vnBQsvSBSkU5",
        "02IiNr0GOoKiPMLMTxO45AnoTbb6AIdILXae9",
        "... 17 more items omitted (shapes seen: string) ..."
      ],
      "end": 1796473118.564486,
      "duration": 0.041673
    }
  ]
}
```

##### Response Headers

```http
Access-Control-Allow-Headers: Authorization, Content-Type, Prefer
Access-Control-Allow-Methods: OPTIONS, POST
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Preference-Applied
Allow: OPTIONS, POST
Cache-Control: no-cache, private
Content-Type: application/json; charset=utf-8
Date: Thu, 03 Dec 2026 12:24:50 GMT
Transfer-Encoding: chunked
Vary: Prefer, Accept-Encoding
X-Powered-By: Ember Nexus API
```

The Debug tab shows how the query was executed, including the relevance score of every result. More examples can be
found in the [reference](/reference/search/elasticsearch-query-dsl-mixin.md#examples).

## With access to Kibana

If you have access to the Elasticsearch database and Kibana, then you can use
[Kibana's console](https://www.elastic.co/docs/explore-analyze/query-filter/tools/console)
to experiment and test custom queries, as well as explore the data schemas of Elasticsearch.\
Note that **Kibana's console requires a full query**, while Ember Nexus API's [`POST /search`](/api/post-search.md)
endpoint only accepts the **content of the `query` property** (short form) or an object with the property `query` (long
form, which also supports `highlight` and `sort`, see the
[reference](/reference/search/elasticsearch-query-dsl-mixin.md#step-query)):

#### Ember Nexus

```bash
curl -X POST \
  -H 'Authorization: Bearer secret-token:PIPeJGUt7c00ENn8a5uDlc' \
  -H 'Content-Type: application/json' \
  -d '{
        "debug": true,
        "steps": [
          {
            "type": "elasticsearch-query-dsl-mixin",
            "query": {
              "match": {
                "description": {
                  "query": "lily"
                }
              }
            }
          },
          {
            "type": "element-hydration"
          }
        ]
      }' \
  https://api.example.com/search
```

#### Kibana's console

```txt
GET /_search
{
  "query": {
    "match": {
      "description": {
        "query": "lily"
      }
    }
  }
}
```

## Without access to Kibana

If you do not have direct access, you need to build the query blindly. As small errors can break the whole query, it is
recommended to slowly build your query up, and to test it frequently. If a query is rejected, the response explains the
problem, see [understanding search errors](/guide/search/understanding-search-errors.md).

As the base of your query you can use one of the provided
[examples](/reference/search/elasticsearch-query-dsl-mixin.md#examples).

Generative AI like Claude and its competitors are usually experienced enough to help with Elasticsearch query
generation.

## Parameterized Elasticsearch queries

Elasticsearch's Query DSL has no native concept of parameters. To still let client-provided data flow into a query,
Ember Nexus lets you write `{{your_expression}}` anywhere inside the `query` object; the expression is evaluated
against the step's parameters and its result replaces the placeholder before the query reaches Elasticsearch.\
See [parameters and expressions](/guide/search/parameters-and-expressions.md) for how parameters flow between search
steps, the [reference page](/reference/search/elasticsearch-query-dsl-mixin.md#parameterized-elasticsearch-queries) for a
worked example, and [Expr Lang](/reference/formats-and-languages/expr-lang.md) for the full expression syntax.

---

Home: [Ember Nexus API docs](/index.md)

Full site map: [llms.txt](/llms.txt)

Previous: [Choosing a search step](/guide/search/choosing-a-search-step.md)

Next: [Writing a Cypher query](/guide/search/writing-a-cypher-query.md)
