Suspect documentation example does not work

Hi,

I stumbled on the following example in the documentation:

from(bucket:"example-bucket")
  |> range(start:-1h)
  |> filter(fn: (r) =>
    r._measurement == "cpu" and
    r._field == "usage_system" and
    r.cpu == "cpu-total"
  )

If I try this in my DB it does not work. So I am wondering what type of key is the cpu entry (tag or field?). Because if it is a field, I think that the example is not working. Could one elaborate on this example and show the structure of the records behind it?

The example can be found here

@yyvus It’s a tag
So r.cpu == "cpu-total" is filtering all cpu that match with the "cpu_total" value

If you want to filter by field, the syntax is r._field == "something you want"

@NicolasDrapier Thanks a lot, it made it all clear now, but let me add this comment.

When you say filter by field, it means you can only SELECT the field (and retrieve a table full of all the records that have this field), but you cannot look at all the records that have the field’s value equal to a certain string. So

r._field == "something you want"

returns a table with the filed equal to "something you want", but you cannot ask to have the records with field equal to "something you want" and value of this field equal to "some other expression". Is it correct?

In other words, filtering by fields allows you to do a SELECT request, but you cannot do a SELECT * WHERE request.

If so, this is the point I struggled to understand from the documentation…

@yyvus
If you want to filter by value, you can do
r._value == "some value here" or using the wanted operator.

I see your question coming, but I don’t know if it is possible to filter with two fields like
r._field == "field1" and r._field == "field2"

1 Like

You’d have to use the or operator to return both fields. With the and operator, if any of the predicate expressions evaluate to false, the row is filtered out of the output. You could also use a regular expression.

r._field == "field1" or r._field == "field2"

// This is the same as:

r._field =~ /field[1-2]$/