首页 > 解决方案 > 在聚合和某些条件后返回产品 ID

问题描述

我有一个带有标识符 ( _id) 的产品列表。我需要以某种方式返回 _id满足聚合条件和其他一些条件的所有产品标识符(索引中有很多数据)。

我的查询:

GET dev_products/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "category_id": {
              "value": 2233
            }
          }
        },
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "properties.char_id": {
                        "value": 347
                      }
                    }
                  },
                  {
                    "term": {
                      "properties.char_value_id": {
                        "value": 3480
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "properties": {
      "nested": {
        "path": "properties"
      },
      "aggs": {
        "char_values_string": {
          "terms": {
            "field": "properties.char_value_string",
            "size": 10000
          },
          "aggs": {
            "char_ids": {
              "terms": {
                "field": "properties.char_id",
                "size": 10000
              }
            }
          }
        },
        "min_max_char_values_numeric": {
          "terms": {
            "field": "properties.char_id",
            "size": 10000
          },
          "aggs": {
            "min_char_value_numeric": {
              "min": {
                "field": "properties.char_value_numeric"
              }
            },
            "max_char_value_numeric": {
              "max": {
                "field": "properties.char_value_numeric"
              }
            }
          }
        }
      }
    },
    "min_price": {
      "min": {
        "field": "price"
      }
    },
    "max_price": {
      "max": {
        "field": "price"
      }
    }
  }
}

标签: elasticsearch

解决方案


我找到了我的问题的答案(我product_ids在查询末尾添加)并且它有效:

GET dev_products/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "category_id": {
              "value": 2233
            }
          }
        },
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "properties.char_id": {
                        "value": 347
                      }
                    }
                  },
                  {
                    "term": {
                      "properties.char_value_id": {
                        "value": 3480
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "properties": {
      "nested": {
        "path": "properties"
      },
      "aggs": {
        "char_values_string": {
          "terms": {
            "field": "properties.char_value_string",
            "size": 10000
          },
          "aggs": {
            "char_ids": {
              "terms": {
                "field": "properties.char_id",
                "size": 10000
              }
            }
          }
        },
        "min_max_char_values_numeric": {
          "terms": {
            "field": "properties.char_id",
            "size": 10000
          },
          "aggs": {
            "min_char_value_numeric": {
              "min": {
                "field": "properties.char_value_numeric"
              }
            },
            "max_char_value_numeric": {
              "max": {
                "field": "properties.char_value_numeric"
              }
            }
          }
        }
      }
    },
    "min_price": {
      "min": {
        "field": "price"
      }
    },
    "max_price": {
      "max": {
        "field": "price"
      }
    },
    "product_ids": {
      "terms": {
        "field" : "_id",
        "size": 10000
      }
    }
  }
}

推荐阅读