2017-04-14 50 views
1

我有以下查詢,它在給予我所有期間(嵌套)+房屋屬於它們,並具有指定期間的到期日期。 現在我想嘗試獲取某個房屋的抵達日期,但我無法弄清楚在Elasticsearch中如何做到這一點的語法。找到某個父母的嵌套結果

GET /houses/house/_search 
{ 
    "_source" : ["HouseId"], 
"query": { 
"nested": { 
    "path": "Periods", 
    "query": { 
    "bool": { 
     "must": [ 
     {"range": { 
      "Periods.ArrivalDate": { 
      "gte" : "2017-10-01", 
      "lt" : "2017-11-01" 
     } 
     } 
     } 
    ] 
    } 
    }, 
"inner_hits" : {} 
} 

} }

的映射是這樣的(縮短到希望相關部分)

{ 
"houses": { 
    "mappings": { 
    "house": { 
     "properties": { 
      "Periods": { 
       "type": "nested", 
       "properties": { 
       "ArrivalDate": { 
        "type": "date", 
        "format": "yyyy-MM-dd" 
       }, 

.... 
      "HouseId": { 
       "type": "keyword" 
      }, 

所以我想找到一個房子的可用arrivaldates具有一定HouseId在某個月內

+0

你的意思是像' 「_source」 :{「include」:「Period.ArrivalDate」}'? –

+0

不,我的意思是把結果限制在一個房子的時期。你的提示很好,可以解決我認爲的'''''''''黑客',但不是我的問題。這個想法是,我可以列出一個房子的可用時間。我已經有的查詢是用於搜索可用的房屋。 – Maarten

+0

'inner_hits'和'_source'過濾是兩回事。什麼是「房子」? –

回答

0

我想我已經想通了,但請讓我知道是否有更好的解決方案可用:

{ 
"_source":[ 
    "HouseCode", 
    "Country", 
    "Region" 
], 
"query":{ 
    "bool":{ 
    "must":[ 
     { 
      "match":{ 
       "HouseId":"someid" 
      } 
     }, 
     { 
      "nested":{ 
       "path":"Periods", 
       "query":{ 
       "range":{ 
        "Periods.ArrivalDate":{ 
         "gte":"2017-05-01", 
         "lt":"2017-06-01" 
        } 
       } 
       }, 
       "inner_hits":{ 
       "size":1000 
       } 
      } 
     } 
    ] 
    } 
} 
}