拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Elasticsearch使用groupby查询栏位的所有值

Elasticsearch使用groupby查询栏位的所有值

白鹭 - 2022-01-24 1968 0 0

我在形成查询以通过某种方式使用 sql 组获取所有值时遇到问题。

所以下面是我的资料结构:

product index:

{
  "createdBy" : "61c1fcdd88dbad1920da8caf",
  "creationTime" : "2021-12-22T11:58:53.576932Z",
  "lastModifiedBy" : "61c1fcdd88dbad1920da8caf",
  "lastModificationTime" : "2021-12-22T11:58:53.576932Z",
  "id" : "61c312fdc6aa620a609db0b2",
  "title" : "string",
  "brand" : "string",
  "longDesc" : "string",
  "categoryId" : "string",
  "imageUrls" : [
    "string",
    "string"
  ],
  "keySpecs" : [
    "string",
    "string",
  ],
  "facets" : [
    {
      "name" : "color",
      "value" : "red"
    },
    {
      "name" : "storage",
      "value" : "16 GB"
    },
    {
      "name" : "brand",
      "value" : "Intex"
    }
  ],
  "categoryName" : "handsets"
}

现在,我想获取具有不同值的所有方面并进行计数。让我们说

  1. 产品A为蓝色,产品B为红色
  2. 产品 A 的品牌为 ABC,产品 B 的品牌为 XYZ

所以,我想要列出所有方面的资料,例如:

  1. 颜色:蓝色(200 支)、红色(12 支)
  2. 品牌:ABC(13支),XYZ(99支)

此外,不同的产品会有不同型别的刻面,比如 iphone 会有颜色记忆品牌大小,但笔只有颜色和品牌(不是存储器/大小)。

注意:我使用的是最新版本的弹性

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

更新 1:以下是 es 映射详细信息

{
  "settings": {
    "analysis": {
      "filter": {
        "english_stop": {
          "type": "stop",
          "stopwords": "_english_"
        },
        "english_keywords": {
          "type": "keyword_marker",
          "keywords": [
            "example"
          ]
        },
        "english_stemmer": {
          "type": "stemmer",
          "language": "english"
        },
        "english_possessive_stemmer": {
          "type": "stemmer",
          "language": "possessive_english"
        }
      },
      "analyzer": {
        "lalashree_standard_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "english_possessive_stemmer",
            "lowercase",
            "english_stop",
            "english_keywords",
            "english_stemmer"
          ]
        },
        "html_standard_analyzer": {
          "char_filter": [
            "html_strip"
          ],
          "tokenizer": "standard",
          "filter": [
            "english_possessive_stemmer",
            "lowercase",
            "english_stop",
            "english_keywords",
            "english_stemmer"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "createdBy": {
        "type": "keyword"
      },
      "creationTime": {
        "type": "date"
      },
      "lastModifiedBy": {
        "type": "keyword"
      },
      "lastModificationTime": {
        "type": "date"
      },
      "deleted": {
        "type": "boolean"
      },
      "deletedBy": {
        "type": "keyword"
      },
      "deletionTime": {
        "type": "date"
      },

      "title": {
        "type": "text",
        "analyzer": "lalashree_standard_analyzer",
        "fields": {
          "suggest": {
            "type": "completion"
          }
        }
      },
      "shortDesc": {
        "type": "text",
        "analyzer": "lalashree_standard_analyzer"
      },
      "longDesc": {
        "type": "text",
        "analyzer": "lalashree_standard_analyzer"
      },
      "categoryId": {
        "type": "keyword"
      },
      "searchDetails": {
        "type": "object",
        "properties": {
          "desc": {
            "type": "text",
            "analyzer": "lalashree_standard_analyzer"
          },
          "keywords": {
            "type": "text",
            "analyzer": "lalashree_standard_analyzer",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      "imageUrls": {
        "type": "keyword",
        "index": false
      },
      "keySpecs": {
        "type": "text",
        "analyzer": "lalashree_standard_analyzer"
      },
      "sections": {
        "type": "object",
        "properties": {
          "name": {
            "type": "text",
            "index": false
          },
          "shortDesc": {
            "type": "text",
            "analyzer": "lalashree_standard_analyzer"
          },
          "longDesc": {
            "type": "text",
            "analyzer": "lalashree_standard_analyzer"
          },
          "htmlContent": {
            "type": "text",
            "analyzer": "html_standard_analyzer"
          }
        }
      },
      "facets": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword"
          },
          "value": {
            "type": "keyword"
          }
        }
      },
      "specificationItems": {
        "type": "object",
        "properties": {
          "key": {
            "type": "text",
            "analyzer": "lalashree_standard_analyzer",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "values": {
            "type": "text",
            "analyzer": "lalashree_standard_analyzer"
          }
        }
      },
      "categoryName": {
        "type": "keyword"
      },
      "productFamily": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "keyword"
          },
          "familyVariantOptions": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "keyword"
              },
              "values": {
                "type": "keyword"
              }
            }
          },
          "productFamilyItems": {
            "type": "nested",
            "properties": {
              "baseProductId": {
                "type": "keyword"
              },
              "itemVariantInfoSet": {
                "type": "nested",
                "properties": {
                  "name": {
                    "type": "keyword"
                  },
                  "value": {
                    "type": "keyword"
                  }
                }
              }
            }
          }
        }
      },
      "rating": {
        "type": "float"
      },
      "totalReviewsCount": {
        "type": "long"
      },
      "stores": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "keyword"
          },
          "logo": {
            "type": "keyword",
            "index": false
          },
          "active": {
            "type": "boolean"
          },
          "name": {
            "type": "text"
          },
          "quantity": {
            "type": "long"
          },
          "rating": {
            "type": "float"
          },
          "totalReviewsCount": {
            "type": "long"
          },
          "price.mrp": {
            "type": "float"
          },
          "price.sp": {
            "type": "float"
          },
          "location.geoPoint": {
            "type": "geo_point"
          },
          "oos": {
            "type": "boolean"
          }
        }
      }
    }
  }
}

uj5u.com热心网友回复:

此查询首先按名称分组,然后对每个名称的值进行分组。通过设定大小,您可以安排您想要的构面数量和每个构面中的项目数量。我认为它可以满足您的需求。

请注意,如果您有太多档案并且性能很重要,则此查询的性能可能会很差。

{
  "size": 0, 
  "aggs": {
    "facets": {
      "nested": {
        "path": "facets"
      },
      "aggs": {
        "names": {
          "terms": {
            "field": "facets.name",
            "size": 10
          },
          "aggs": {
            "values": {
              "terms": {
                "field": "facets.value",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *