ElasticSearc - невозможно получить вложенный объект из многоуровневого вложенного объекта - PullRequest
0 голосов
/ 15 февраля 2019

У меня есть многоуровневый вложенный объект.первый вложенный объект - это категория, а внутри категории есть еще один вложенный объект - это группа.Поэтому я хочу получить отдельную категорию вместе с вложенными группами, используя запрос агрегации.мне удалось получить отдельную категорию, но не удалось получить сведения о группе.

Отображение:

  "mappings": {
  "doc": {
    "properties": {
      "categories": {
        "type": "nested",
        "properties": {
          "cat_id": {
            "type": "integer"
          },
          "cat_name": {
            "type": "keyword"
          },
          "cat_slug": {
            "type": "keyword"
          },
          "cat_type": {
            "type": "long"
          },
          "groups": {
            "type": "nested",
            "properties": {
              "group": {
                "type": "keyword"
              },
              "group_name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "id": {
                "type": "long"
              }
            }
          },
          "ordering": {
            "type": "integer"
          },
          "parent_id": {
            "type": "integer"
          },
          "parent_name": {
            "type": "keyword"
          },
          "parent_slug": {
            "type": "keyword"
          },
          "parent_type": {
            "type": "long"
          }
        }
      }
    }
  }
}

Пример данных:

    {
  "_index": "product",
  "_type": "doc",
  "_id": "18556",
  "_score": 1,
  "_source": {
    "sku": "GR0005P08",
    "product_id": 18556,
    "slug": "blue-garter-with-sexy-laces",
    "categories": [
      {
        "ordering": 10,
        "cat_id": 343,
        "cat_type": 1,
        "cat_slug": "t-thisr",
        "cat_name": "cat1"
      },
      {
        "ordering": 9999999,
        "cat_id": 2,
        "cat_type": 3,
        "cat_slug": "pajams",
        "cat_name": "pajams"
      },
      {
        "ordering": 5,
        "cat_id": 77,
        "cat_type": 3,
        "cat_slug": "accessories",
        "cat_name": "Accessories"
      },
      {
        "parent_name": "Pajams",
        "cat_name": "Night",
        "ordering": 1,
        "cat_id": 139,
        "parent_type": 3,
        "cat_slug": "night",
        "parent_id": 2,
        "groups": [
          {
            "id": 146,
            "group_name": "Shop By Style"
          },
          {
            "id": 481,
            "group_name": "Shop By Offer "
          }
        ],
        "parent_slug": "pajams",
        "cat_type": 1
      }
    ],
    "name": "love for pajams"
  }
}

Это мой агрегационный запрос:

    GET product/_search
{
  "_source": [
    "product_id"
  ],
  "query": {
    "nested": {
      "path": "categories",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "categories.cat_slug": "xyz"
              }
            }

          ]
        }
      }
    }
  },
  "aggs": {
    "categories": {
      "nested": {
        "path": "categories"
      },
      "aggs": {
        "distinct_categories.cat_name": {
          "terms": {
            "field": "categories.cat_name"
          },
          "aggs": {
            "categories.groups.group_name": {
              "terms": {
                "field": "categories.groups.group_name.keyword"
              }
            }
          }
        }
      }
    }
  }
}

И это мой ответ:

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 3.232121,
    "hits": [
      {
        "_index": "product",
        "_type": "doc",
        "_id": "15621",
        "_score": 3.232121,
        "_source": {
          "product_id": 15621
        }
      },
      {
        "_index": "product",
        "_type": "doc",
        "_id": "18556",
        "_score": 2.5758784,
        "_source": {
          "product_id": 18556
        }
      }
    ]
  },
  "aggregations": {
    "categories": {
      "doc_count": 98,
      "distinct_categories.cat_name": {
        "doc_count_error_upper_bound": 2,
        "sum_other_doc_count": 50,
        "buckets": [
          {
            "key": "Accessories",
            "doc_count": 8,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "T-shirt",
            "doc_count": 8,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "Sexy",
            "doc_count": 7,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "clothing",
            "doc_count": 6,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "Pants",
            "doc_count": 6,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "Colour Me",
            "doc_count": 4,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          },
          {
            "key": "Pajamas",
            "doc_count": 3,
            "categories.groups.group_name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          }
        ]
      }
    }
  }
}

Ответы [ 2 ]

0 голосов
/ 18 февраля 2019

Я обнаружил проблему с определением сопоставления групп после изменения запроса.

{
"type": "nested",
"properties": {
  "group_name": {
    "type": "keyword"
  },
  "id": {
    "type": "long"
      }
    }
  }
0 голосов
/ 15 февраля 2019

Возможно, вы попробуете "field": "categories.groups.group_name" вместо "field": "categories.groups.group_name.keyword" в вашей самой глубокой агрегации?

Редактировать:

Использование "categories.groups.group_name.keyword" - верный способ добиться этого.Проблема должна заключаться в том, что вам нужно будет добавить nested для каждого уровня вашей вложенной структуры для вашего запроса.

"aggs": {
"categories": {
  "nested": {
    "path": "categories"
  },
  "aggs": {
    "distinct_categories.cat_name": {
      "terms": {
        "field": "categories.cat_name"
      },
      "aggs": {
        "deeper_nested_agg": {
          "nested": {
            "path": "categories.groups"
          },
          "aggs": {
            "categories.groups.group_name": {
              "terms": {
                "field": "categories.groups.group_name.keyword"
              }
            }
          }
        }
      }
    }
  }
}

Пожалуйста, попробуйте.Надеюсь, это поможет!

...