首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >带有自字段值的Elasticsearch查询

带有自字段值的Elasticsearch查询
EN

Stack Overflow用户
提问于 2015-09-29 03:33:07
回答 1查看 84关注 0票数 0

创建我的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对单个商品进行排序。

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

谢谢。

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

编辑,并进一步详细说明所需资源:

非常感谢您仔细研究这个问题并准备代码。我应该做得很好的。我想我错引用了我的问题。让我重新表述这个问题:我从给定的数据集中有两个要求:

  • 当用户搜索价格范围为950-1150的商品时,系统应该首先检查"displayPrice“的值是什么,然后使用这个值作为查询来查找带有"price.type”的“价格”对象。因此,例如,对于commodity1,"displayPrice“是"price1”。“价格”项下相应的“价格”对象(带有"price.type"="price1")的数量为"1000“。所以,这批商品应该还回去。类似地,commodity2将"displayPrice“作为"price2”。commodity2中“price.type”(带有"price.type"="price2")下的相应“价格”对象的数量为"1100“。所以,这批商品应该还回去。
  • 当用户按价格对商品进行排序时,应该选择"displayPrice“的价值。以"price.type“作为此值的"price”对象应用于排序。因此,在commodity1中,它应该使用"1000“(对"price1"),在commodity2中应该使用"1100”(对"price2")。
EN

回答 1

Stack Overflow用户

发布于 2015-09-29 07:42:34

我不得不稍微修改一下您的数据结构,但是我认为下面的设置会给您提供您想要的。

为了测试它,我创建了一个具有"commodities"类型的索引,该索引具有嵌套的"prices"数据结构:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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"嵌套对象的结构之后),我对您的两个文档进行了索引:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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
      }
   ]
}

现在,这个查询似乎返回您所要求的内容:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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,例如:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
POST /test_index/_search
{
    "fields": [
       "name", "displayPrice"
    ], 
   "query": {
    ...   

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32842818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文