elasticsearch:具有提升的多个字段

时间:2016-05-18 10:15:09

标签: php elasticsearch

我正在尝试为elasticsearch创建一个查询。条件:始终有客户过滤器。有一个字段" name"如果匹配则应创建最高分,下一个最佳字段应为"类别"然后"描述"。但即使搜索没有找到" name"中的任何内容,也应返回结果。或"类别",但仅限于"说明"。

我尝试了什么:

        $query = [
        'filtered' => [
            'filter' => [
                'term' => [ 'customer' => $selectedCustomer ]
            ],
            'query' => [
                'bool' => [
                    'should' => [
                        'match' => [
                            'name' => [
                                'query' => $val,
                                'boost' => 7
                            ]
                        ]
                    ],
                    'should' => [
                        'match' => [
                            'category' => [
                                'query' => $val,
                                'boost' => 3
                            ]
                        ],
                    ],
                    'should' => [
                        'match' => [
                            'description' => [
                                'query' => $val,
                                'boost' => 1
                            ]
                        ],
                    ],
                ]
            ]
        ]
    ];

但这似乎只返回了"描述"中匹配的结果。我究竟做错了什么?任何提示?

1 个答案:

答案 0 :(得分:0)

似乎我通过这样做找到了第一个问题的解决方案:

        $query = [
        'filtered' => [
            'filter' => [
                'term' => [ 'customer' => $selectedCustomer ]
            ],
            'query' => [
                'bool' => [
                    'should' => [
                        ['match' => [
                            'name' => [
                                'query' => $val,
                                'boost' => 7
                            ]
                        ]],
                        ['match' => [
                            'category' => [
                                'query' => $val,
                                'boost' => 5
                            ]
                        ]],
                        ['match' => [
                            'description' => [
                                'query' => $val,
                                'boost' => 1
                            ]
                        ]],

                    ],
                ]
            ]
        ]
    ];

但下一个问题是,它只找到整个单词的匹配项。比如,如果$ val ='面包'我得到所有包含单词" bread"的比赛,但我没有得到像#34; gingerbread"这样的东西。有人知道任何解决方案吗?