2014-10-30 88 views
0

我有一個數組這是形式的濾出結果:ElasticSearch:從陣列

[ { _index: 'people', 
    _type: 'items', 
    _id: '5450c69d9d3545a816df39d4', 
    _score: null, 
    _source: 
    { _id: '5450c69d9d3545a816df39d4', 
     number: '2062', 
     bids: [Object] }, 
    { _index: 'people', 
    _type: 'items', 
    _id: '5450c69d9d3545a816df396d', 
    _score: null, 
    _source: 
    { _id: '5450c69d9d3545a816df396d', 
     number: '2195', 
     bids: [Object] } 
] 

詳細本期特價貨品

{ _id: '5450c69d9d3545a816df391c', 
    number: '2004', 
    bids: 
    [ { collector: '5450c69a9d3545a816df311d', 
     state: 'losing', 
     createdAt: '2014-10-30T09:33:36.905Z', 
     amount: 10, 
     _id: '545205f01fc10e3816cb2073' }, 
    { collector: '5450c69a9d3545a816df311c', 
     amount: 20, 
     state: 'winning', 
     createdAt: '2014-10-30T09:29:38.561Z', 
     _id: '545205f01fc10e3816cb2074' } ] 
} 

欲應用過濾器,使得我可以過濾掉勝利和失敗的投標。我想要一個返回這種類型結果的查詢,即在這個項目上獲勝的收藏家。

{ collector: '5450c69a9d3545a816df311c', 
    amount: 20, 
    state: 'winning', 
    createdAt: '2014-10-30T09:29:38.561Z', 
    _id: '545205f01fc10e3816cb2074' } ] 

的映射是:

{ 
    "bids": { 
    "properties": { 
     "_id": { 
     "type": "string" 
     }, 
     "amount": { 
     "type": "long" 
     }, 
     "collector": { 
     "type": "string" 
     }, 
     "createdAt": { 
     "type": "date", 
     "format": "dateOptionalTime" 
     }, 
     "state": { 
     "type": "string" 
     } 
    } 
    } 
} 

查詢應該像collector=<id> AND bids.state="winning/losing"

會有什麼實際ElasticSearch查詢?

+0

你可以發佈你的映射嗎? 'bids'被映射爲'nested'或'object'? – ThomasC 2014-10-30 10:45:00

+0

' 「出價」:{ 「屬性」:{ 「_id」:{ 「類型」: 「串」 }, 「量」:{ 「類型」: 「長」 }, 「集電極「:{ 」類型「: 」串「 }, 」createdAt「:{ 」類型「: 」日期「, 」格式「: 」dateOptionalTime「 }, 」狀態「:{ 」類型「 :「string」 } }' 查詢應該像collector = AN D bids.state =「贏/輸」。對? – 2014-10-30 10:56:24

回答

0

根據我的理解,您希望ElasticSearch在其答案中只返回中標競標。你目前的映射是不可能的。

如果文檔有多個投標,使用過濾器是這樣的:

{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "term": { 
      "bids.state": "winning" 
     } 
     } 
    } 
    } 
} 

將不會返回它只有在狀態投標失去的項目。但是,如果有贏得失去的混合,將返回整個文檔:

{ 
    ... 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "people", 
      "_type": "items", 
      "_id": "Gpnvxhf_QtGIcaaAeKBiSA", 
      "_score": 1, 
      "_source": { 
       "number": 2004, 
       "bids": [ 
        { 
        "collector": "5450c69a9d3545a816df311c", 
        "state": "winning", 
        "createdAt": "2014-10-30T09:29:38.561Z", 
        "amount": 20 
        }, 
        { 
        "collector": "5450c69a9d3545a816df311d", 
        "state": "losing", 
        "createdAt": "2014-10-30T09:33:36.905Z", 
        "amount": 10 
        } 
       ] 
      } 
     } 
     ] 
    } 
} 

爲了讓只有bids對象返回,你可以使用一個單獨的類型,併成立了親子關係

這裏如下一個例子:

{ 
    "mappings": { 
    "items": { 
     "properties": { 
     "number": { 
      "type": "long" 
     } 
     } 
    }, 
    "bids": { 
     "_parent": { 
     "type": "items" 
     }, 
     "properties": { 
     "_id": { 
      "type": "string" 
     }, 
     "amount": { 
      "type": "long" 
     }, 
     "collector": { 
      "type": "string" 
     }, 
     "createdAt": { 
      "type": "date", 
      "format": "dateOptionalTime" 
     }, 
     "state": { 
      "type": "string" 
     } 
     } 
    } 
    } 
} 

然後,加入一些值:

POST people/bids/_search 
{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "term": { 
      "state": "winning" 
     } 
     } 
    } 
    } 
} 

,其輸出:

POST people/items/1 
{ 
    "number": 2004 
} 

POST people/bids?parent=1 
{ 
    "collector": "5450c69a9d3545a816df311d", 
    "state": "losing", 
    "createdAt": "2014-10-30T09:33:36.905Z", 
    "amount": 10 
} 

POST people/bids?parent=1 
{ 
    "collector": "5450c69a9d3545a816df311c", 
    "state": "winning", 
    "createdAt": "2014-10-30T09:29:38.561Z", 
    "amount": 20 
} 

可以最終通過查詢等具有期望的結果:

{ 
    ... 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "people", 
      "_type": "bids", 
      "_id": "BV22GSsaRh6gHukVxL6tVw", 
      "_score": 1, 
      "_source": { 
       "collector": "5450c69a9d3545a816df311c", 
       "state": "winning", 
       "createdAt": "2014-10-30T09:29:38.561Z", 
       "amount": 20 
      } 
     } 
     ] 
    } 
} 

你可以在這裏找到更多關於relationships的信息,更具體地說是關於親子here

請注意,父母 - 子女關係是最後的解決方案,因爲他們在內存中保存父母的id地圖。

+0

謝謝@ Tom83! – 2014-10-31 09:31:40