Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 部分字符串上的Elasticsearch聚合,而不是整个字符串_Node.js_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Aggregation - Fatal编程技术网 elasticsearch,aggregation,Node.js,elasticsearch,Aggregation" /> elasticsearch,aggregation,Node.js,elasticsearch,Aggregation" />

Node.js 部分字符串上的Elasticsearch聚合,而不是整个字符串

Node.js 部分字符串上的Elasticsearch聚合,而不是整个字符串,node.js,elasticsearch,aggregation,Node.js,elasticsearch,Aggregation,基本上,我在这里要做的是从一个分层存储的字符串中获取第二级类别。问题是层次结构的级别各不相同,一个产品类别可能有六个级别,而另一个只有四个级别,否则我只会实现预定义的级别 我有一些产品的分类如下: [ { title: 'product one', categories: [ 'clothing/mens/shoes/boots/steel-toe' ] }, { title: 'product two', categories:

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

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

[
  {
    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',
    ...
  },
]
我试图得到这样的聚合桶:

[
  {
    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',
    ...
  },
]

我试过按术语查看过滤器前缀、include和exclude,但找不到任何有效的方法。请有人给我指出正确的方向。

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

{
  "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'深度。如果你想要任何(和所有)“路径”,那么请查看。