# Named queries confusion

**URL:** <https://discuss.elastic.co/t/named-queries-confusion/245122>\
**Category:** Elasticsearch\
**Created:** [August 16, 2020, 6:15am UTC](https://discuss.elastic.co/t/named-queries-confusion/245122 "2020-08-16T06:15:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![liamwhite1](https://avatars.discourse-cdn.com/v4/letter/l/57b2e6/32.png) [@liamwhite1](https://discuss.elastic.co/u/liamwhite1)\
**Post date:** [August 16, 2020, 6:15am UTC](https://discuss.elastic.co/t/named-queries-confusion/245122/1 "2020-08-16T06:15:34Z")

</div>

After reading the documentation for named queries, and attempting to perform some myself, I am thoroughly confused on how to name queries.

This doesn't work:

```auto
{
    "term": {
        "tag_id": 1,
        "_name": "test"
    }
}

{"reason": "[term] query doesn't support multiple fields, found [_name] and [tag_id]"}

```

Neither does this:

```auto
{
    "bool": {
        "must": [{"match_all": {}}],
    },
    "_name": "test"
}

{"reason": "[_name] query malformed, no start_object after query name"}

```

But this does:

```auto
{
    "terms": {
        "tag_id": [1],
        "_name": "test"
    }
}

```

The documentation says "Each filter and query can accept a `_name` in its top level definition." But based on what I observe, this doesn't seem to be the case. What are the rules for how named queries actually need to work?

---

<div class="post-metadata">

**Author:** ![liamwhite1](https://avatars.discourse-cdn.com/v4/letter/l/57b2e6/32.png) [@liamwhite1](https://discuss.elastic.co/u/liamwhite1)\
**Post date:** [August 16, 2020, 6:31am UTC](https://discuss.elastic.co/t/named-queries-confusion/245122/2 "2020-08-16T06:31:33Z")

</div>

After reading the [Elasticsearch source code](https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java), I found that this is the correct syntax for using the named queries for the two examples I gave:

```auto
    "term": {
        "tag_id": {
            "value": 1,
            "_name": "test"
        }
    }

```

```auto
{
    "bool": {
        "must": [{"match_all": {}}],
        "_name": "test"
    }
}

```

The documentation is confusing and should be updated to show how to name all of the leaf queries to save me the time of having to read the source code of Elasticsearch to figure out how to do it.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 13, 2020, 6:31am UTC](https://discuss.elastic.co/t/named-queries-confusion/245122/3 "2020-09-13T06:31:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
