elasticsearch 路径层次标记上的聚合,elasticsearch,navigation,e-commerce,aggregate,elasticsearch,Navigation,E Commerce,Aggregate" /> elasticsearch 路径层次标记上的聚合,elasticsearch,navigation,e-commerce,aggregate,elasticsearch,Navigation,E Commerce,Aggregate" />

elasticsearch 路径层次标记上的聚合

elasticsearch 路径层次标记上的聚合,elasticsearch,navigation,e-commerce,aggregate,elasticsearch,Navigation,E Commerce,Aggregate,在过去的几天里,我读了很多书,但是没有找到一个适合我的解决方案。还看到了一些使用字符串类型的东西,这在我使用的ES版本中是不推荐的 我正在使用Elasticsearch:5.6.4 我为一些文档编制了索引,并尝试使用分析器(path\u hierarchy-tokenizer),查看摄取节点,但似乎没有什么适合我的。 它是关于category_tags字段的(参见底部的示例)。我确实有可能按照我喜欢的方式重新构造它,如果有必要,我正在生成这些数据 我想要一个典型的电子商务导航,所以我认为这应该通

在过去的几天里,我读了很多书,但是没有找到一个适合我的解决方案。还看到了一些使用
字符串
类型的东西,这在我使用的ES版本中是不推荐的

我正在使用Elasticsearch:5.6.4

我为一些文档编制了索引,并尝试使用
分析器
path\u hierarchy-tokenizer
),查看
摄取节点
,但似乎没有什么适合我的。 它是关于category_tags字段的(参见底部的示例)。我确实有可能按照我喜欢的方式重新构造它,如果有必要,我正在生成这些数据

我想要一个典型的电子商务导航,所以我认为这应该通过category_标签上的聚合来实现?我创建了一个数组来显示文档可以有多个类别, 其中每个层次可以是任意深度的。我不认为它会比4或5级更深,但可能会发生

我的动态模板如下所示:

      ...
    "analyzer": {
      "my_path_hierarchy_analyzer": {
        "type": "custom",
        "tokenizer": "my_path_hierarchy_tokenizer"
      },
      "my_pipe_analyzer": {
        "type": "custom",
        "tokenizer": "my_pipe_tokenizer"
      }
    },
    "tokenizer": {
      "my_path_hierarchy_tokenizer": {
        "type": "path_hierarchy",
        "delimiter": "|"
      },
      "my_pipe_tokenizer": {
        "type": "pattern",
        "pattern": "|"
      }
    }
  }
},
"mappings": {
  "item": {
    "dynamic_templates": [
      {
        "category_tags_analyzed": {
          "match": "category_tags",
          "mapping": {
            "type": "text",
            "analyzer": "my_path_hierarchy_analyzer",
            "fields": {
              "textsearch": {
                "type": "text",
                "analyzer": "my_pipe_analyzer"
              }
            }
          }
        }
      },
      ...
文本
类型字段上执行aggs时,由于
字段数据
,它会抛出一个错误。此外,我认为我不应该在这里将其设置为
true
。在
关键字
类型字段上,它甚至没有为文档编制索引,会抛出一个错误。我猜这是不允许的

这些文件看起来像:

"hits": [
  {
    "_index" : "my_index",
    "_type" : "my_type",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      ...,
      "category_tags" : [
        "Men|Tops|Shirts",
        "Men|Sale",
        "Men|Whatever"
      ],
      ...
    }
  }
]
现在我不知道我是否必须以某种方式使用
路径层次标记器
,即
嵌套的
类型,两者的组合或ES提供的任何东西。 因此,甚至可以在
类别标签上进行术语聚合,并获得“有用”的结果吗

有用的是,数据是结构化的,这样我就可以使用它进行基于电子商务(树状)的导航。
因此,用户可以单击导航树(每次单击时,我都会向ES发送一个请求以对其进行筛选),并根据单击的内容显示结果。

我找到了一些关于此问题的优秀文章(和),并进行了一些试验。虽然这两篇文章引用了一个较旧的版本,但一些调整使ES 6能够正常工作

以下是对我有效的方法,我没有对每个项目使用多个类别进行测试(例如,您的数组示例),但它可能仍然有效:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "path-analyzer": {
          "tokenizer": "path-tokenizer"
        }
      },
      "tokenizer": {
        "path-tokenizer": {
          "type": "path_hierarchy",
          "delimiter": "|"
        }
      }
    }
  },
  "mappings": {
    "item": {
      "dynamic": "strict",
      "properties": {
        "category_path": {
          "type": "text",
          "analyzer": "path-analyzer",
          "search_analyzer": "keyword",
          "fielddata": true
        }
      }
    }
  }
}
您的聚合请求如下所示:

{
  "aggs": {
    "category": {
      "terms": {
        "field": "category_path"
      }
    }
  },
  "size": 0
}
你的结果是:

  "buckets": [
    {
      "key": "Men",
      "doc_count": 11
    },
    {
      "key": "Men|Sale",
      "doc_count": 4
    },
    {
      "key": "Men|Tops",
      "doc_count": 3
    },
    {
      "key": "Men|Tops|Shirts",
      "doc_count": 2
    }
  ]