1

i have added a json document into my redis db using JSON.set Below given is my Json SET command

{
 "books": [
{
  "title": "Peter Pan",
  "price": 8.95
},
{
  "title": "Moby Dick",
  "price": 12.99
}
]
}

JSON.SET myDoc $ '{"books": [{"title": "Peter Pan", "price": 8.95},{"title": "A Thousand Suns", "price": 8.15}, {"title": "Moby Dick", "price": 12.99}]}'

Now i want to search this specific array of objects to fetch me the records which have price greater and equal to 8

i have tried creating an index but it always return me 0 records Below given is my index

 FT.CREATE docIdx ON JSON SCHEMA $.myDoc.books.price AS price Numeric

1 Answer 1

3

in the way you propose the index, you're ignoring the array of elements that belong to the object book. One way could be:

FT.CREATE docIdx ON JSON SCHEMA $.myDoc.books[*].price AS price Numeric 

or

FT.CREATE docIdx ON JSON SCHEMA $.myDoc.books[0:].price AS price Numeric

another recommendation it's to use the prefix since all documents with the same prefix will be indexed by Redisearch.

Something like:

FT.CREATE docIdx PREFIX 1 myDoc ON JSON SCHEMA $.books[*].price AS price Numeric

Notice, since you are trying to match the condition within the same document when you search for your price condition (between 8 and 9), the result will be the entire document "myDoc" with all the books, since the "Peter Pan" and "A Thousand Suns" matches the query condition. Such as:

> FT.SEARCH docIdx "@price:[8 12]"
1) "1"
2) "myDoc"
3) 1) "$"
   2) "{\"books\":[{\"title\":\"Peter Pan\",\"price\":8.95},{\"title\":\"A Thousand Suns\",\"price\":8.15},{\"title\":\"Moby Dick\",\"price\":12.99}]}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.