Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit 0a5838f

Browse files
committed
feature(Query): can obtain a JSON object from the created query
1 parent 63204f0 commit 0a5838f

File tree

10 files changed

+114
-51
lines changed

10 files changed

+114
-51
lines changed

dist/apisearch.js

Lines changed: 31 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.node.js

Lines changed: 31 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.node.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/apisearch.node.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/search-input.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
// 2.- compose query
4848
query = api.query
4949
.create(e.target.value)
50-
.enableHighlights()
50+
.toJSON()
5151
;
5252

5353
// 3.- Execute search

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "apisearch",
33
"version": "0.1.21",
44
"description": "Javascript client for Apisearch.",
5-
"main": "./dist/apisearch.node.js",
5+
"main": "./dist/apisearch.js",
66
"scripts": {
77
"examples": "webpack-dev-server --content-base examples/ --port 1234 --history-api-fallback index.html",
88
"dev": "webpack --config ./webpack.dev.js --progress --colors --watch",

src/Query/Query.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ export default class Query {
4242
this.page = params.page || QUERY_DEFAULT_PAGE;
4343
this.size = params.size || QUERY_DEFAULT_SIZE;
4444
this.from = params.from || QUERY_DEFAULT_FROM;
45-
this.results_enabled = params.results_enabled || true;
46-
this.aggregations_enabled = params.aggregations_enabled || true;
47-
this.suggestions_enabled = params.suggestions_enabled || false;
48-
this.highlight_enabled = params.highlight_enabled || false;
45+
this.results_enabled = params.results_enabled || null;
46+
this.aggregations_enabled = params.aggregations_enabled || null;
47+
this.suggestions_enabled = params.suggestions_enabled || null;
48+
this.highlight_enabled = params.highlight_enabled || null;
4949
this.filter_fields = params.filter_fields || [];
5050
this.user = params.user || null;
5151
this.coordinate = (typeof params.coordinate !== 'undefined')
@@ -54,8 +54,7 @@ export default class Query {
5454
params.coordinate.lon
5555
) : null
5656
;
57-
this.sort = {};
58-
this.sortBy(SORT_BY_SCORE);
57+
this.sort = null;
5958

6059
return this;
6160
}
@@ -589,4 +588,30 @@ export default class Query {
589588

590589
return null;
591590
}
591+
592+
toJSON() {
593+
let object = {
594+
q: this.q
595+
};
596+
597+
Object.keys(this).forEach((key) => {
598+
let value = this[key];
599+
600+
/**
601+
* When null, server handles its default values
602+
*/
603+
if (value instanceof Array && value.length === 0) return;
604+
if (value === null) return;
605+
if (key === 'page' && value === QUERY_DEFAULT_PAGE) return;
606+
if (key === 'from' && value === QUERY_DEFAULT_FROM) return;
607+
if (key === 'size' && value === QUERY_DEFAULT_SIZE) return;
608+
609+
object = {
610+
...object,
611+
[key]: value
612+
}
613+
});
614+
615+
return object;
616+
}
592617
}

test/apisearch.test.js

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,11 @@ describe('apisearch()', () => {
3333
items_promoted: [],
3434
coordinate: null,
3535
user: null,
36-
results_enabled: true,
37-
aggregations_enabled: true,
38-
highlight_enabled: false,
39-
suggestions_enabled: false,
40-
sort: {
41-
_score: {
42-
order: "asc"
43-
}
44-
}
36+
results_enabled: null,
37+
aggregations_enabled: null,
38+
highlight_enabled: null,
39+
suggestions_enabled: null,
40+
sort: null
4541
});
4642
});
4743

@@ -59,15 +55,11 @@ describe('apisearch()', () => {
5955
items_promoted: [],
6056
coordinate: null,
6157
user: null,
62-
results_enabled: true,
63-
aggregations_enabled: true,
64-
highlight_enabled: false,
65-
suggestions_enabled: false,
66-
sort: {
67-
_score: {
68-
order: "asc"
69-
}
70-
}
58+
results_enabled: null,
59+
aggregations_enabled: null,
60+
highlight_enabled: null,
61+
suggestions_enabled: null,
62+
sort: null
7163
});
7264
});
7365

@@ -92,15 +84,11 @@ describe('apisearch()', () => {
9284
lon: -12.345
9385
},
9486
user: null,
95-
results_enabled: true,
96-
aggregations_enabled: true,
97-
highlight_enabled: false,
98-
suggestions_enabled: false,
99-
sort: {
100-
_score: {
101-
order: "asc"
102-
}
103-
}
87+
results_enabled: null,
88+
aggregations_enabled: null,
89+
highlight_enabled: null,
90+
suggestions_enabled: null,
91+
sort: null
10492
});
10593
});
10694

0 commit comments

Comments
 (0)