0

I have a simple entry as such :

json.set a:1 . '{"name":"chloe", "age":26 , "foo" : {"bar" : 1}'

When creating an index I can go as far as

FT.CREATE ON JSON SCHEMA $.name AS name TEXT ..... 

However bar is a dynamic value and it can be changed. How can I create an index for a dynamic value under foo?

Hypothesis
  • 1,208
  • 3
  • 17
  • 43
  • So are you thinking data that looks like this: `{"name":"chloe", "age":26 , "foo" : {"bar" : 1}` `{"name":"chloe", "age":26 , "foo" : {"baz" : 1}` `{"name":"chloe", "age":26 , "foo" : {"qux" : 1}` And you want to get the value of `1` regardless of the property name? – Guy Royse Oct 12 '22 at 12:37
  • @GuyRoyse I wish to get the value of bar but bar is a variable and can be something else. I don't want to specify bar and wish to say any key that is the child of foo. – Hypothesis Oct 12 '22 at 13:52
  • @GuyRoyse, I have a similar situation as you described in the comment, can you propose a solution and we can assume that `bar, baz, qux` are on the top level. – JB's Apr 03 '23 at 20:30
  • @JB's I think that is my answer below. – Guy Royse Apr 04 '23 at 13:16

2 Answers2

0

You can use full JSONPath syntax to match fields in the JSON. If the property foo will always contain an object and that object will always contain one and only one property, you could use a JSONPath $.foo.* like this:


> FT.CREATE a:index ON JSON SCHEMA $.name AS name TEXT $.foo.* AS num NUMERIC
OK
> FT.SEARCH a:index "@num:[1 1]"
1) (integer) 1
2) "a:1"
3) 1) "$"
   2) "{\"name\":\"chloe\",\"age\":26,\"foo\":{\"bar\":1}}"

Note that this JSONPath could very well return multiple results if foo contains more than one property:

{
  "name" : "chloe",
  "age": 26,
  "foo": {
    "bar": 1,
    "baz": 13
  }
}

This could be desirable if your field is a TAG field and the JSON contained strings instead of numbers. But it won't work for a NUMERIC field.

Guy Royse
  • 2,739
  • 12
  • 9
0

You can try a JSONPath such as $.foo.*, which would match all immediate children of top-level foo.

If there are multiple children, support for multi values will be available with RediSearch 2.6.1.

If children are of different types, you can try using a filter to avoid indexing failures due to type mismatch, for example, '$.foo.[?(@.*>-999)]', to handle specific numeric values, or '$.foo.[?(@.*!="")]' to handle text values.

oshadmi
  • 136
  • 3