Skip to content

Commit 330838c

Browse files
committed
[FEATURE] Add Eel operation for count query
Used exactly like execute() the new count() operation works on the Elasticsearch count API and returns the total number of hits for a query.
1 parent 4043b1a commit 330838c

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryBuilder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,25 @@ public function execute() {
380380
}
381381

382382
return array_values($nodes);
383+
}
384+
385+
/**
386+
* Return the total number of hits for the query.
387+
*
388+
* @return integer
389+
*/
390+
public function count() {
391+
$timeBefore = microtime(TRUE);
392+
$response = $this->elasticSearchClient->getIndex()->request('GET', '/_count', array(), json_encode($this->request));
393+
$timeAfterwards = microtime(TRUE);
394+
395+
$count = $response->getTreatedContent()['count'];
396+
397+
if ($this->logThisQuery === TRUE) {
398+
$this->logger->log('Query Log (' . $this->logMessage . '): ' . json_encode($this->request) . ' -- execution time: ' . (($timeAfterwards-$timeBefore)*1000) . ' ms -- Total Results: ' . $count, LOG_DEBUG);
399+
}
383400

401+
return $count;
384402
}
403+
385404
}

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ alternative to FlowQuery. In the long run, we might be able to integrate this AP
3636
but for now it works well as-is.
3737

3838
Generally, ElasticSearch queries are done using the `ElasticSearch` Eel helper. In case you want
39-
to retieve a *list of nodes*, you'll generally do:
39+
to retrieve a *list of nodes*, you'll generally do:
4040
```
4141
nodes = ${ElasticSearch.query(site)....execute()}
4242
```
@@ -46,10 +46,15 @@ In case you just want to retrieve a *single node*, the form of a query is as fol
4646
nodes = ${q(ElasticSearch.query(site)....execute()).get(0)}
4747
```
4848

49+
To fetch the total number of hits a query returns, the form of a query is as follows:
50+
```
51+
nodes = ${ElasticSearch.query(site)....count()}
52+
```
53+
4954
All queries search underneath a certain subnode. In case you want to search "globally", you will
5055
search underneath the current site node (like in the example above).
5156

52-
Furthermore, the following operators are supported:
57+
Furthermore, the following operators are supported:
5358

5459
* `nodeType("Your.Node:Type")`
5560
* `exactMatch(key, value)`; supports simple types: `exactMatch('tag', 'foo')`, or node references: `exactMatch('author', authorNode)`
@@ -116,7 +121,7 @@ ElasticSearch `_all` field, and are configured with different `boost` values.
116121
In order to search this index, you can just search inside the `_all` field with an additional limitation
117122
of `__typeAndSupertypes` containing `TYPO3.Neos:Document`.
118123

119-
**Currently, this package does not contain a plugin for searching, though we might provide one lateron.**
124+
**Currently, this package does not contain a plugin for searching, though we might provide one later on.**
120125

121126

122127
## Advanced: Configuration of Indexing
@@ -125,13 +130,13 @@ of `__typeAndSupertypes` containing `TYPO3.Neos:Document`.
125130

126131
Indexing of properties is configured at two places. The defaults per-data-type are configured
127132
inside `Flowpack.ElasticSearch.ContentRepositoryAdaptor.defaultConfigurationPerType` of `Settings.yaml`.
128-
Furthermore, this can be overridden using the `properties.[....].elasticSearch` path inside
133+
Furthermore, this can be overridden using the `properties.[....].elasticSearch` path inside
129134
`NodeTypes.yaml`.
130135

131136
This configuration contains two parts:
132137

133138
* Underneath `mapping`, the ElasticSearch property mapping can be defined.
134-
* Underneath `indexing`, an Eel expression which preprocesses the value before indexing has to be
139+
* Underneath `indexing`, an Eel expression which processes the value before indexing has to be
135140
specified. It has access to the current `value` and the current `node`.
136141

137142
Example (from the default configuration):
@@ -141,7 +146,7 @@ Flowpack:
141146
ElasticSearch:
142147
ContentRepositoryAdaptor:
143148
defaultConfigurationPerType:
144-
149+
145150
# strings should, by default, not be included in the _all field; and
146151
# indexing should just use their simple value.
147152
string:
@@ -167,7 +172,7 @@ Flowpack:
167172
indexing: '${(node.hiddenBeforeDateTime ? node.hiddenBeforeDateTime.format("Y-m-d\TH:i:s") + "Z" : null)}'
168173
```
169174

170-
There are a few indexing helpers inside the `ElasticSearch` namespace which are usable inside the
175+
There are a few indexing helpers inside the `ElasticSearch` namespace which are usable inside the
171176
`indexing` expression. In most cases, you don't need to touch this, but they were needed to build up
172177
the standard indexing configuration:
173178

0 commit comments

Comments
 (0)