如何使用 match_phrase 和 constant_score 条款

How to use match_phrase with constant_score, terms

这是我在 symfony2 中的查询。 我想在此处添加 "match_phrase",但是我在任何地方添加它,都会出错。

$params = [
    'index' => 'articles_v2',
    'type' => 'article',
    'body' => [
        "sort"  => [
            [ "date"  =>
                ["order" => "desc"]
            ],
        ],
        "from" => $fromId,
        "size"  => $newsPerPage,
        "query" => [
            "constant_score" => [
                "filter" => [
                    "bool" => [
                        "must" => [
                            ["terms" => [ "article.topics" => $topics ] ],
                            ["match_phrase" => ["article.bodytext" => [$search_phrase] ]]
                        ]
                    ]
                ]
            ]

        ]
    ]
];
$response = $client->search($params);

当我尝试 运行 时,出现错误: 嵌套:QueryParsingException[[articles_v2] 没有为 [match_phrase]] 注册过滤器; }]","status":400

那么把这个 match_phrase 放在哪里? (我想得到类似 SQL LIKE '%xxxx%' 的结果)


我已经更改了查询。这次没有错误,但无论如何,没有过滤。

$params = [
    'index' => 'articles_v2',
    'type' => 'article',
    'body' => [
        "sort"  => [
            [ "date"  =>
                ["order" => "desc"]
            ],
        ],
        "from" => $fromId,
        "size"  => $newsPerPage,
        "query" => [
            "constant_score" => [
                "filter" => [
                    "bool" => [
                        "must" => [
                            ["terms" => [ "article.topics" => $topics ] ]
                        ]
                    ]
                ],
                "query" => [
                    "multi_match" => [
                        "query" =>    $search_phrase, 
                        "fields" => [ "title", "bodytext" ]
                    ]
                ]
            ]
        ]
    ]
];
$response = $client->search($params);

解决方法是不使用常量分数匹配。

您必须使用 query/bool/must 并且所有这些 匹配和内部的其他条件 must.

这是代码。

$params = [
    'index' => 'articles_v2',
    'type' => 'article',
    'size' => 50,
    'body' => [
        "sort"  => [
            [ "date"  =>
                ["order" => "desc"]
            ],
        ],
        "from" => $fromId,
        "size"  => $newsPerPage,
        "query" => [
            "bool" => [
                "must" => [
                    [
                        "match_phrase_prefix" => [
                            "_all" => [
                                "query" => $search_phrase,
                                "operator" => "and",
                                "analyzer" => "analyzer_cs"
                            ]
                        ]
                    ],
                    ["terms" => [ "article.topics" => $topics ] ],
                    ["range" => [ "article.date" => [ "from" => $date_from,"to" => $date_till]]]
                ]
            ]
        ]
    ]
];