Constant score query

More info about Constant score query is in the official elasticsearch docs

Inside constant score query you can insert filter or query.

Lets take an example to write a constant score query with filter inside.

{
    "constant_score" : {
        "filter" : {
            "term" : { "user" : "kimchy"}
        },
        "boost" : 1.2
    }
}

And now the query via DSL:

$termFilter = new TermQuery("user", "kimchy");
$constantScoreQuery = new ConstantScoreQuery($termFilter, ["boost" => 1.2]);

$search = new Search();
$search->addQuery($constantScoreQuery);

$queryArray = $search->toArray();

To form a query with query inside is very easy, just add a query in ConstantScoreQuery constructor instead of filter.

{
    "constant_score" : {
        "query" : {
            "term" : { "user" : "kimchy"}
        },
        "boost" : 1.2
    }
}

via DSL:

$termQuery = new TermQuery("user", "kimchy");
$constantScoreQuery = new ConstantScoreQuery($termQuery, ["boost" => 1.2]);

$search = new Search();
$search->addQuery($constantScoreQuery);

$queryArray = $search->toArray();