# Can I automate this task in elastic without leveraging external programming?

**URL:** <https://discuss.elastic.co/t/can-i-automate-this-task-in-elastic-without-leveraging-external-programming/358237>\
**Category:** Elasticsearch\
**Created:** [April 25, 2024, 8:52pm UTC](https://discuss.elastic.co/t/can-i-automate-this-task-in-elastic-without-leveraging-external-programming/358237 "2024-04-25T20:52:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jingyi\_Wang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jingyi_wang/32/121787_2.png) [@Jingyi\_Wang](https://discuss.elastic.co/u/Jingyi_Wang)\
**Post date:** [April 25, 2024, 8:52pm UTC](https://discuss.elastic.co/t/can-i-automate-this-task-in-elastic-without-leveraging-external-programming/358237/1 "2024-04-25T20:52:34Z")

</div>

One elastic index1 have documents like:

```auto
{username: u1, analysisId: 1}
{username: u1, analysisId: 2}
{username: u2, analysisId: 3}
{username: u3, analysisId: 4}
{username: u3, analysisId: 5}

```

In this index, analysis Id is like primary key and unique, while documents with multiple analysisId corresponds to one username, e.g., analysisId with 1 and 2 belongs to username, u1.

Another elastic index2 have documents like:

```auto
{username: u1}
{username: u2}

```

In this index, username is unique, like primary key.

Is it possible to filter all documents under index 1 using the primary key username in index 2 ONLY using elastic without external programming?

The steps would be like:  
Step one:

```auto
GET /index2/_search
{
  "size": 1000, // Adjust based on the expected number of usernames
  "_source": ["username"]
}

```

Step two: Then have the returned result from the script above and being used as the parameter for "all\_usernames" in the second script:

```auto
PUT /index2/_doc/usernames_list
{
  "all_usernames": "the _source from the first script"
}

```

Then finally, Query `index1` using the `terms lookup` with the `id` of the document created:

```auto
GET /index1/_search
{
  "query": {
    "terms": {
      "username": {
        "index": "index2",
        "id": "usernames_list",
        "path": "all_usernames"
      }
    }
  }
}

```

I checked with ChatGPT with an response that "Automating the those steps you mentioned directly in Elasticsearch isn't possible without external scripting or tooling, as Elasticsearch does not support chaining queries internally." I have experienced lots of BS from ChatGPT regarding Elastic, so now seeking source of truth from real person. Thanks!

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 26, 2024, 4:31am UTC](https://discuss.elastic.co/t/can-i-automate-this-task-in-elastic-without-leveraging-external-programming/358237/2 "2024-04-26T04:31:26Z")

</div>

In this instance ChatGPT is actally correct. Elasticsearch does not support joins or chaining of queries. A common way to get around this in Elasticsearch is to denormalise data, e.g. storing the data held about users in index2 in each of the documents in index 1.

---

<div class="post-metadata">

**Author:** ![Jingyi\_Wang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jingyi_wang/32/121787_2.png) [@Jingyi\_Wang](https://discuss.elastic.co/u/Jingyi_Wang)\
**Post date:** [April 26, 2024, 12:52pm UTC](https://discuss.elastic.co/t/can-i-automate-this-task-in-elastic-without-leveraging-external-programming/358237/3 "2024-04-26T12:52:03Z")

</div>

Thanks for responding. Can you be more specific in " A common way to get around this in Elasticsearch is to denormalise data"? The username in index2 is already stored in each documents in index1.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 26, 2024, 12:56pm UTC](https://discuss.elastic.co/t/can-i-automate-this-task-in-elastic-without-leveraging-external-programming/358237/4 "2024-04-26T12:56:46Z")

</div>

Why do you then need to query index2?

---

<div class="post-metadata">

**Author:** ![Jingyi\_Wang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jingyi_wang/32/121787_2.png) [@Jingyi\_Wang](https://discuss.elastic.co/u/Jingyi_Wang)\
**Post date:** [April 26, 2024, 1:42pm UTC](https://discuss.elastic.co/t/can-i-automate-this-task-in-elastic-without-leveraging-external-programming/358237/5 "2024-04-26T13:42:42Z")

</div>

To query index2 is to filter out those documents in index1 that are not listed in index2.
