|
3 | 3 | * Support for type checking and intellisense in vscode: |
4 | 4 | * @typedef {import("./solr").SolrConfig} SolrConfig |
5 | 5 | * @typedef {import("./solr").ConfigRequest} ConfigRequest |
6 | | - * @typedef {import("./solr").SolrData} SolrData |
| 6 | + * @typedef {import("./solr").SolrException} SolrException |
7 | 7 | * @typedef {import("./solr").SolrDocument} SolrDocument |
8 | 8 | * @typedef {import("./solr").SolrResponse} SolrResponse |
9 | 9 | * @typedef {import("./solr").DeleteRequest} DeleteRequest |
10 | 10 | * @typedef {import("./solr").FieldProperties} FieldProperties |
11 | 11 | * @typedef {import("./solr").FieldTypeProperties} FieldTypeProperties |
12 | 12 | * @typedef {import("./solr").QueryRequest} QueryRequest |
| 13 | + * @typedef {{name:string, type?:string, [key:string]:any}} FieldDef |
| 14 | + * @typedef {import("axios").AxiosResponse} AxiosResponse |
13 | 15 | */ |
14 | 16 |
|
15 | 17 | const url = require("url") |
16 | 18 | const { default: axios } = require("axios") |
17 | | -const { mergeConfig, ensureArray, isEmptyObject } = require("./utils") |
| 19 | +const { mergeConfig, ensureArray } = require("./utils") |
18 | 20 |
|
19 | 21 | /** @type {SolrConfig} */ |
20 | 22 | const defaultConfig = { |
21 | | - urlConfig: { |
22 | | - hostname: "localhost", |
23 | | - port: 8983, |
24 | | - protocol: "http", |
25 | | - query: { |
26 | | - commitWithin: 500, |
27 | | - overwrite: true, |
28 | | - wt: "json" |
29 | | - } |
30 | | - }, |
31 | | - debug: false, |
32 | | - apiPrefix: "api/cores" |
| 23 | + hostname: "localhost", |
| 24 | + port: 8983, |
| 25 | + protocol: "http", |
| 26 | + query: { |
| 27 | + commitWithin: 500, |
| 28 | + overwrite: true |
| 29 | + } |
33 | 30 | } |
34 | 31 |
|
35 | | -/** @type {(config:SolrConfig) => (path:string) => (data:SolrData) => Promise<SolrResponse>} */ |
36 | | -const solrPost = config => path => async data => { |
37 | | - const { core, apiPrefix } = config |
38 | | - const solrUrl = url.format({ |
39 | | - ...config.urlConfig, |
40 | | - pathname: `${apiPrefix}/${core}/${path}` |
41 | | - }) |
42 | | - |
43 | | - if (config.debug) { |
44 | | - const dataPart = isEmptyObject(data) ? "" : `-d '${JSON.stringify(data)}'` |
45 | | - console.debug( |
46 | | - `\ncurl -X POST '${solrUrl}' -H 'Content-Type: application/json' ${dataPart}\n` |
47 | | - ) |
| 32 | +/** @type {(axiosResp:AxiosResponse) => SolrResponse} */ |
| 33 | +const convertSolrResponse = axiosResp => axiosResp.data |
| 34 | + |
| 35 | +// converting error response from solr |
| 36 | +/** @param {SolrException} reason */ |
| 37 | +const convertSolrError = reason => { |
| 38 | + switch (reason.response.status) { |
| 39 | + case 400: |
| 40 | + throw Error(reason.response.data.error.msg) |
| 41 | + case 404: |
| 42 | + throw Error(`${reason.message}: ${reason.response.statusText}`) |
| 43 | + default: |
| 44 | + throw reason |
48 | 45 | } |
49 | | - |
50 | | - const response = await axios |
51 | | - .post(solrUrl, data, { |
52 | | - headers: { "Content-Type": "application/json" } |
53 | | - }) |
54 | | - .catch(reason => { |
55 | | - // converting error response from solr |
56 | | - switch (reason.response.status) { |
57 | | - case 400: |
58 | | - throw Error(reason.response.data.error.msg) |
59 | | - case 404: |
60 | | - throw Error(`${reason.message}: ${reason.response.statusText}`) |
61 | | - default: |
62 | | - throw reason |
63 | | - } |
64 | | - }) |
65 | | - |
66 | | - return response.data |
67 | 46 | } |
68 | 47 |
|
69 | | -/** @param {SolrConfig} userConfig */ |
70 | | -const prepareSolrClient = (userConfig = {}) => { |
| 48 | +/** |
| 49 | + * @param {SolrConfig} userConfig This is merged with the defaultConfig |
| 50 | + * @param {string} core |
| 51 | + */ |
| 52 | +const prepareSolrClient = (core, userConfig = {}) => { |
| 53 | + console.assert(core, "Missing 'core' parameter") |
| 54 | + |
71 | 55 | const config = mergeConfig(defaultConfig, userConfig) |
72 | 56 |
|
73 | | - // sanity checks |
74 | | - if (!config.core) { |
75 | | - throw Error("missing 'core' parameter in your config") |
76 | | - } |
| 57 | + /** @type {(pathname:string) => (data:any) => Promise<SolrResponse>} */ |
| 58 | + const httpPostReq = pathname => data => |
| 59 | + axios |
| 60 | + .post(url.format({ ...config, pathname }), data) |
| 61 | + .then(convertSolrResponse) |
| 62 | + .catch(convertSolrError) |
77 | 63 |
|
78 | | - // some functions require /solr prefix instead of /api/cores/ |
79 | | - const configWithSolrPrefix = { ...config, apiPrefix: "solr" } |
| 64 | + /** @type {(pathname:string) => (query:any) => Promise<SolrResponse>} */ |
| 65 | + const httpGetReq = pathname => query => |
| 66 | + axios |
| 67 | + .get(url.format({ ...mergeConfig(config, { query }), pathname })) |
| 68 | + .then(convertSolrResponse) |
| 69 | + .catch(convertSolrError) |
80 | 70 |
|
81 | | - // we already use the variable config, therefore solrConfigRequest |
82 | | - // represents the "config" API call from Solr |
83 | | - const solrConfigRequest = solrPost(configWithSolrPrefix)("config") |
| 71 | + /** @type {(op:string) => (fieldDef:FieldDef) => Promise<SolrResponse>} */ |
| 72 | + const solrSchemaReq = op => fieldDef => |
| 73 | + httpPostReq(`/api/cores/${core}/schema`)({ [op]: fieldDef }) |
84 | 74 |
|
85 | | - const solrSchemaRequest = op => data => |
86 | | - solrPost(config)("schema")({ [op]: data }) |
| 75 | + /** @type {(data:ConfigRequest) => Promise<SolrResponse> } */ |
| 76 | + const configReq = httpPostReq(`/api/cores/${core}/config`) |
87 | 77 |
|
88 | 78 | // now creating the API |
89 | 79 | return { |
90 | 80 | mergedConfig: () => config, |
91 | | - ping: () => |
92 | | - solrPost(configWithSolrPrefix)("admin/ping")({}) |
93 | | - .then(value => { |
94 | | - return value.status === "OK" |
95 | | - }) |
96 | | - .catch(() => false), |
| 81 | + |
| 82 | + commit: () => httpGetReq(`/solr/${core}/update`)({ commit: true }), |
| 83 | + |
| 84 | + /** @type {(data:QueryRequest) => Promise<SolrResponse>} */ |
| 85 | + query: httpPostReq(`/api/cores/${core}/query`), |
97 | 86 |
|
98 | 87 | /** @param {SolrDocument | SolrDocument[]} data */ |
99 | | - add: data => solrPost(config)("update")(ensureArray(data)), |
| 88 | + add: data => httpPostReq(`/api/cores/${core}/update`)(ensureArray(data)), |
100 | 89 |
|
101 | 90 | /** @param {DeleteRequest} deleteQuery */ |
102 | 91 | delete: deleteQuery => |
103 | | - solrPost(configWithSolrPrefix)("update")({ |
104 | | - delete: deleteQuery |
105 | | - }), |
| 92 | + httpPostReq(`/api/cores/${core}/update`)({ delete: deleteQuery }), |
| 93 | + |
| 94 | + /** @param {string} name */ |
| 95 | + deleteField: name => solrSchemaReq("delete-field")({ name }), |
| 96 | + |
| 97 | + /** @param {string} name */ |
| 98 | + deleteFieldType: name => solrSchemaReq("delete-field-type")({ name }), |
106 | 99 |
|
107 | 100 | /** @type {(data:FieldProperties) => Promise<SolrResponse>} */ |
108 | | - addField: solrSchemaRequest("add-field"), |
| 101 | + addField: solrSchemaReq("add-field"), |
109 | 102 |
|
110 | 103 | /** @type {(data:FieldTypeProperties) => Promise<SolrResponse>} */ |
111 | | - addFieldType: solrSchemaRequest("add-field-type"), |
| 104 | + addFieldType: solrSchemaReq("add-field-type"), |
112 | 105 |
|
113 | | - /** @type {({name:string}) => Promise<SolrResponse>} */ |
114 | | - deleteField: solrSchemaRequest("delete-field"), |
| 106 | + /** @type {(data:FieldTypeProperties) => Promise<SolrResponse>} */ |
| 107 | + replaceField: solrSchemaReq("replace-field"), |
115 | 108 |
|
116 | | - /** @type {({name:string}) => Promise<SolrResponse>} */ |
117 | | - deleteFieldType: solrSchemaRequest("delete-field-type"), |
| 109 | + config: configReq, |
118 | 110 |
|
119 | | - /** @type {(data:QueryRequest) => Promise<SolrResponse>} */ |
120 | | - query: solrPost(config)("query"), |
| 111 | + /** @type {() => Promise<string[]>} */ |
| 112 | + solrListFields: () => |
| 113 | + httpGetReq(`/solr/${core}/schema/fields`)({}).then(data => |
| 114 | + data.fields.map(field => field.name) |
| 115 | + ), |
121 | 116 |
|
122 | | - config: solrConfigRequest, |
123 | | - |
124 | | - /** |
125 | | - * Convenience function to set the `update.autoCreateFields` user property. |
126 | | - * @param {boolean} enable |
127 | | - */ |
| 117 | + /** @param {boolean} enable */ |
128 | 118 | configAutoEnableFields: enable => |
129 | | - solrConfigRequest({ |
| 119 | + configReq({ |
130 | 120 | "set-user-property": { |
131 | 121 | "update.autoCreateFields": enable ? "true" : "false" |
132 | 122 | } |
133 | 123 | }) |
134 | 124 | } |
135 | 125 | } |
136 | 126 |
|
| 127 | +/** @param {SolrConfig} userConfig */ |
| 128 | +const prepareCoreAdmin = userConfig => { |
| 129 | + const config = mergeConfig(defaultConfig, userConfig) |
| 130 | + return { |
| 131 | + /** @type {(core:string) => Promise<SolrResponse>} */ |
| 132 | + ping: core => |
| 133 | + axios |
| 134 | + .get(url.format({ ...config, pathname: `/solr/${core}/admin/ping` })) |
| 135 | + .then(convertSolrResponse) |
| 136 | + .catch(convertSolrError), |
| 137 | + |
| 138 | + /** @type {(core:string) => Promise<SolrResponse>} */ |
| 139 | + solrDeleteCore: core => |
| 140 | + axios |
| 141 | + .get( |
| 142 | + url.format({ |
| 143 | + ...mergeConfig(config, { |
| 144 | + query: { |
| 145 | + core, |
| 146 | + action: "UNLOAD", |
| 147 | + deleteIndex: true, |
| 148 | + deleteDataDir: true, |
| 149 | + deleteInstanceDir: true |
| 150 | + } |
| 151 | + }), |
| 152 | + pathname: "/solr/admin/cores" |
| 153 | + }) |
| 154 | + ) |
| 155 | + .then(convertSolrResponse) |
| 156 | + .catch(convertSolrError) |
| 157 | + } |
| 158 | +} |
| 159 | + |
137 | 160 | module.exports = { |
138 | 161 | prepareSolrClient, |
| 162 | + prepareCoreAdmin, |
139 | 163 | defaultConfig, |
140 | 164 | mergeConfig |
141 | 165 | } |
0 commit comments