skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search