Skip to content

Commit

Permalink
Merge pull request #134 from social-native/feat/get-raw-query
Browse files Browse the repository at this point in the history
Get raw es query
  • Loading branch information
markrsocialnative authored May 17, 2021
2 parents d217700 + cd945d6 commit 5ea0ff5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default observer(() => {
- [Set middleware](#set-middleware)
- [Get the initial results for a manager](#get-the-initial-results-for-a-manager)
- [Run a custom elasticsearch query using the current filters](#run-a-custom-elasticsearch-query-using-the-current-filters)
- [Get the raw elasticsearch query derived from the current filters](#get-the-raw-elasticsearch-query-derived-from-the-current-filters)
- [Setting a range filter](#setting-a-range-filter)
- [Setting a boolean filter](#setting-a-boolean-filter)
- [Setting a exists filter](#setting-a-exists-filter)
Expand Down Expand Up @@ -445,6 +446,12 @@ If you wanted to bulk export a subset of the filtered results without having to
const results = await manager.runCustomFilterQuery({whiteList: ['id'], pageSize: 10000});
```
### Get the raw elasticsearch query derived from the current filters
```typescript
const rawEsQuery = await manager.getCurrentEsQuery();
```
### Setting a range filter
```typescript
Expand Down Expand Up @@ -794,6 +801,7 @@ const options = {suggestions: {fuzzy: fuzzyFilterInstance}};
| runStartQuery | runs the initial elasticsearch query that fetches unfiltered data | `(): void` |
| runCustomFilterQuery | runs a custom query using the existing applied filters outside the side effect queue flow. white lists and black lists control which data is returned in the elasticsearch response source object | `async (options?: {fieldBlackList?: string[], fieldWhiteList?: string[], pageSize?: number }): Promise<ESResponse>` |
| setMiddleware | adds middleware to run during construction of the elasticsearch query request object | `(middlewares: Middleware): void`. Middleware has the type `(effectRequest: EffectRequest<EffectKinds>, request: ESRequest) => ESRequest` |
| getCurrentEsQuery | gets the raw Elasicsearch query that is derived from the current state | `() => ESRequest` |
#### Attributes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elastic-composer",
"version": "4.6.2",
"version": "4.7.0",
"description": "",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down
35 changes: 18 additions & 17 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1064,26 +1064,33 @@ class Manager<
);
};

public _createCustomFilteredQueryRequest = (
startingRequest: ESRequest,
options: Pick<ManagerOptions, 'fieldBlackList' | 'fieldWhiteList' | 'pageSize'>
): ESRequest => {
/**
* Get the raw ES query based on the current state.
*/
public getCurrentEsQuery = ({
startingRequest = BLANK_ES_REQUEST,
options = {}
}: {
startingRequest?: ESRequest;
options?: Pick<ManagerOptions, 'fieldBlackList' | 'fieldWhiteList' | 'pageSize'>;
} = {}): ESRequest => {
const fullRequest = objKeys(this.filters).reduce((request, filterName) => {
const filter = this.filters[filterName];
if (!filter) {
return request;
}

return filter._addFilteredQueryToRequest(request);
}, startingRequest);

// We want:
// - a page of results
// - the results to be sorted
// no middleware used b/c this is a custom request that is used outside the side effect queue
return this._applyBlackAndWhiteListsToQuery(
this._addSortToQuery(this._addPageSizeToQuery(fullRequest, options.pageSize)),
options
return removeEmptyArrays(
this._applyBlackAndWhiteListsToQuery(
this._addSortToQuery(this._addPageSizeToQuery(fullRequest, options.pageSize)),
options
)
);
};
public _createFilteredQueryRequest = (
Expand Down Expand Up @@ -1116,14 +1123,8 @@ class Manager<
public runCustomFilterQuery = async (
options: Pick<ManagerOptions, 'fieldBlackList' | 'fieldWhiteList' | 'pageSize'>
): Promise<ESResponse> => {
try {
const request = this._createCustomFilteredQueryRequest(BLANK_ES_REQUEST, options);
const response = await this.client.search(removeEmptyArrays(request));

return response;
} catch (e) {
throw e;
}
const request = this.getCurrentEsQuery({options});
return this.client.search(request);
};

public _runAllEnabledSuggestionSearch = async (effectRequest: EffectRequest<EffectKinds>) => {
Expand All @@ -1144,7 +1145,7 @@ class Manager<
}
} catch (e) {
throw e;
} // No cursor change b/c only dealing with filters
}
};

public _runSuggestionSearch = async (
Expand Down

0 comments on commit 5ea0ff5

Please sign in to comment.