Elasticsearch部分匹配或模糊匹配,提升部分结果

时间:2017-02-07 21:37:27

标签: php elasticsearch elasticsearch-2.0

尝试在Elasticsearch中使用PHP客户端进行查询,并优先考虑部分单词匹配但仍包含模糊匹配。如果我删除address.company匹配块,查询将按预期工作,但无论我如何构建它,它都会被打破。我在格式化上迷失了还包括优先级较低的模糊搜索?

    $search_data = [
        "from"  => (int) $start, "size" => (int) $count,
        'query' => [
            'bool' => [
                'filter' => [
                    ['term' => ['active' => 1]],
                    ['term' => ['type' => 2]],
                ],
                'must'   => [
                    'wildcard' => [
                        'address.company' => '*' . $search_query . '*'
                    ],
                    'match'    => [
                        'address.company' => [
                            'query'     => $search_query,
                            'operator'  => 'and',
                            'fuzziness' => 'AUTO',
                        ],
                    ],
                ],
            ],
        ],
    ];

1 个答案:

答案 0 :(得分:0)

虽然我可能很明显是ES的新手,但这个解决方案似乎得到了我追求的数据。我只提到这一点,因为如果有人在将来看到这个,可能会有更理想的方式。从必须切换到应该并且有点不同地包装数组就行了。

$search_data = [
    "from"  => (int) $start, "size" => (int) $count,
    'query' => [
        'bool' => [
            'filter' => [
                ['term' => ['active' => 1]],
                ['term' => ['type' => 2]],
            ],
            'should'   => [
                [
                    'match' => ['address.company' => ['query'=>$search_query,'boost'=>10]],
                ],
                [
                    'match' =>
                     [
                         'address.company' =>
                             [
                                'query'     => $search_query,
                                'fuzziness' => 'AUTO',
                            ],
                    ],
                ],
            ],
            'minimum_should_match'=>1,
        ],
    ],
];