2016-04-21 62 views
0

條件篩選任務聽起來很簡單:如果 文件字段等於特定值,添加過濾器,否則不能過濾應用。ElasticSearch:基於字段值

F.X.讓所有的水果從倉庫中,但如果fruit.type是蘋果讓他們交付8天前。

那是可能的彈性水平呢?

預先感謝您。

回答

0

有可能在elassticsearch中使用Nesting Boolean Queriesrange-sugug和date-math-magic做到這一點。

既然你沒有張貼您的映射或我將只使用下面的一些數據樣本數據:

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"}' 

則下面的查詢會做你想要什麼:

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" 
         } 
        } 
        } 
       ] 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
}' 

在SQL這大致翻譯成:

WHERE fruit <> 'apple' OR (fruit = 'apple' AND delivered < Now-7d)