Query only a subset of a string array

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

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:

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

What I did

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

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

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

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:

1 Like

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

1 Like

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.

1 Like