elasticsearch 如何在弹性查询中从对象数组中获取一个元素

aor9mmx1  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(142)

我有一个产品索引,它有一个**offers字段。Offers是一个对象数组。我想在数组或新字段中返回一个offer乘以seller_id**。

输入

卖方标识=5

{
   "_index":"dev_products",
   "_type":"_doc",
   "_id":"138",
   "_score":1.0,
   "_source":{
      "is_adult":false,
      "status_id":3,
      "allow_publish":false,
      "name":"Consequuntur expedita sit perferendis est.",
      "category_id":816,
      "brand_id":363,
      "description":"Nec.",
      "type":3,
      "vendor_code":"4968258909901",
      "barcode":"98735976",
      "code":"consequuntur-expedita-sit-perferendis-est",
      "updated_at":"2022-11-15T10:42:33.000000Z",
      "created_at":"2022-11-15T10:42:33.000000Z",
      "id":138,
      "offers":[
         {
            "product_id":"138",
            "seller_id":"1",
            "sale_status":"2",
            "external_id":"1267631",
            "store_id":"2",
            "qty":"44",
            "storage_address":"",
            "base_price":"312.84",
            "updated_at":"2022-11-15T10:42:49.000000Z",
            "created_at":"2022-11-15T10:42:49.000000Z",
            "id":74
         },
         {
            "product_id":"138",
            "seller_id":"2",
            "sale_status":"1",
            "external_id":"2795841",
            "store_id":"2",
            "qty":"1",
            "storage_address":"",
            "base_price":"1812.3",
            "updated_at":"2022-11-15T10:44:50.000000Z",
            "created_at":"2022-11-15T10:44:50.000000Z",
            "id":76
         },
         {
            "product_id":"138",
            "seller_id":"3",
            "sale_status":"1",
            "external_id":"32219",
            "store_id":"1",
            "qty":"32",
            "storage_address":"",
            "base_price":"1556.25",
            "updated_at":"2022-11-15T10:50:16.000000Z",
            "created_at":"2022-11-15T10:50:16.000000Z",
            "id":77
         },
         {
            "product_id":"138",
            "seller_id":"4",
            "sale_status":"1",
            "external_id":"967427",
            "store_id":"1",
            "qty":"35",
            "storage_address":"",
            "base_price":"137.62",
            "updated_at":"2022-11-15T10:50:18.000000Z",
            "created_at":"2022-11-15T10:50:18.000000Z",
            "id":78
         },
         {
            "product_id":"138",
            "seller_id":"5",
            "sale_status":"2",
            "external_id":"209466",
            "store_id":"1",
            "qty":"45",
            "storage_address":"",
            "base_price":"187.03",
            "updated_at":"2022-11-15T10:50:19.000000Z",
            "created_at":"2022-11-15T10:50:19.000000Z",
            "id":79
         },
         {
            "product_id":"138",
            "seller_id":"6",
            "sale_status":"1",
            "external_id":"522912",
            "store_id":"1",
            "qty":"61",
            "storage_address":"",
            "base_price":"306.39",
            "updated_at":"2022-11-15T10:50:20.000000Z",
            "created_at":"2022-11-15T10:50:20.000000Z",
            "id":80
         }
      ]
   }
}

预期值:

{
   "_index":"dev_products",
   "_type":"_doc",
   "_id":"138",
   "_score":1.0,
   "_source":{
      "is_adult":false,
      "status_id":3,
      "allow_publish":false,
      "name":"Consequuntur expedita sit perferendis est.",
      "category_id":816,
      "brand_id":363,
      "description":"Nec.",
      "type":3,
      "vendor_code":"4968258909901",
      "barcode":"98735976",
      "code":"consequuntur-expedita-sit-perferendis-est",
      "updated_at":"2022-11-15T10:42:33.000000Z",
      "created_at":"2022-11-15T10:42:33.000000Z",
      "id":138,
      "offers":[
         {
            "product_id":"138",
            "seller_id":"5",
            "sale_status":"2",
            "external_id":"209466",
            "store_id":"1",
            "qty":"45",
            "storage_address":"",
            "base_price":"187.03",
            "updated_at":"2022-11-15T10:50:19.000000Z",
            "created_at":"2022-11-15T10:50:19.000000Z",
            "id":79
         }
      ]
   }
}

或预期:

