Skip to content

Commit 67a71a4

Browse files
committed
Translate sort and fields attribute to string when using dsl for a transport that doesnt support it
1 parent 9c4692d commit 67a71a4

File tree

2 files changed

+115
-15
lines changed

2 files changed

+115
-15
lines changed

lib/ElasticSearchDSL.php

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ public function __construct(array $dsl) {
3030
* @return string
3131
*/
3232
public function __toString() {
33-
$dsl = $this->dsl['query'];
33+
$dsl = $this->dsl;
34+
$query = $dsl['query'];
35+
3436
$string = "";
35-
if (array_key_exists("term", $dsl))
36-
$string .= $this->transformDSLTermToString($dsl['term']);
37-
if (array_key_exists("wildcard", $dsl))
38-
$string .= $this->transformDSLTermToString($dsl['wildcard']);
37+
if (array_key_exists("term", $query))
38+
$string .= $this->transformDSLTermToString($query['term']);
39+
if (array_key_exists("wildcard", $query))
40+
$string .= $this->transformDSLTermToString($query['wildcard']);
41+
if (array_key_exists("sort", $dsl))
42+
$string .= $this->transformDSLSortToString($dsl['sort']);
43+
if (array_key_exists("fields", $dsl))
44+
$string .= $this->transformDSLFieldsToString($dsl['fields']);
3945
return $string;
4046
}
4147

@@ -51,20 +57,60 @@ protected function transformDSLTermToString($dslTerm) {
5157
if (is_array($dslTerm)) {
5258
$key = key($dslTerm);
5359
$value = $dslTerm[$key];
54-
55-
/**
56-
* If a specific key is used as key in the array
57-
* this should translate to searching in a specific field (field:term)
58-
*/
5960
if (is_string($key))
6061
$string .= "$key:";
61-
if (strpos(" ", $value) !== false)
62-
$string .= '"' . $value . '"';
63-
else
64-
$string .= $value;
6562
}
6663
else
67-
$string .= $dslTerm;
64+
$value = $dslTerm;
65+
/**
66+
* If a specific key is used as key in the array
67+
* this should translate to searching in a specific field (field:term)
68+
*/
69+
if (strpos($value, " ") !== false)
70+
$string .= '"' . $value . '"';
71+
else
72+
$string .= $value;
73+
return $string;
74+
}
75+
76+
/**
77+
* Transform search parameters to string
78+
*
79+
* @return string
80+
* @param mixed $dslSort
81+
*/
82+
protected function transformDSLSortToString($dslSort) {
83+
$string = "";
84+
if (is_array($dslSort)) {
85+
foreach ($dslSort as $sort) {
86+
if (is_array($sort)) {
87+
$field = key($sort);
88+
$info = current($sort);
89+
}
90+
else
91+
$field = $sort;
92+
$string .= "&sort=" . $field;
93+
if (isset($info)) {
94+
if (is_string($info) && $info == "desc")
95+
$string .= ":reverse";
96+
elseif (is_array($info) && array_key_exists("reverse", $info) && $info['reverse'])
97+
$string .= ":reverse";
98+
}
99+
}
100+
}
101+
return $string;
102+
}
103+
104+
/**
105+
* Transform a selection of fields to return to string form
106+
*
107+
* @return string
108+
* @param mixed $dslFields
109+
*/
110+
protected function transformDSLFieldsToString($dslFields) {
111+
$string = "";
112+
if (is_array($dslFields))
113+
$string .= "&fields=" . join(",", $dslFields);
68114
return $string;
69115
}
70116
}

tests/DSLTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,58 @@ public function testTerm() {
2323
$strDsl = (string)$dsl;
2424
$this->assertEquals("cool", $strDsl);
2525
}
26+
27+
public function testGroupedTerms() {
28+
$arr = array(
29+
'query' => array(
30+
'term' => 'cool stuff'
31+
)
32+
);
33+
$dsl = new ElasticSearchDSL($arr);
34+
$strDsl = (string)$dsl;
35+
$this->assertEquals('"cool stuff"', $strDsl);
36+
}
37+
38+
public function testNamedGroupedTerms() {
39+
$arr = array(
40+
'query' => array(
41+
'term' => array('title' => 'cool stuff')
42+
)
43+
);
44+
$dsl = new ElasticSearchDSL($arr);
45+
$strDsl = (string)$dsl;
46+
$this->assertEquals('title:"cool stuff"', $strDsl);
47+
}
48+
49+
public function testSort() {
50+
$arr = array(
51+
'sort' => array(
52+
array('title' => 'desc')
53+
),
54+
'query' => array(
55+
'term' => array('title' => 'cool stuff')
56+
)
57+
);
58+
$dsl = new ElasticSearchDSL($arr);
59+
$this->assertEquals('title:"cool stuff"&sort=title:reverse', (string)$dsl);
60+
61+
$arr['sort'] = array('title');
62+
$dsl = new ElasticSearchDSL($arr);
63+
$this->assertEquals('title:"cool stuff"&sort=title', (string)$dsl);
64+
65+
$arr['sort'] = array(array('title' => array('reverse' => true)));
66+
$dsl = new ElasticSearchDSL($arr);
67+
$this->assertEquals('title:"cool stuff"&sort=title:reverse', (string)$dsl);
68+
}
69+
70+
public function testLimitReturnFields() {
71+
$arr = array(
72+
'fields' => array('title','body'),
73+
'query' => array(
74+
'term' => array('title' => 'cool')
75+
)
76+
);
77+
$dsl = new ElasticSearchDSL($arr);
78+
$this->assertEquals('title:cool&fields=title,body', (string)$dsl);
79+
}
2680
}

0 commit comments

Comments
 (0)