Flux: Check string within stored set/array

I’m wondering if there’s any way to filter an input string based on a set stored in influxdb2.
e.g: here’s the (Java) entity that is being stored in influxdb2

@Measurement(name = "blah_blah")
public class EntityName {
    ...
    @Column(timestamp = true)
    private Instant time;
    @Column(tag = true)
    private Set<String> tags;
}

The goal here is to filter based on tags, say if any stored record contains a tag: “tag1”. Based on my limited knowledge of flux, I was thinking of using contains but seems that it’s not working:

|> filter(fn: (r) => contains(value: "tag1", set: r["tags"]))

error:

 runtime error @8:6-8:64: filter: type conflict: [string] != string

I guess the reason is that r[“tags”] is being treated as a string, thus I tried to pass it to array function, but it also doesn’t work.

Thanks in advance.

Hi @nima,

thanks for using our client.

As you said, this configuration causes that the tags are saved as a string. The corresponding LineProtocol looks like: blah_blah,tags=[tag1\,\ tag2] value=6i 1643897137.

You have to separate your tags to string fields:

    @Measurement(name = "blah_blah")
    public class EntityName {
        @Column(timestamp = true)
        private Instant time;
        @Column(tag = true)
        private String tagA;
        @Column(tag = true)
        private String tagB;
        @Column
        private long value;
    }

and then your Flux queries will looks like:

|> filter(fn: (r) => contains(value: "tag1", set: [r["tagA"], r["tagB"]]))

Regards

Hi @bednar
Thanks for the quick reply.

I think it’s not possible, since field: tags here will have variable length & might accept different values, say, an entity might have 2 tags & another one might contain 10 tags. Is there any way to search for array fields in influxdb2 ?

What do you think by tag? Is it a structure corresponding to the definition of InfluxDB’s tag: key-value - location=north,type=production,state=ready? Or is it just values like - north, production, ready?

Sorry, I should have thought that the name could be misleading. It’s definitely not the influxdb tag, but just random set of string values as you mentioned (second option) received by user

So, I’m guessing there’s no way to achieve this (searching for a term within a saved array of values) in InfluxDB (since the array itself is being stored as string?). If I"m mistaken, please let me know.
Regards.

What about encode the tags to something like: ##tag1##tag2##tag3## and then use |> filter(fn: (r) => contains(value: "##tag2##", set: r["tags"]))?