From 5745287c7a5810d6c8ce4f0ceeeba3e78d0e990e Mon Sep 17 00:00:00 2001 From: Jose Javier Vega Date: Wed, 14 Jun 2023 12:37:45 -0600 Subject: [PATCH] Update search API - #6116 (#72) --- packages/classes/src/config.ts | 2 +- packages/content/README.md | 2 +- packages/models/src/CrafterConfig.ts | 2 +- packages/search/README.md | 2 +- packages/search/search.ts | 1 - packages/search/src/SearchService.ts | 9 ++++---- packages/search/src/elastic-query.ts | 32 ---------------------------- packages/search/src/query.ts | 8 +++++++ packages/search/test/index.spec.ts | 10 ++++----- packages/utils/src/types.ts | 2 -- 10 files changed, 21 insertions(+), 49 deletions(-) delete mode 100644 packages/search/src/elastic-query.ts diff --git a/packages/classes/src/config.ts b/packages/classes/src/config.ts index d6a1aa4..5a60917 100644 --- a/packages/classes/src/config.ts +++ b/packages/classes/src/config.ts @@ -31,7 +31,7 @@ const DEFAULTS: CrafterConfig = { GET_NAV_TREE: '/api/1/site/navigation/tree.json', GET_BREADCRUMB: '/api/1/site/navigation/breadcrumb.json', TRANSFORM_URL: '/api/1/site/url/transform.json', - ELASTICSEARCH: '/api/1/site/elasticsearch/search' + SEARCH: '/api/1/site/search/search.json' }, contentTypeRegistry: {}, headers: {} diff --git a/packages/content/README.md b/packages/content/README.md index 7c2e5e9..2bcd2cf 100644 --- a/packages/content/README.md +++ b/packages/content/README.md @@ -125,7 +125,7 @@ import { createQuery, search } from '@craftercms/search'; import { parseDescriptor, preParseSearchResults } from '@craftercms/content'; search( - createQuery('elasticsearch', { + createQuery({ query: { bool: { filter: [/*...*/] diff --git a/packages/models/src/CrafterConfig.ts b/packages/models/src/CrafterConfig.ts index 172d3c7..9b1aa5e 100644 --- a/packages/models/src/CrafterConfig.ts +++ b/packages/models/src/CrafterConfig.ts @@ -34,7 +34,7 @@ export interface Endpoints { GET_NAV_TREE: string; GET_BREADCRUMB: string; TRANSFORM_URL: string; - ELASTICSEARCH: string; + SEARCH: string; } export default CrafterConfig; diff --git a/packages/search/README.md b/packages/search/README.md index f94eb36..8ae55a2 100644 --- a/packages/search/README.md +++ b/packages/search/README.md @@ -39,7 +39,7 @@ All of Crafter CMS packages can be used either via npm or in plain html/javascri const { parseDescriptor, preParseSearchResults } = content; search( - createQuery('elasticsearch', { + createQuery({ query: { bool: { filter: [/*...*/] diff --git a/packages/search/search.ts b/packages/search/search.ts index c098e80..d6b2f28 100644 --- a/packages/search/search.ts +++ b/packages/search/search.ts @@ -15,5 +15,4 @@ */ export * from './src/query'; -export * from './src/elastic-query'; export * from './src/SearchService'; diff --git a/packages/search/src/SearchService.ts b/packages/search/src/SearchService.ts index 24a5f75..7beabd6 100644 --- a/packages/search/src/SearchService.ts +++ b/packages/search/src/SearchService.ts @@ -16,11 +16,10 @@ import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; -import { composeUrl, SearchEngines } from '@craftercms/utils'; +import { composeUrl } from '@craftercms/utils'; import { crafterConf, SDKService } from '@craftercms/classes'; import { CrafterConfig } from '@craftercms/models'; import { Query } from './query'; -import { ElasticQuery } from '@craftercms/search'; import uuid from 'uuid'; import 'url-search-params-polyfill'; @@ -39,8 +38,8 @@ export function search(queryOrParams: Query | Object, config?: CrafterConfig): O ? queryOrParams.params : queryOrParams; - if (queryOrParams instanceof ElasticQuery) { - requestURL = composeUrl(config, config.endpoints.ELASTICSEARCH) + '?crafterSite=' + config.site; + if (queryOrParams instanceof Query) { + requestURL = composeUrl(config, config.endpoints.SEARCH) + '?crafterSite=' + config.site; return SDKService.httpPost(requestURL, params) .pipe(map((response: any) => { @@ -60,7 +59,7 @@ export function createQuery(params?: Object): T { ? params['uuid'] : uuid(); - query = new ElasticQuery(); + query = new Query(); Object.assign(query.params, params); query.uuid = queryId; diff --git a/packages/search/src/elastic-query.ts b/packages/search/src/elastic-query.ts deleted file mode 100644 index 2112c7a..0000000 --- a/packages/search/src/elastic-query.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2007-2021 Crafter Software Corporation. All Rights Reserved. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ - -import { Query } from './query'; - -/** - * Query implementation for Elasticsearch - */ -export class ElasticQuery extends Query { - - /** - * Sets the actual query. - * @param {string} query - Query string - */ - set query(query) { - this.params = query; - } - -} diff --git a/packages/search/src/query.ts b/packages/search/src/query.ts index 62bca32..99bd505 100644 --- a/packages/search/src/query.ts +++ b/packages/search/src/query.ts @@ -55,4 +55,12 @@ export class Query { } } + /** + * Sets the actual query. + * @param {string} query - Query string + */ + set query(query) { + this.params = query; + } + } diff --git a/packages/search/test/index.spec.ts b/packages/search/test/index.spec.ts index 5a77377..d31422f 100644 --- a/packages/search/test/index.spec.ts +++ b/packages/search/test/index.spec.ts @@ -19,7 +19,7 @@ import { SearchService } from '@craftercms/search'; import { crafterConf } from '@craftercms/classes'; import 'mocha'; import 'url-search-params-polyfill'; -import { ElasticQuery } from '../src/elastic-query'; +import { Query } from "../src/query"; import mock from 'xhr-mock'; import MockRequest from "xhr-mock/lib/MockRequest"; @@ -42,13 +42,13 @@ describe('Search Client', () => { describe('search', () => { it('should find all documents', done => { - mock.post("http://localhost:8080/api/1/site/elasticsearch/search", + mock.post("http://localhost:8080/api/1/site/search/search.json", (req: MockRequest, res: MockResponse) => { res.body(JSON.stringify(searchResponse)); return res; }); - const query = SearchService.createQuery({ 'uuid': '12345' }); + const query = SearchService.createQuery({ 'uuid': '12345' }); query.query = { "query" : { "match_all" : {} @@ -63,13 +63,13 @@ describe('Search Client', () => { }); it('should apply all filters', done => { - mock.post("http://localhost:8080/api/1/site/elasticsearch/search", + mock.post("http://localhost:8080/api/1/site/search/search.json", (req: MockRequest, res: MockResponse) => { res.body(JSON.stringify(searchResponse)); return res; }); - var query = SearchService.createQuery({ 'uuid': '12345' }); + var query = SearchService.createQuery({ 'uuid': '12345' }); query.query = { "query" : { "bool": { diff --git a/packages/utils/src/types.ts b/packages/utils/src/types.ts index eb3f22d..5b384ad 100644 --- a/packages/utils/src/types.ts +++ b/packages/utils/src/types.ts @@ -17,5 +17,3 @@ import { PartialObserver } from 'rxjs'; export declare type ObserverOrNext = (value: T) => void | PartialObserver; - -export declare type SearchEngines = 'elasticsearch';