# Filtering documents by an unknown value of a field

**URL:** <https://discuss.elastic.co/t/filtering-documents-by-an-unknown-value-of-a-field/256768>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [November 26, 2020, 12:29pm UTC](https://discuss.elastic.co/t/filtering-documents-by-an-unknown-value-of-a-field/256768 "2020-11-26T12:29:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonatelo](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jonatelo](https://discuss.elastic.co/u/jonatelo)\
**Post date:** [November 26, 2020, 12:29pm UTC](https://discuss.elastic.co/t/filtering-documents-by-an-unknown-value-of-a-field/256768/1 "2020-11-26T12:29:43Z")

</div>

Hi,

I'm trying to create a query to filter my documents by one (can be anyone) value from a field (in my case "host.name"). The point is that I don't know previously the unique values of this field. I need found these and choose one to be used in the query.

I had tried the below query using a painless script, but I have not been able to achieve the goal.

```auto
    {
      "sort" : [{"@timestamp": "desc"}, {"host.name": "asc"}],
      "query": {
        "bool": {
          "filter": {
            "script": {
              "script": {
                "source": """
                  String k = doc['host.name'][0];
                  return doc['host.name'].value == k;
                """,
                "lang": "painless"
              }
            }
          }
        }
      }
    }

```

I'll appreciate if any can help me improving this idea of suggesting me a new one.

---

<div class="post-metadata">

**Author:** ![egalpin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egalpin/32/79772_2.png) [@egalpin](https://discuss.elastic.co/u/egalpin)\
**Post date:** [November 26, 2020, 9:07pm UTC](https://discuss.elastic.co/t/filtering-documents-by-an-unknown-value-of-a-field/256768/2 "2020-11-26T21:07:05Z")

</div>

Is it that you want to retrieve unique values from this `host.name` field, and maybe present users with an option to select one of the values? If that's what you want to do, and assuming that `host.name` is a `keyword` field, I would recommend using 2 queries:

1. Use a [terms aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html) on the `host.name` field to retrieve unique values across all docs (optionally using a query to limit which docs match, like a time range)
2. Perform a second query with a [term query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html) using one of the values selected from the resulting aggregations of the first query.

---

<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:** [December 24, 2020, 9:07pm UTC](https://discuss.elastic.co/t/filtering-documents-by-an-unknown-value-of-a-field/256768/3 "2020-12-24T21:07:08Z")

</div>

Closed on December 24, 2020, 9:07pm UTC

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