在ElasticSearch中表达SQL嵌套条件

时间:2016-01-21 09:50:08

标签: elasticsearch

我有这个SQL片段:

and (
     (notrequired is not null and notrequired <= datedue)
     or 
     (datefulfilled is not null and datefulfilled <= datedue)
)

我试过这样表达:

""or"": [
    {{
        ""and"": [
            {{
                ""exists"": {{ ""field"": ""NotRequired"" }}
            }},
            {{
                ""script"": {{
                    ""script"": ""doc['NotRequired'].value <= doc['DateDue'].value""
                }}
            }}
        ],
        ""and"": [
            {{
                ""exists"": {{ ""field"": ""DateFulfilled"" }}
            }},
            {{
                ""script"": {{
                    ""script"": ""doc['DateFulfilled'].value <= doc['DateDue'].value""
                }}
            }}
        ]
    }}
]

最外面的and作为过滤器在已应用过滤器的聚合上完成。

aggs: { 
    "name": { 
        filter: {}, 
        aggs: { 
            "inner" : { 
                filter: {} 
             } 
        } 
    } 
}

内部过滤器是应用and sql片段内的所有内容的地方。因此,通过我的计算,因为您在过滤器内部进行过滤,然后应用了and

请原谅ES片段的转义。

1 个答案:

答案 0 :(得分:0)

我的搜索格式错误了:

""or"": [
    {{
        ""and"": [
            {{
                ""exists"": {{ ""field"": ""NotRequired"" }}
            }},
            {{
                ""script"": {{
                    ""script"": ""doc['NotRequired'].value <= doc['DateDue'].value""
                }}
            }}
        ]
    }}, 
    {{
        ""and"": [
            {{
                ""exists"": {{ ""field"": ""DateFulfilled"" }}
            }},
            {{
                ""script"": {{
                    ""script"": ""doc['DateFulfilled'].value <= doc['DateDue'].value""
                }}
            }}
        ]
    }}
]

每个and都需要在其自己的对象中!