【问题标题】:Elasticsearch Query with self field value具有 self 字段值的 Elasticsearch 查询
【发布时间】:2015-09-30 12:45:32
【问题描述】:

我的 elasticsearch 映射已创建以支持以下数据结构:

“商品”:[ { “名称”:“商品1”, "displayPrice" : "price1", “价格” : [ “价格” : { “类型”:“价格1”, “金额”:“1000” }, “价格” : { “类型”:“价格2”, “金额”:“1100” } “价格” : { “类型”:“价格 3”, “金额”:“1200” } ] }, { “名称”:“商品2”, "displayPrice" : "price2", “价格” : [ “价格” : { “类型”:“价格1”, “金额”:“1300” }, “价格” : { “类型”:“价格2”, “金额”:“1100” } “价格” : { “类型”:“价格 3”, “金额”:“1500” } ] } ]

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

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

任何帮助/指针将不胜感激。

谢谢。

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

已编辑,并附有要求的更多详细信息:

非常感谢您回答问题并准备代码。理想情况下,我应该这样做。我相信我错误地引用了我的问题。让我重新表述这个问题: 我对给定数据集有两个要求:

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

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我不得不稍微改变一下你的数据结构,但我认为下面的设置会给你想要的。

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

    【讨论】:

    • 非常感谢 Sloan 提供代码。请查看已编辑的问题。我相信,我在提出要求时不够清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多