Es ist möglich, dies in Elasticsearch unter Verwendung Nesting Boolean Queries mit einigen range -sugar und date-math -magic.
Da Sie haben Post nicht Ihre Mapping oder einige Beispieldaten Ich werde nur die folgenden Daten verwenden:
curl -XPOST http://localhost:9200/testing/foo/ -d '{ "fruit" : "orange", "delivered" : "2016-04-22"}'
curl -XPOST http://localhost:9200/testing/foo/ -d '{ "fruit" : "banana", "delivered" : "2016-03-22"}'
curl -XPOST http://localhost:9200/testing/foo/ -d '{ "fruit" : "orange", "delivered" : "2016-04-12"}'
curl -XPOST http://localhost:9200/testing/foo/ -d '{ "fruit" : "apple", "delivered" : "2016-04-12"}'
curl -XPOST http://localhost:9200/testing/foo/ -d '{ "fruit" : "apple", "delivered" : "2016-04-20"}'
Dann wird die folgende Abfrage wird tun, was Sie wollen:
curl -XGET "http://localhost:9200/testing/foo/_search" -d'
{
"query": {
"constant_score": {
"filter": {
"bool": {
"should": [
{
"bool": {
"must_not": {
"term": {
"fruit": "apple"
}
}
}
},
{
"bool": {
"must": [
{
"term": {
"fruit": "apple"
}
},
{
"range": {
"delivered": {
"lt": "now-7d"
}
}
}
]
}
}
]
}
}
}
}
}'
In SQL dies übersetzt sich grob zu:
WHERE fruit <> 'apple' OR (fruit = 'apple' AND delivered < Now-7d)