0

这是我在 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);

当我尝试运行它时,出现错误: nested: QueryParsingException[[articles_v2] No filter registered for [match_phrase]]; }]","状态":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);
4

1 回答 1

0

解决方案是不要使用具有恒定分数的匹配。

您必须使用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]]]
                ]
            ]
        ]
    ]
];
于 2017-06-13T13:26:29.820 回答