2014-10-30 79 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