如何将match_phrase与constant_score,

时间:2017-06-09 09:40:42

标签: symfony elasticsearch match-phrase

这是我在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]注册过滤器]; }]""状态":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);

1 个答案:

答案 0 :(得分:0)

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

您必须使用 query / bool / must ,并且所有匹配和其他条件必须

这是代码。

  @foreach ($order->cart->items as $item)
         {{ dd(json_decode($order->cart, true)) }}
        {{ $item['price']['title'] }}
   @endforeach