代码之家  ›  专栏  ›  技术社区  ›  user3013494

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

  •  0
  • user3013494  · 技术社区  · 5 年前

    对于聚合,我有一个字段的原始值。但我无法在查询中访问此值。例如,在我的情况下,我有一个 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',
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
    

    在我的例子中,我将brand存储在两个字段中:默认的“Tommy Hilfiger”用于全文搜索,格式化的关键字(slug)“Tommy Hilfiger”用于精确搜索。我可以按slug聚合数据,但无法在查询中获取此字段。例如,这个查询返回品牌为Tommy Hilfiger的所有记录,但只返回默认值,而不是slug。

    'body' => [
        '_source'   => [
            'brand',
            'brand.keyword'
        ],
        'query' => [
            'bool' => [
                'must' => [
                    [
                        'terms' => [
                            'brand.keyword' => [
                                'tommy-hilfiger',
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
    
    0 回复  |  直到 5 年前