# How can I query many indexes based on a particular field value?

**URL:** <https://discuss.elastic.co/t/how-can-i-query-many-indexes-based-on-a-particular-field-value/367026>\
**Category:** Elasticsearch\
**Created:** [September 24, 2024, 9:21am UTC](https://discuss.elastic.co/t/how-can-i-query-many-indexes-based-on-a-particular-field-value/367026 "2024-09-24T09:21:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Zhang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/_zhang/32/137117_2.png) [@\_Zhang](https://discuss.elastic.co/u/_Zhang)\
**Post date:** [September 24, 2024, 9:21am UTC](https://discuss.elastic.co/t/how-can-i-query-many-indexes-based-on-a-particular-field-value/367026/1 "2024-09-24T09:21:34Z")

</div>

Background:

- Indexes: `index-apple`, `index-banana`, `index-orange`, etc. Each of them has a field called `type`.

I wanna query "apple" and "banana":

```auto
POST /index-apple,index-banana/_search
{
  "query": {
    "terms": {
      "type": ["apple", "banana"]
    }
  }
}

```

This query, in my opinion, would function as follows: look up "banana" or "apple" in two indexes.

How can I use a single query to look up "apple" and "banana" separately in their respective indexes?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 24, 2024, 11:56am UTC](https://discuss.elastic.co/t/how-can-i-query-many-indexes-based-on-a-particular-field-value/367026/2 "2024-09-24T11:56:56Z")

</div>

May be something like this:

```auto
PUT _scripts/fruit-template
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "terms": {
                      "type": [
                        "{{name}}"
                      ]
                    }
                  }
                ],
                "filter": [
                  {
                    "term": {
                      "_index": "index-{{name}}"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}
GET _msearch/template
{ }
{ "id": "fruit-template", "params": { "name": "banana" }}
{ }
{ "id": "fruit-template", "params": { "name": "apple" }}

```

But it's 2 separated queries at the end.

---

<div class="post-metadata">

**Author:** ![fm\_ferreira](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fm\_ferreira](https://discuss.elastic.co/u/fm_ferreira)\
**Post date:** [September 24, 2024, 12:19pm UTC](https://discuss.elastic.co/t/how-can-i-query-many-indexes-based-on-a-particular-field-value/367026/3 "2024-09-24T12:19:14Z")

</div>

How about using ES|QL ?

[ES|QL REST API | Elasticsearch Guide [8.15] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/esql-rest.html)

not exactly, but something like this :

> POST /\_query  
> {  
> "query": """  
> FROM _index-apple_, _index-banana_  
> | where "type" == "apple"  
> | LIMIT 5  
> """  
> }

---

<div class="post-metadata">

**Author:** ![\_Zhang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/_zhang/32/137117_2.png) [@\_Zhang](https://discuss.elastic.co/u/_Zhang)\
**Post date:** [September 24, 2024, 2:08pm UTC](https://discuss.elastic.co/t/how-can-i-query-many-indexes-based-on-a-particular-field-value/367026/4 "2024-09-24T14:08:57Z")

</div>

Thanks for you reply. But I don't think it is related to this question.
