简体   繁体   English

如何使用 RediSearch 和 RedisJSON 根据条件(比较运算符)查询文档?

[英]How to use RediSearch and RedisJSON to query documents based on a condition (comparison operators)?

I have the following data stored as json using RedisJSON and I want to query a particular set of rows based on input criteria using RediSearch.我使用 RedisJSON 将以下数据存储为 json,我想使用 RediSearch 根据输入条件查询一组特定的行。

Ex Data:防爆数据:

{
    "Ticker": "AAPL",
    "Name": "Apple Inc.",
    "returns": [
        { "Range": { "Begin": "9/6/2014", "End": "4/5/2020"}, "Value": "0.0231"},
        { "Range": { "Begin": "11/12/2011", "End": "14/9/2021"}, "Value": "1.455"},
        { "Range": { "Begin": "1/2/2016", "End": "25/7/2022"}, "Value": "0.436"},
        { "Range": { "Begin": "24/6/2012", "End": "18/5/2016"}, "Value": "2.756"},
        { "Range": { "Begin": "14/8/2017", "End": "8/7/2020"}, "Value": "0.945"},
        { "Range": { "Begin": "9/12/2019", "End": "22/10/2015"}, "Value": "4.256"},
    ]
}

For example if the input given is "1/1/2015", then I would like to fetch only the records which have "Range.Begin" less than the input value.例如,如果给定的输入是“1/1/2015”,那么我只想获取“Range.Begin”小于输入值的记录。 So it should return the following rows from the json document.因此它应该返回 json 文档中的以下行。

{ "Range": { "Begin": "9/6/2014", "End": "4/5/2020"}, "Value": "0.0231"},
{ "Range": { "Begin": "11/12/2011", "End": "14/9/2021"}, "Value": "1.455"},
{ "Range": { "Begin": "24/6/2012", "End": "18/5/2016"}, "Value": "2.756"}

How can I create an index using RediSearch with such a requirement?我如何使用 RediSearch 创建具有此类要求的索引?

Note: All the values stored in json are strings.注意:json 中存储的所有值都是字符串。 And it might not be possible to do a comparison on strings in this case.在这种情况下,可能无法对字符串进行比较。 So, please let me know if the comparison works if dates are changed to numerical values like "11122011" from "11/12/2011".因此,如果日期从“11/12/2011”更改为“11122011”等数值,请告诉我比较是否有效。

First you need to create an index that will index @Begin .首先,您需要创建一个索引@Begin的索引。

eg例如

 FT.CREATE myidx ON JSON PREFIX 1 ticker: SCHEMA $.Ticker.returns[*].Range.Begin AS begin TEXT

Then you can utilize this index on your query and last project only the relevant parts.然后您可以在您的查询中使用此索引,并仅对相关部分进行最后的项目。

eg例如

> ft.search myidx '@begin:24/6/2012' RETURN 3 "$.returns[?(@.Range.Begin=='24/6/2012')]" as d DIALECT 3
1) (integer) 1
2) "ticker:apple"
3) 1) "d"
   2) "[{\"Range\":{\"Begin\":\"24/6/2012\",\"End\":\"18/5/2016\"},\"Value\":\"2.756\"}]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM