Skip to content

Commit e50ee4d

Browse files
committed
Moved array->string conversion out to a separate class, updated tests, started building the dsl query builder and added term+wildcard+range query support
1 parent 9abaf55 commit e50ee4d

10 files changed

+510
-55
lines changed

ElasticSearchClient.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php // vim:set ts=4 sw=4 et:
2-
require_once 'lib/ElasticSearchDSL.php';
32
require_once 'lib/ElasticSearchDSLStringify.php';
3+
4+
require_once 'lib/builder/ElasticSearchDSLBuilder.php';
5+
46
require_once 'lib/transport/ElasticSearchTransport.php';
57
require_once 'lib/transport/ElasticSearchTransportHTTP.php';
68
require_once 'lib/transport/ElasticSearchTransportMemcached.php';

docs/dsl_spec

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
From: http://groups.google.com/a/elasticsearch.com/group/users/browse_thread/thread/549fb5ede5df6ff4/0890e504cc13d486
2+
3+
QUERY DSL SPEC: (better read in fixed-width font)
4+
--------------------------------------------------
5+
6+
curl -XGET 'http://$server/_search -d '{ TOPLEVEL }'
7+
curl -XGET 'http://$server/_all/_search -d '{ TOPLEVEL }'
8+
curl -XGET 'http://$server/_all/$types/_search -d '{ TOPLEVEL }'
9+
curl -XGET 'http://$server/$indices/_search -d '{ TOPLEVEL }'
10+
curl -XGET 'http://$server/$indices/$types/_search -d '{ TOPLEVEL }'
11+
12+
TOPLEVEL:
13+
---------
14+
{
15+
explain: BOOL,
16+
from: INT,
17+
size: INT,
18+
fields: ["field_1", "field_n"],
19+
query: { QUERY_CLAUSE },
20+
facets: { FACETS_CLAUSE },
21+
sort: { SORT_CLAUSE }
22+
}
23+
24+
QUERY_CLAUSE:
25+
-------------
26+
{
27+
term: { TERM_QUERY }
28+
| range: { RANGE_QUERY }
29+
| prefix: { PREFIX_QUERY }
30+
| wildcard: { WILDCARD_QUERY }
31+
| matchAll: { MATCH_ALL_QUERY }
32+
| queryString: { QUERY_STRING_QUERY }
33+
| bool: { BOOLEAN_QUERY }
34+
| disMax: { DISMAX_QUERY }
35+
| constantScore: { CONSTANT_SCORE_QUERY }
36+
| filteredQuery: { FILTERED_QUERY },
37+
}
38+
39+
FILTER_CLAUSE:
40+
--------------
41+
{
42+
query: { QUERY_CLAUSE },
43+
| term: { TERM_FILTER },
44+
| range: { RANGE_FILTER },
45+
| prefix: { PREFIX_FILTER },
46+
| wildcard: { WILDCARD_FILTER },
47+
| bool: { BOOLEAN_FILTER },
48+
| constantScore: { CONSTANT_SCORE_QUERY }
49+
}
50+
51+
FACETS_CLAUSE:
52+
--------------
53+
{
54+
$facet_name_1: { QUERY_CLAUSE },
55+
$facet_name_n: ...
56+
}
57+
58+
SORT_CLAUSE:
59+
------------
60+
[
61+
$fieldname_1 | "score
62+
| { $fieldname_1 : { reverse: BOOL }},
63+
...
64+
]
65+
66+
TERM_FILTER:
67+
------------
68+
{ $fieldname: VALUE_1 }
69+
70+
TERM_QUERY:
71+
-----------
72+
{ $fieldname: VALUE_1 }
73+
| { $fieldname: { value: VALUE, boost: FLOAT } }
74+
75+
PREFIX_FILTER:
76+
--------------
77+
{ $fieldname: STRING}
78+
79+
PREFIX_QUERY:
80+
-------------
81+
{ $fieldname: STRING}
82+
| { $fieldname: { prefix: STRING_1, boost: FLOAT } }
83+
84+
WILDCARD_FILTER:
85+
----------------
86+
{ $fieldname: STRING }
87+
88+
WILDCARD_QUERY:
89+
---------------
90+
{ $fieldname: STRING }
91+
| { $fieldname: { wildcard: STRING, boost: FLOAT } }
92+
93+
MATCH_ALL_QUERY:
94+
----------------
95+
{}
96+
| { boost: FLOAT }
97+
98+
RANGE_FILTER:
99+
-------------
100+
{ $fieldname: {
101+
from: INT | FLOAT | STRING | DATETIME,
102+
to: INT | FLOAT | STRING | DATETIME,
103+
includeLower: BOOL,
104+
includeUpper: BOOL,
105+
}}
106+
107+
RANGE_QUERY:
108+
------------
109+
{ $fieldname: {
110+
from: INT | FLOAT | STRING | DATETIME,
111+
to: INT | FLOAT | STRING | DATETIME,
112+
includeLower: BOOL
113+
includeUpper: BOOL
114+
boost: FLOAT
115+
}}
116+
117+
BOOLEAN_FILTER:
118+
---------------
119+
{
120+
must: { FILTER_CLAUSE } | [ { FILTER_CLAUSE }, ... ],
121+
should: { FILTER_CLAUSE } | [ { FILTER_CLAUSE }, ... ],
122+
mustNot: { FILTER_CLAUSE } | [ { FILTER_CLAUSE }, ... ],
123+
124+
minimumNumberShouldMatch: INT
125+
}
126+
127+
BOOLEAN_QUERY:
128+
--------------
129+
{
130+
must: { QUERY_CLAUSE} | [ { QUERY_CLAUSE }, ... ],
131+
should: { QUERY_CLAUSE} | [ { QUERY_CLAUSE }, ... ],
132+
mustNot: { QUERY_CLAUSE} | [ { QUERY_CLAUSE }, ... ],
133+
134+
boost: FLOAT,
135+
minimumNumberShouldMatch: INT
136+
}
137+
138+
DISMAX_QUERY:
139+
-------------
140+
{
141+
queries: [ { QUERY_CLAUSE }, ... ],
142+
tieBreakerMultiplier: FLOAT,
143+
boost: FLOAT
144+
}
145+
146+
CONSTANT_SCORE_QUERY:
147+
---------------------
148+
{
149+
filter: { FILTER_CLAUSE }
150+
boost: FLOAT
151+
}
152+
153+
FILTERED_QUERY:
154+
---------------
155+
{
156+
query: { QUERY_CLAUSE },
157+
filter: { FILTER_CLAUSE, ... }
158+
}
159+
160+
QUERY_STRING_QUERY:
161+
-------------------
162+
{
163+
query: STRING,
164+
defaultField: $fieldname,
165+
defaultOperator: "AND" | "OR",
166+
analyzer: STRING,
167+
allowLeadingWildcard: BOOL,
168+
lowercaseExpandedTerms: BOOL,
169+
enablePositionIncrements: BOOL,
170+
fuzzyPrefixLength: BOOL,
171+
fuzzyMinSim: FLOAT,
172+
phraseSlop: INT,
173+
boost: FLOAT
174+
}
175+
176+
ARGUMENT TYPES:
177+
---------------
178+
- BOOL: true | false
179+
- INT: integer eg 5
180+
- FLOAT: float eg 1.2
181+
- STRING: text eg "foo"
182+
- DATETIME: dates and times
183+
eg "2010-02-31T13:30:45", "2010-02-31", "13:30:45"
184+
- VALUE: BOOL | INT | FLOAT | STRING | DATETIME

lib/ElasticSearchDSL.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/ElasticSearchDSLStringify.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
/**
44
* Parse a DSL object into a string based representation
5+
* Return string representation of DSL for search.
6+
* This will remove certain fields that are not supported
7+
* in a string representation
58
*
69
* @author Raymond Julin <raymond.julin@gmail.com>
710
* @package ElasticSearchClient
@@ -12,12 +15,12 @@ class ElasticSearchDSLStringify {
1215

1316
protected $dsl = array();
1417

15-
public function __construct(ElasticSearchDSL $dsl) {
18+
public function __construct(array $dsl) {
1619
$this->dsl = $dsl;
1720
}
1821

19-
public function convert() {
20-
$dsl = $this->dsl->toArray();
22+
public function __toString() {
23+
$dsl = $this->dsl;
2124
$query = $dsl['query'];
2225

2326
$string = "";
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php // vim:set ts=4 sw=4 et:
2+
require_once 'ElasticSearchDSLBuilderQuery.php';
3+
require_once 'ElasticSearchDSLBuilderRangeQuery.php';
4+
5+
/**
6+
* Helper stuff for working with the ElasticSearch DSL
7+
* How to build a mildly complex query:
8+
* $dsl = new ElasticSearchDSL;
9+
* $bool = $dsl->bool(); // Return a new bool structure
10+
*
11+
* @author Raymond Julin <raymond.julin@gmail.com>
12+
* @package ElasticSearchClient
13+
* @since 0.1
14+
* Created: 2010-07-23
15+
*/
16+
class ElasticSearchDSLBuilder {
17+
18+
protected $dsl = array();
19+
20+
private $explain = null;
21+
private $from = null;
22+
private $size = null;
23+
private $fields = null;
24+
private $query = null;
25+
private $facets = null;
26+
private $sort = null;
27+
28+
/**
29+
* Construct DSL object
30+
*
31+
* @return ElasticSearchDSL
32+
* @param array $options
33+
*/
34+
public function __construct(array $options=array()) {
35+
}
36+
37+
/**
38+
* Add array clause, can only be one
39+
*
40+
* @return ElasticSearchDSLBuilderQuery
41+
* @param array $options
42+
*/
43+
public function query(array $options=array()) {
44+
$this->query = new ElasticSearchDSLBuilderQuery($options);
45+
return $this->query;
46+
}
47+
48+
/**
49+
* Build the DSL as array
50+
*
51+
* @return array
52+
*/
53+
public function build() {
54+
$built = array();
55+
if (!$this->query)
56+
throw new Exception("Query must be specified");
57+
else
58+
$built['query'] = $this->query->build();
59+
return $built;
60+
}
61+
}

0 commit comments

Comments
 (0)