Newer
Older
Simonas Šerlinskas
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Constant score query
> More info about Constant score query is in the [official elasticsearch docs][1]
Inside constant score query you can insert filter or query.
Lets take an example to write a constant score query with filter inside.
```JSON
{
"constant_score" : {
"filter" : {
"term" : { "user" : "kimchy"}
},
"boost" : 1.2
}
}
```
And now the query via DSL:
```php
$termFilter = new TermFilter("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.
```JSON
{
"constant_score" : {
"query" : {
"term" : { "user" : "kimchy"}
},
"boost" : 1.2
}
}
```
via DSL:
```php
$termQuery = new TermQuery("user", "kimchy");
$constantScoreQuery = new ConstantScoreQuery($termQuery, ["boost" => 1.2]);
$search = new Search();
$search->addQuery($constantScoreQuery);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html