【问题标题】:Elasticsearch aggregation on part of string, not full string部分字符串而不是完整字符串的 Elasticsearch 聚合
【发布时间】:2015-09-06 01:35:13
【问题描述】:

基本上,我在这里尝试做的是从分层存储的字符串中获取第二级下的类别。问题是层次结构的级别各不相同,一个产品类别可能有六个级别,而另一个只有四个级别,否则我只会实现预定义的级别。

我有一些产品的类别如下:

[
  {
    title: 'product one',
    categories: [
      'clothing/mens/shoes/boots/steel-toe'
    ]
  },
  {
    title: 'product two',
    categories: [
      'clothing/womens/tops/sweaters/open-neck'
    ]
  },
  {
    title: 'product three',
    categories: [
      'clothing/kids/shoes/sneakers/light-up'
    ]
  },
  {
    title: 'product etc.',
    categories: [
      'clothing/baby/bibs/super-hero'
    ]
  }, 
  ... more products
]

我正在尝试像这样获得聚合桶:

buckets: [
  {
    key: 'clothing/mens',
    ...
  },
  {
    key: 'clothing/womens',
    ...
  },
  {
    key: 'clothing/kids',
    ...
  },
  {
    key: 'clothing/baby',
    ...
  },
]

我尝试查看过滤器前缀、包含和排除项,但我找不到任何有效的方法。请有人指出我正确的方向。

【问题讨论】:

    标签: node.js elasticsearch aggregation


    【解决方案1】:

    您的category 字段应使用自定义分析器进行分析。也许您对category 有其他计划,所以我将添加一个仅用于聚合的子字段:

    {
      "settings": {
        "analysis": {
          "filter": {
            "category_trimming": {
              "type": "pattern_capture",
              "preserve_original": false,
              "patterns": [
                "(^\\w+\/\\w+)"
              ]
            }
          },
          "analyzer": {
            "my_analyzer": {
              "tokenizer": "keyword",
              "filter": [
                "category_trimming",
                "lowercase"
              ]
            }
          }
        }
      },
      "mappings": {
        "test": {
          "properties": {
            "category": {
              "type": "string",
              "fields": {
                "just_for_aggregations": {
                  "type": "string",
                  "analyzer": "my_analyzer"
                }
              }
            }
          }
        }
      }
    }
    

    测试数据:

    POST /index/test/_bulk
    {"index":{}}
    {"category": "clothing/womens/tops/sweaters/open-neck"}
    {"index":{}}
    {"category": "clothing/mens/shoes/boots/steel-toe"}
    {"index":{}}
    {"category": "clothing/kids/shoes/sneakers/light-up"}
    {"index":{}}
    {"category": "clothing/baby/bibs/super-hero"}
    

    查询本身:

    GET /index/test/_search?search_type=count
    {
      "aggs": {
        "by_category": {
          "terms": {
            "field": "category.just_for_aggregations",
            "size": 10
          }
        }
      }
    }
    

    结果:

       "aggregations": {
          "by_category": {
             "doc_count_error_upper_bound": 0,
             "sum_other_doc_count": 0,
             "buckets": [
                {
                   "key": "clothing/baby",
                   "doc_count": 1
                },
                {
                   "key": "clothing/kids",
                   "doc_count": 1
                },
                {
                   "key": "clothing/mens",
                   "doc_count": 1
                },
                {
                   "key": "clothing/womens",
                   "doc_count": 1
                }
             ]
          }
       }
    

    【讨论】:

    • 我只是在看那些,并认为可能有更简单的方法,但这应该可行。谢谢楼主!
    • 感谢安德烈的回答,再三考虑。看起来图案只会有两层深。有没有办法让我可以聚合任何层深?您会看到,在一种情况下,我可能只需要获得“level1/level2”深度,而在另一种情况下,我可能需要“level1/level2/level3”甚至“level1/level2/level3/level4”深度。
    • 如果您想要任何(和所有)“路径”,请查看path hierarchy tokenizer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 2014-08-29
    • 2019-10-12
    • 2018-11-29
    • 1970-01-01
    相关资源
    最近更新 更多