# Query only a subset of a string array

**URL:** <https://discuss.dgraph.io/t/query-only-a-subset-of-a-string-array/17440>\
**Category:** Dgraph\
**Created:** [June 21, 2022, 8:28am UTC](https://discuss.dgraph.io/t/query-only-a-subset-of-a-string-array/17440 "2022-06-21T08:28:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaime](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jaime/32/9618_2.png) [@Jaime](https://discuss.dgraph.io/u/Jaime)\
**Post date:** [June 21, 2022, 8:28am UTC](https://discuss.dgraph.io/t/query-only-a-subset-of-a-string-array/17440/1 "2022-06-21T08:28:04Z")

</div>

## What I want to do

Imagine I have nodes of type A, and those nodes have a long array of strings. I want to query the nodes that have a string in the array that matches my search but only return the matched array terms instead of the whole array.

example

```auto
A1 {
    strings: ["apple", "banana", "orange", "pear", "pineapple"]
}

A2 {
    strings: ["appleton", "london","liverpool"]
}

A3 {
    strings: ["ninja", "dinosaur", "unicorn", "starlord"]
}

```

if i query for “apple” I want dgraph to return something like this:

```auto
query: [
    A1: {
        strings: ["apple","pineapple"]
    },
    A2: {
        strings: ["appleton"]
    }

```

## What I did

```auto
query(func: regexp(strings,/apple/i) {
    strings
}

```

and this excludes A3, which is nice but it returns the whole strings array for each node

```auto
query: [
    A1: {
        strings: ["apple", "banana", "orange", "pear", "pineapple"]
    },
    A2: {
        strings: ["appleton", "london","liverpool"]
    }

```

Is there any way of make this work with the arrays?  
Thank you

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [June 23, 2022, 5:52pm UTC](https://discuss.dgraph.io/t/query-only-a-subset-of-a-string-array/17440/2 "2022-06-23T17:52:16Z")

</div>

In the current state of Dgraph, I don’t believe there is a way to do what you want.

Sets of strings can be used to filter nodes, but no logic to filter the sets themselves.

Furthermore, FYI from personal experience, regex function filters can become quite slow when you start working with a slightly large dataset. And there is also the restrictions that you cannot search for a regex if it is less than 3 characters long.

This might interest you:

> **[String Functions in DQL](https://discuss.dgraph.io/t/string-functions-in-dql/9940)**
>
> Experience Report for Feature Request What you wanted to do Use String Functions within DQL to do upserts and transform data on server side without client script. What you actually did Started this Topic 😉 After I double checked and searched the...

---

<div class="post-metadata">

**Author:** ![d4nielhdz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/d4nielhdz/32/7610_2.png) [@d4nielhdz](https://discuss.dgraph.io/u/d4nielhdz)\
**Post date:** [June 29, 2022, 4:18am UTC](https://discuss.dgraph.io/t/query-only-a-subset-of-a-string-array/17440/3 "2022-06-29T04:18:13Z")

</div>

You could create a lambda query or field resolver that filters the results after the results after the query and then returns them

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [June 20, 2023, 6:59pm UTC](https://discuss.dgraph.io/t/query-only-a-subset-of-a-string-array/17440/4 "2023-06-20T18:59:46Z")

</div>

Just to be clear, IMHO, a lambda will always be a work-around and never a solution. Lambdas come with their own unique set of faults, limitations, and added complexities that shouldn’t be needed.

Lambdas are useful in some situations, but when they get over-utilized and end up being a catch-all due to the lack of actual development of the needed enhancements and bug/security that the community has been asking for for years.