{
   "_index":"dev_products",
   "_type":"_doc",
   "_id":"138",
   "_score":1.0,
   "_source":{
      "is_adult":false,
      "status_id":3,
      "allow_publish":false,
      "name":"Consequuntur expedita sit perferendis est.",
      "category_id":816,
      "brand_id":363,
      "description":"Nec.",
      "type":3,
      "vendor_code":"4968258909901",
      "barcode":"98735976",
      "code":"consequuntur-expedita-sit-perferendis-est",
      "updated_at":"2022-11-15T10:42:33.000000Z",
      "created_at":"2022-11-15T10:42:33.000000Z",
      "id":138,
      "offer":{
         "product_id":"138",
         "seller_id":"5",
         "sale_status":"2",
         "external_id":"209466",
         "store_id":"1",
         "qty":"45",
         "storage_address":"",
         "base_price":"187.03",
         "updated_at":"2022-11-15T10:50:19.000000Z",
         "created_at":"2022-11-15T10:50:19.000000Z",
         "id":79
      }
   }
}

谢谢你的帮助

blmhpbnm

blmhpbnm1#

如果offers字段是嵌套类型,您可以使用inner hits来获取列表中唯一匹配的对象。
您期望的对象将在“inner_hits”响应中出现。
查询

GET idx_nested/_search?filter_path=hits.hits
{
  "query": {
    "nested": {
      "path": "offers",
      "query": {
        "match": {
          "offers.seller_id": "5"
        }
      },
      "inner_hits": {}
    }
  }
}

回应:

{
  "hits": {
    "hits": [
      {
        "_index": "idx_nested",
        "_id": "kYyYf4QBgXg8h_rctd1z",
        "_score": 1.540445,
        "_source": {
          "is_adult": false,
          "status_id": 3,
          "allow_publish": false,
          "name": "Consequuntur expedita sit perferendis est.",
          "category_id": 816,
          "brand_id": 363,
          "description": "Nec.",
          "type": 3,
          "vendor_code": "4968258909901",
          "barcode": "98735976",
          "code": "consequuntur-expedita-sit-perferendis-est",
          "updated_at": "2022-11-15T10:42:33.000000Z",
          "created_at": "2022-11-15T10:42:33.000000Z",
          "id": 138,
          "offers": [
            {
              "product_id": "138",
              "seller_id": "1",
              "sale_status": "2",
              "external_id": "1267631",
              "store_id": "2",
              "qty": "44",
              "storage_address": "",
              "base_price": "312.84",
              "updated_at": "2022-11-15T10:42:49.000000Z",
              "created_at": "2022-11-15T10:42:49.000000Z",
              "id": 74
            },
            {
              "product_id": "138",
              "seller_id": "2",
              "sale_status": "1",
              "external_id": "2795841",
              "store_id": "2",
              "qty": "1",
              "storage_address": "",
              "base_price": "1812.3",
              "updated_at": "2022-11-15T10:44:50.000000Z",
              "created_at": "2022-11-15T10:44:50.000000Z",
              "id": 76
            },
            {
              "product_id": "138",
              "seller_id": "3",
              "sale_status": "1",
              "external_id": "32219",
              "store_id": "1",
              "qty": "32",
              "storage_address": "",
              "base_price": "1556.25",
              "updated_at": "2022-11-15T10:50:16.000000Z",
              "created_at": "2022-11-15T10:50:16.000000Z",
              "id": 77
            },
            {
              "product_id": "138",
              "seller_id": "4",
              "sale_status": "1",
              "external_id": "967427",
              "store_id": "1",
              "qty": "35",
              "storage_address": "",
              "base_price": "137.62",
              "updated_at": "2022-11-15T10:50:18.000000Z",
              "created_at": "2022-11-15T10:50:18.000000Z",
              "id": 78
            },
            {
              "product_id": "138",
              "seller_id": "5",
              "sale_status": "2",
              "external_id": "209466",
              "store_id": "1",
              "qty": "45",
              "storage_address": "",
              "base_price": "187.03",
              "updated_at": "2022-11-15T10:50:19.000000Z",
              "created_at": "2022-11-15T10:50:19.000000Z",
              "id": 79
            },
            {
              "product_id": "138",
              "seller_id": "6",
              "sale_status": "1",
              "external_id": "522912",
              "store_id": "1",
              "qty": "61",
              "storage_address": "",
              "base_price": "306.39",
              "updated_at": "2022-11-15T10:50:20.000000Z",
              "created_at": "2022-11-15T10:50:20.000000Z",
              "id": 80
            }
          ]
        },
        "inner_hits": {
          "offers": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 1.540445,
              "hits": [
                {
                  "_index": "idx_nested",
                  "_id": "kYyYf4QBgXg8h_rctd1z",
                  "_nested": {
                    "field": "offers",
                    "offset": 4
                  },
                  "_score": 1.540445,
                  "_source": {
                    "store_id": "1",
                    "updated_at": "2022-11-15T10:50:19.000000Z",
                    "storage_address": "",
                    "product_id": "138",
                    "qty": "45",
                    "base_price": "187.03",
                    "sale_status": "2",
                    "created_at": "2022-11-15T10:50:19.000000Z",
                    "external_id": "209466",
                    "id": 79,
                    "seller_id": "5"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

相关问题