Home > Net >  Elasticsearch query for all values of field with group by
Elasticsearch query for all values of field with group by

Time:01-01

i am having trouble forming query to fetch all values with sql group by kind of thing.

so below is my data structure:

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"
}

Now, i want to fetch all the facets with their different values and count as well. Let's say

  1. productA has color blue, productB has color red
  2. productA has brand ABC, productB has brand XYZ

so, i want data which list all facets like:

  1. color: blue(200 count), red (12 count)
  2. brand: ABC(13 count), XYZ (99 count)

Also, different product will have different type of facet, like iphone will have color memory brand size, but a pen will have color and brand only (not memory/size).

Note: i'm using latest version of elastic

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

UPDATE 1: Below is the es mapping details

{
  "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"
          }
        }
      }
    }
  }
}

CodePudding user response:

This query first group by names then groups each name's values. By setting sizes, you can arrange number of facets you want and number of items in each facet. I think it does what you need.

Note that if you have too many documents and if performance matters, this query may perform bad.

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