opensearch-ts 1.2.2 → 1.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +60 -51
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -1,32 +1,33 @@
1
-
2
-
3
1
  # Opensearch-Typescript
4
2
 
5
- Opensearch Typescript
6
-
7
- [![Node.js Package](https://github.com/derrickfutschik/opensearch-typescript/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/derrickfutschik/opensearch-typescript/actions/workflows/npm-publish.yml)
3
+ [![Publish to npm](https://github.com/derrickfutschik/opensearch-ts/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/derrickfutschik/opensearch-ts/actions/workflows/npm-publish.yml)
8
4
 
9
5
  This library can be used to proxy the opensearch client which then provides a more type-rich client. For the most part this is a seemless change, the same requests which worked with `@opensearch-project/opensearch` will work with `opensearch-typescript`, with the exception of **Multi-search** (see workaround below). I wrote this library after experiencing too much difficulty when trying to write opensearch querries in code, I would always have ot write them in the **Dev Tools** console, and then turn them into code, which wasn't too dificult. But what provided me enough motiviation to create this library was then parsing the respones in code was overly complex without a type-system for the respones.
10
6
 
11
7
  ## Usage
8
+
12
9
  To use this library, create the `TypescriptOSProxyClient` client by wrapping the `opensearch Client`:
10
+
13
11
  ```typescript
14
- new TypescriptOSProxyClient(client)
12
+ new TypescriptOSProxyClient(client);
15
13
  ```
16
14
 
17
15
  To have strongly typed query and responses, the requirement is to define a `Search<T,A extends AggsQuery>` whereby `T` represents the type of your document in the index, and `A` is a description of the aggregation query you are planning to perform, which contains the nested aggregation structure, as well as the type of aggregation looking to be performed. With `T` & `A` this provides typescript with enough information to calculate the type of the response from opensearch. For example lets look at the sample ecommerce data and do a query to find the average baes price in a category. I used [quicktype](https://quicktype.io/) to generate the type `Ecommerce` which can be found [here](docs/files/Ecommerce.ts).
18
16
 
19
17
  ```typescript
20
- type AvgBasePriceByCategoryQuery = Search<Ecommerce, {
21
- category : {
22
- agg : "terms",
23
- aggs : {
24
- "avg_base_price_in_category" : {
25
- agg : "avg",
26
- }
27
- }
28
- }
29
- }>
18
+ type AvgBasePriceByCategoryQuery = Search<
19
+ Ecommerce,
20
+ {
21
+ category: {
22
+ agg: "terms";
23
+ aggs: {
24
+ avg_base_price_in_category: {
25
+ agg: "avg";
26
+ };
27
+ };
28
+ };
29
+ }
30
+ >;
30
31
  ```
31
32
 
32
33
  Now when we write this query we get autocomplete suggestions, and highlighting for incorrect type errors:
@@ -35,43 +36,48 @@ Now when we write this query we get autocomplete suggestions, and highlighting f
35
36
  Then to do the search:
36
37
 
37
38
  ```typescript
38
- const search : AvgBasePriceByCategoryQuery =
39
- {
40
- "size": 0,
41
- "aggs": {
42
- "category": {
43
- "terms": {
44
- "field": "category.keyword"
39
+ const search: AvgBasePriceByCategoryQuery = {
40
+ size: 0,
41
+ aggs: {
42
+ category: {
43
+ terms: {
44
+ field: "category.keyword",
45
+ },
46
+ aggs: {
47
+ avg_base_price_in_category: {
48
+ avg: {
49
+ field: "products.base_price",
50
+ },
45
51
  },
46
- "aggs" : {
47
- "avg_base_price_in_category" : {
48
- "avg" : {
49
- "field" : "products.base_price"
50
- }
51
- }
52
- }
53
- }
54
- }
55
- }
52
+ },
53
+ },
54
+ },
55
+ };
56
56
 
57
57
  const result = await tsClient.searchTS({
58
- body : search,
59
- index : "opensearch_dashboards_sample_data_ecommerce"
60
- })
58
+ body: search,
59
+ index: "opensearch_dashboards_sample_data_ecommerce",
60
+ });
61
61
  ```
62
+
62
63
  Whilst you will have to specify the `type` of the `search` object, as it's not possible to infer types for this case, however the `result` object you'll notice is strongly typed, having a type of:
63
- ``` typescript
64
- const result: SearchResponse<Ecommerce, {
64
+
65
+ ```typescript
66
+ const result: SearchResponse<
67
+ Ecommerce,
68
+ {
65
69
  category: {
66
- agg: "terms";
67
- aggs: {
68
- "avg_base_price_in_category": {
69
- agg: "avg";
70
- };
70
+ agg: "terms";
71
+ aggs: {
72
+ avg_base_price_in_category: {
73
+ agg: "avg";
71
74
  };
75
+ };
72
76
  };
73
- }>
77
+ }
78
+ >;
74
79
  ```
80
+
75
81
  This provides proper auto-complete suggestions and compile checks:
76
82
 
77
83
  **Autocomplete**
@@ -82,23 +88,26 @@ This provides proper auto-complete suggestions and compile checks:
82
88
 
83
89
  Drastically reducing programmer errors, and making it just as easy if not easier to write queries within a typescript project.
84
90
 
85
-
86
91
  ## Incomplete Client Coverage
92
+
87
93
  Please note this client is not 100% complete, please reach out if parts of the opensearch client are missing and you want to make use of this library, I will do my best to keep this as up-to-date as possible. I also haven't tested all `agg` queries, with all permutations of `number|string` and `dates`.
88
94
 
89
95
  ## Incomplete Types
96
+
90
97
  I have support thus for for:
91
- - `string`
92
- - `number`
93
- - `Date`
94
- - `boolean`
95
- - `GeoPoint`
98
+
99
+ - `string`
100
+ - `number`
101
+ - `Date`
102
+ - `boolean`
103
+ - `GeoPoint`
96
104
 
97
105
  I plan to have strong types for other fields in the future, for not strings can be used.
98
106
 
99
107
  ## Multi-search Limitation
100
- I was not able to map individual values in a dictionary to different types. This meant I couldn't map a collection of requests to a strongly typed collection of responses. The work-around for this was to store the responses of each individual request back onto itself. Because the type information of the request remained in-tact after execution, this was the only way to all the type information in type-script. Obviously this has some paradigm drawbacks, mutating requests as opposed to immutability and functional paradigms It also requires different code if you were to refactor existing opensearh code to make use of this library.
101
108
 
109
+ I was not able to map individual values in a dictionary to different types. This meant I couldn't map a collection of requests to a strongly typed collection of responses. The work-around for this was to store the responses of each individual request back onto itself. Because the type information of the request remained in-tact after execution, this was the only way to all the type information in type-script. Obviously this has some paradigm drawbacks, mutating requests as opposed to immutability and functional paradigms It also requires different code if you were to refactor existing opensearh code to make use of this library.
102
110
 
103
111
  ## Elasticsearch
112
+
104
113
  This same approach could be taken with Elasticsearch but unless adoption of this client takes off I don't plan to replicate this in Elasticsearch.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opensearch-ts",
3
- "version": "1.2.2",
3
+ "version": "1.2.7",
4
4
  "description": "Typescript types for the Opensearch Query DSL",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,7 +16,7 @@
16
16
  "node": ">=14.15.0"
17
17
  },
18
18
  "dependencies": {
19
- "@opensearch-project/opensearch": "^3.2.0",
19
+ "@opensearch-project/opensearch": "^3.5.1",
20
20
  "lodash": "^4.17.21",
21
21
  "type-fest": "^4.15.0"
22
22
  },
@@ -31,11 +31,11 @@
31
31
  "tsconfig-paths": "^3.9.0",
32
32
  "typescript": "^4.1.3"
33
33
  },
34
- "author": "derrops@derrops.net",
34
+ "author": "derrops@derrops.com",
35
35
  "license": "(ISC OR GPL-3.0)",
36
36
  "repository": {
37
37
  "type": "git",
38
- "url": "https://github.com/derrops-net/opensearch-ts.git"
38
+ "url": "https://github.com/derrickfutschik/opensearch-ts.git"
39
39
  },
40
40
  "keywords": [
41
41
  "opensearch",