如何在Elasticsearch的结果中包括映射字段子字段?

时间:2018-12-22 17:16:07

标签: elasticsearch

对于聚合,我具有我的字段的原始值。但是我无法在查询中访问此值。例如,在我的情况下,我有一个brand汤米·希尔菲格(Tommy Hilfiger),它的原始值是汤米·希尔菲格(Tommy-hilfiger),为brand.keyword。如何在搜索结果中包含此值?

'body' => [
    'settings'  => [
        'analysis'  => [
            'filter'        => [
                'remove_spaces_inside'  => [
                    'type'      => 'pattern_replace',
                    'pattern'   => '\\s+',
                    'replacement'   => ' '
                ],
                'convert_spaces'    => [
                    'type'      => 'pattern_replace',
                    'pattern'   => '\\s+',
                    'replacement'   => '-'
                ],
            ],
            'char_filter'   => [
                'convert_amp'   => [
                    'type'      => 'pattern_replace',
                    'pattern'   => '&',
                    'replacement'   => 'and'
                ]
            ],
            'analyzer'  => [
                'slug'  => [
                    'char_filter'   => ['convert_amp'],
                    'tokenizer'     => 'keyword',
                    'filter'        => ['trim', 'lowercase', 'asciifolding', 'remove_spaces_inside', 'convert_spaces']
                ],
                'format'    => [
                    'char_filter'   => ['convert_amp'],
                    'tokenizer'     => 'keyword',
                    'filter'        => ['trim', 'remove_spaces_inside']
                ]
            ]
        ]
    ],
    'mappings' => [
        'my_type' => [
            'properties' => [
                'brand' => [
                    'type'      => 'string',
                    'fields'    => [
                        'keyword'   => [
                            'type'              => 'string',
                            'analyzer'          => 'slug',
                            'index_options'     => 'docs',
                        ]
                    ]
                ]
            ]
        ]
    ]
]

更新。

在我的情况下,我将品牌存储在两个字段中:全文搜索使用默认“ Tommy Hilfiger”,精确搜索使用格式化的关键字(“ slug”)“ tommy-hilfiger”。我可以按段汇总数据,但是无法在查询中获得此字段。例如,此查询返回所有品牌为Tommy Hilfiger的记录,但仅返回默认值,而不是任何数据。

'body' => [
    '_source'   => [
        'brand',
        'brand.keyword'
    ],
    'query' => [
        'bool' => [
            'must' => [
                [
                    'terms' => [
                        'brand.keyword' => [
                            'tommy-hilfiger',
                        ]
                    ]
                ]
            ]
        ]
    ]
]

0 个答案:

没有答案