elasticsearch 7 中的同义词聚合 - 基于术语

Synonyms aggregation in elasticsearch 7 - term based

我正在尝试聚合字段,但字段类似于 Med 和 Medium。我不希望两者都出现在我的聚合结果中,只有其中一个应该出现。我尝试使用同义词,但它似乎不起作用。 问题是:当基于术语时,如何连接或统一相似的聚合结果?

下面是我的作品。

映射与设置

{
"settings": {
    "index" : {
        "analysis" : {
            "filter" : {
                "synonym_filter" : {
                    "type" : "synonym",
                    "synonyms" : [
                        "medium, m, med",
                        "large, l",
                        "extra small, xs, x small"
                    ]
                }
            },
            "analyzer" : {
                "synonym_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : ["lowercase", "synonym_filter"] 
                }
            }
        }
    }
},
"mappings": {
    "properties": {
            "skus": {
                "type": "nested",
                "properties": {
                    "labels": {
                        "dynamic": "true",
                        "properties": {
                            "Color": {
                                "type": "text",
                                "fields": {
                                    "synonym": {
                                        "analyzer": "synonym_analyzer",
                                        "type": "text",
                                        "fielddata":true
                                    }
                                }
                            },
                            "Size": {
                                "type": "text",
                                "fields": {
                                    "synonym": {
                                        "analyzer": "synonym_analyzer",
                                        "type": "text",
                                        "fielddata":true
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
}}

聚合

{   
"aggs":{
    "sizesFilter": {
                "aggs": {
                    "sizes": {
                        "terms": {
                            "field": "skus.labels.Size.synonym"
                        }
                    }
                },
                "nested": {
                    "path": "skus"
                }
            }
}}

只有 一个 文档,我的聚合结果是

"aggregations": {
    "sizesFilter": {
        "doc_count": 1,
        "sizes": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "m",
                    "doc_count": 1
                },
                {
                    "key": "med",
                    "doc_count": 1
                },
                {
                    "key": "medium",
                    "doc_count": 1
                }
            ]
        }
    }
}

我通过将分析器中的 tokenizer 设置为“keyword”得到了它

{
  "analyzer" : {
                "synonym_analyzer" : {
                    "tokenizer" : "keyword",
                    "filter" : ["lowercase", "synonym_filter"] 
                }
              }
}