Skip to content

Commit

Permalink
Update search API - #6116 (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvega190 authored Jun 14, 2023
1 parent c362220 commit 5745287
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/classes/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
Expand Down
2 changes: 1 addition & 1 deletion packages/content/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ import { createQuery, search } from '@craftercms/search';
import { parseDescriptor, preParseSearchResults } from '@craftercms/content';

search(
createQuery('elasticsearch', {
createQuery({
query: {
bool: {
filter: [/*...*/]
Expand Down
2 changes: 1 addition & 1 deletion packages/models/src/CrafterConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface Endpoints {
GET_NAV_TREE: string;
GET_BREADCRUMB: string;
TRANSFORM_URL: string;
ELASTICSEARCH: string;
SEARCH: string;
}

export default CrafterConfig;
2 changes: 1 addition & 1 deletion packages/search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: [/*...*/]
Expand Down
1 change: 0 additions & 1 deletion packages/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
*/

export * from './src/query';
export * from './src/elastic-query';
export * from './src/SearchService';
9 changes: 4 additions & 5 deletions packages/search/src/SearchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) => {
Expand All @@ -60,7 +59,7 @@ export function createQuery<T extends Query>(params?: Object): T {
? params['uuid']
: uuid();

query = new ElasticQuery();
query = new Query();
Object.assign(query.params, params);
query.uuid = queryId;

Expand Down
32 changes: 0 additions & 32 deletions packages/search/src/elastic-query.ts

This file was deleted.

8 changes: 8 additions & 0 deletions packages/search/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ export class Query {
}
}

/**
* Sets the actual query.
* @param {string} query - Query string
*/
set query(query) {
this.params = query;
}

}
10 changes: 5 additions & 5 deletions packages/search/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<ElasticQuery>({ 'uuid': '12345' });
const query = SearchService.createQuery<Query>({ 'uuid': '12345' });
query.query = {
"query" : {
"match_all" : {}
Expand 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<ElasticQuery>({ 'uuid': '12345' });
var query = SearchService.createQuery<Query>({ 'uuid': '12345' });
query.query = {
"query" : {
"bool": {
Expand Down
2 changes: 0 additions & 2 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@
import { PartialObserver } from 'rxjs';

export declare type ObserverOrNext<T> = (value: T) => void | PartialObserver<T>;

export declare type SearchEngines = 'elasticsearch';

0 comments on commit 5745287

Please sign in to comment.