具有自身字段值

时间:2015-09-29 11:33:07

标签: 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" } ] } ]

price对象是嵌套类型。 “displayPrice”是“not_analyzed”。 prices.price.type是“not_analyzed”。

现在,我想在这里做两件事: 1.当用户搜索价格时,DSL查询应该能够找到并返回显示价格,例如,如果用户想要搜索显示价格在950到1150之间的商品,他应该同时获得商品1和商品2,如,对于commodity1,displayPrice为“price1”,price.type =“price1”的值为1000。 2.当用户想要按价格对商品进行分类时,DSL应该能够根据个别商品的displayPrice进行排序。

任何帮助/指示都会非常感激。

感谢。

=============================================== ===================

已编辑,有关要求的进一步详情:

非常感谢您完成问题并准备代码。我应该理想地完成它。我相信我错误地引用了我的问题。让我重新说一下这个问题: 我有两个来自给定数据集的要求:

  • 当用户搜索价格范围为950-1150的商品时,系统应首先检查“displayPrice”的值是什么,然后使用该值作为查询来查找具有“price.type”的“price”对象。因此,例如,对于commodity1,“displayPrice”是“price1”。 “价格”下的相应“价格”对象(“price.type”=“price1”)的金额为“1000”。所以,这个商品应该归还。同样,对于commodity2,“displayPrice”为“price2”。商品2中“价格”下的相应“价格”对象(“price.type”=“price2”)的金额为“1100”。所以,这个商品应该归还。
  • 当用户按价格对商品进行分类时,应选择“displayPrice”的值。具有“price.type”作为该值的“price”对象应该用于排序。因此,在commodity1中,它应该使用“1000”(对“price1”)而在commodity2中,它应该使用“1100”(对“price2”)。

1 个答案:

答案 0 :(得分:0)

我不得不稍微更改数据的结构,但我认为以下设置会为您提供所需的内容。

为了测试它,我创建了一个类型为"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"
            ]
         }
      ]
   }
} 

如果您不想退回整个文档,可以使用"fields" parameter,例如:

POST /test_index/_search
{
    "fields": [
       "name", "displayPrice"
    ], 
   "query": {
    ...   

以下是我用来测试它的代码:

http://sense.qbox.io/gist/8f60bcd96e27eedd0dc0af780d4e2f8bed028445