symfonyelasticsearchmatch-phrase

How to use match_phrase with constant_score, terms


This is my query in symfony2. I want to add here "match_phrase", but anywhere i add that, I get errors.

$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);

When I try to run this, I get error: nested: QueryParsingException[[articles_v2] No filter registered for [match_phrase]]; }]","status":400

So where to place this match_phrase? (I want to get results like SQL LIKE '%xxxx%')


I have changed the query. this time no errors, but anyway, no filtration.

$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);

Solution

  • The solution is not to use match with constant score.

    You have to use query/bool/must and all this match and other conditions inside must.

    Here is the code.

    $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]]]
                    ]
                ]
            ]
        ]
    ];