首页 文章

ElasticSearch条款聚合订单案例不敏感

提问于
浏览
1

我试图在弹性搜索不区分大小写的情况下对术语聚合的桶进行排序 . 这是字段映射:

'brandName'       => [
    'type'     => 'string',
    'analyzer' => 'english',
    'index'    => 'analyzed',
    'fields'   => [
        'raw' => [
            'type'  => 'string',
            'index' => 'not_analyzed'
        ]
    ]
]

请注意,此处的数据结构适用于PHP .

聚合看起来像这样:

aggregations => [
    'brands' => [
        'terms' => [
            'field' => 'brandName.raw',
            'size'  => 0,
            'order' => ['_term' => 'asc']
        ]
    ]
]

这样可行,但生成的桶按字典顺序排列 .

我找到了一些有趣的文档here,它解释了如何做到这一点,但它是在对命中进行排序的背景下,而不是聚合桶 .

无论如何我试过了 . 这是我创建的分析器:

'analysis' => [
    'analyzer' => [
        'case_insensitive_sort' => [
            'tokenizer' => 'keyword',
            'filter' => [ 'lowercase' ]
        ]
    ]
]

这里是更新的字段映射,使用分析器称为“排序”的新子字段 .

'brandName'       => [
    'type'     => 'string',
    'analyzer' => 'english',
    'index'    => 'analyzed',
    'fields'   => [
        'raw' => [
            'type'  => 'string',
            'index' => 'not_analyzed'
        ],
        'sort' => [
            'type'  => 'string',
            'index' => 'not_analyzed',
            'analyzer' => 'case_insensitive_sort'
        ]
    ]
]

这是我的查询的更新聚合部分:

aggregations => [
    'brands' => [
        'terms' => [
            'field' => 'brandName.raw',
            'size'  => 0,
            'order' => ['brandName.sort' => 'asc']
        ]
    ]
]

这会生成以下错误: Invalid term-aggregator order path [brandName.sort]. Unknown aggregation [brandName] .

我接近了吗?可以进行这种聚合桶分类吗?

1 回答

  • 2

    简短的回答是,尚未支持对聚合进行此类高级排序,并且正在解决此问题(定于v2.0.0) .

    还有两点值得一提:

    • brandName.sort 子字段被声明为 not_analyzed ,同时设置 analyzer 也是矛盾的 .

    • 您得到的错误是因为 order 部分只能引用子聚合名称,而不是字段名称(即 brandName.sort 是字段名称)

相关问题