我有一个形式的数组:
[ { _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 :(得分: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/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
}
您最终可以通过查询来获得所需的结果:
POST people/bids/_search
{
"query": {
"filtered": {
"filter": {
"term": {
"state": "winning"
}
}
}
}
}
输出:
{
...
"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映射。