创建我的elasticsearch映射是为了支持以下数据结构:
"commodities" : [ { "name" : "commodity1", "displayPrice" : "price1", "prices" : [ "price" : { "type" : "price1", "amount" : "1000" }, "price" : { "type" : "price2", "amount" : "1100" } "price" : { "type" : "price3", "amount" : "1200" } ] }, { "name" : "commodity2", "displayPrice" : "price2", "prices" : [ "price" : { "type" : "price1", "amount" : "1300" }, "price" : { "type" : "price2", "amount" : "1100" } "price" : { "type" : "price3", "amount" : "1500" } ] } ]
价格对象是嵌套类型的。"displayPrice“是not_analyzed。prices.price.type是"not_analyzed“。
现在,我想在这里做两件事: 1.当用户搜索价格时,DSL查询应该能够找到并返回显示价格,例如,如果用户想要搜索显示价格在950到1150之间的商品,他应该同时得到commodity1和commodity2,就像commodity1的displayPrice是"price1“和price.type="price1”有1000值一样。2.当用户希望按价格对商品进行排序时,DSL应该能够根据displayPrice对单个商品进行排序。
任何帮助/指示都会非常感谢。
谢谢。
==================================================================
编辑,并进一步详细说明所需资源:
非常感谢您仔细研究这个问题并准备代码。我应该做得很好的。我想我错引用了我的问题。让我重新表述这个问题:我从给定的数据集中有两个要求:
发布于 2015-09-29 07:42:34
我不得不稍微修改一下您的数据结构,但是我认为下面的设置会给您提供您想要的。
为了测试它,我创建了一个具有"commodities"
类型的索引,该索引具有嵌套的"prices"
数据结构:
PUT /test_index
{
"mappings": {
"commodities": {
"properties": {
"displayPrice": {
"type": "string"
},
"name": {
"type": "string"
},
"prices": {
"type": "nested",
"properties": {
"amount": {
"type": "integer"
},
"type": {
"type": "string"
}
}
}
}
}
}
}
还请注意,"amount"
有"integer"
类型。然后(在调整了金额和"prices"
嵌套对象的结构之后),我对您的两个文档进行了索引:
PUT /test_index/commodities/1
{
"name": "commodity1",
"displayPrice": "price1",
"prices": [
{
"type": "price1",
"amount": 1000
},
{
"type": "price2",
"amount": 1100
},
{
"type": "price3",
"amount": 1200
}
]
}
PUT /test_index/commodities/2
{
"name": "commodity2",
"displayPrice": "price2",
"prices": [
{
"type": "price1",
"amount": 1300
},
{
"type": "price2",
"amount": 1100
},
{
"type": "price3",
"amount": 1500
}
]
}
现在,这个查询似乎返回您所要求的内容:
POST /test_index/_search
{
"query": {
"filtered": {
"filter": {
"nested": {
"path": "prices",
"filter": {
"range": {
"prices.amount": {
"from": 950,
"to": 1150
}
}
}
}
}
}
},
"sort": [
{
"displayPrice": {
"order": "desc"
}
}
]
}
...
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "test_index",
"_type": "commodities",
"_id": "2",
"_score": null,
"_source": {
"name": "commodity2",
"displayPrice": "price2",
"prices": [
{
"type": "price1",
"amount": 1300
},
{
"type": "price2",
"amount": 1100
},
{
"type": "price3",
"amount": 1500
}
]
},
"sort": [
"price2"
]
},
{
"_index": "test_index",
"_type": "commodities",
"_id": "1",
"_score": null,
"_source": {
"name": "commodity1",
"displayPrice": "price1",
"prices": [
{
"type": "price1",
"amount": 1000
},
{
"type": "price2",
"amount": 1100
},
{
"type": "price3",
"amount": 1200
}
]
},
"sort": [
"price1"
]
}
]
}
}
如果不想返回整个文档,可以使用 parameter,例如:
POST /test_index/_search
{
"fields": [
"name", "displayPrice"
],
"query": {
...
下面是我用来测试它的代码:
http://sense.qbox.io/gist/8f60bcd96e27eedd0dc0af780d4e2f8bed028445
https://stackoverflow.com/questions/32842818
复制相似问题