Skip to content

Commit

Permalink
return query and options in search result
Browse files Browse the repository at this point in the history
  • Loading branch information
fergiemcdowall committed Feb 12, 2025
1 parent 5c9e9fc commit 31a4b78
Show file tree
Hide file tree
Showing 31 changed files with 467 additions and 130 deletions.
5 changes: 3 additions & 2 deletions src/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export class Reader {
// This function reads queries in a JSON format and then translates them to
// Promises
#parseJsonQuery = (q, options = {}) => {
// options.PAGE = { NUMBER: 0, SIZE: 10, ...options.PAGE }

const runQuery = cmd => {
// if string or object with only FIELD or VALUE, assume
// that this is a GET
Expand Down Expand Up @@ -44,9 +42,12 @@ export class Reader {
const formatResults = result =>
result.RESULT
? Object.assign(result, {
QUERY: { q, options },
RESULT_LENGTH: result.RESULT.length
})
: {
QUERY: q,
OPTIONS: options,
RESULT: result,
RESULT_LENGTH: result.length
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const packageVersion = "5.1.4"
export const packageVersion = '5.1.4'
4 changes: 4 additions & 0 deletions test/src/514-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ test('simple _SEARCH with 1 clause', async t => {
t.plan(3)
await global[indexName].SEARCH(['Zeppelin']).then(res =>
t.deepEqual(res, {
QUERY: { AND: ['Zeppelin'] },
OPTIONS: { SCORE: { TYPE: 'TFIDF' }, SORT: true },
RESULT: [
{
_id: 3,
Expand All @@ -66,6 +68,8 @@ test('simple _SEARCH with 1 clause', async t => {

await global[indexName].SEARCH(['Zeppelin']).then(res =>
t.deepEqual(res, {
QUERY: { AND: ['Zeppelin'] },
OPTIONS: { SCORE: { TYPE: 'TFIDF' }, SORT: true },
RESULT: [],
RESULT_LENGTH: 0,
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 0, DOC_OFFSET: 0 }
Expand Down
232 changes: 119 additions & 113 deletions test/src/AND-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,70 +124,72 @@ test('simple _AND with 2 clauses', t => {

test('simple AND with 2 clauses (JSON)', t => {
t.plan(1)
global[indexName]
.QUERY({
AND: ['make:volvo', 'manufacturer:bmw']
})
.then(res => {
t.deepEqual(res, {
RESULT: [
{
_id: '8',
_match: [
{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' },
{ FIELD: 'manufacturer', VALUE: 'bmw', SCORE: '1.00' }
]
}
],
RESULT_LENGTH: 1,
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 1, DOC_OFFSET: 0 }
})
const q = {
AND: ['make:volvo', 'manufacturer:bmw']
}
global[indexName].QUERY(q).then(res => {
t.deepEqual(res, {
QUERY: q,
OPTIONS: {},
RESULT: [
{
_id: '8',
_match: [
{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' },
{ FIELD: 'manufacturer', VALUE: 'bmw', SCORE: '1.00' }
]
}
],
RESULT_LENGTH: 1,
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 1, DOC_OFFSET: 0 }
})
})
})

test('simple AND with 2 clauses (JSON)', t => {
t.plan(1)
global[indexName]
.QUERY({
AND: ['volvo', 'bmw']
})
.then(res => {
t.deepEqual(res, {
RESULT: [
{
_id: '1',
_match: [
{ FIELD: 'brand', VALUE: 'volvo', SCORE: '1.00' },
{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' },
{ FIELD: 'manufacturer', VALUE: 'volvo', SCORE: '1.00' }
]
},
{
_id: '9',
_match: [
{ FIELD: 'brand', VALUE: 'volvo', SCORE: '1.00' },
{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' }
]
},
{
_id: '8',
_match: [
{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' },
{ FIELD: 'manufacturer', VALUE: 'bmw', SCORE: '1.00' }
]
},
{
_id: '3',
_match: [
{ FIELD: 'brand', VALUE: 'bmw', SCORE: '1.00' },
{ FIELD: 'manufacturer', VALUE: 'volvo', SCORE: '1.00' }
]
}
],
RESULT_LENGTH: 4,
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 1, DOC_OFFSET: 0 }
})
const q = {
AND: ['volvo', 'bmw']
}
global[indexName].QUERY(q).then(res => {
t.deepEqual(res, {
QUERY: q,
OPTIONS: {},
RESULT: [
{
_id: '1',
_match: [
{ FIELD: 'brand', VALUE: 'volvo', SCORE: '1.00' },
{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' },
{ FIELD: 'manufacturer', VALUE: 'volvo', SCORE: '1.00' }
]
},
{
_id: '9',
_match: [
{ FIELD: 'brand', VALUE: 'volvo', SCORE: '1.00' },
{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' }
]
},
{
_id: '8',
_match: [
{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' },
{ FIELD: 'manufacturer', VALUE: 'bmw', SCORE: '1.00' }
]
},
{
_id: '3',
_match: [
{ FIELD: 'brand', VALUE: 'bmw', SCORE: '1.00' },
{ FIELD: 'manufacturer', VALUE: 'volvo', SCORE: '1.00' }
]
}
],
RESULT_LENGTH: 4,
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 1, DOC_OFFSET: 0 }
})
})
})

test('_AND with no VALUE', t => {
Expand Down Expand Up @@ -246,78 +248,82 @@ test('_AND with no VALUE', t => {

test('AND with no VALUE (JSON)', t => {
t.plan(1)
global[indexName]
.QUERY({
AND: [
const q = {
AND: [
{
FIELD: ['make']
}
]
}
global[indexName].QUERY(q).then(res => {
t.deepEqual(res, {
QUERY: q,
OPTIONS: {},
RESULT: [
{
_id: '1',
_match: [{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' }]
},
{
_id: '7',
_match: [{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' }]
},
{
_id: '9',
_match: [{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' }]
},
{
_id: '0',
_match: [{ FIELD: 'make', VALUE: 'tesla', SCORE: '1.00' }]
},
{
_id: '2',
_match: [{ FIELD: 'make', VALUE: 'tesla', SCORE: '1.00' }]
},
{
_id: '3',
_match: [{ FIELD: 'make', VALUE: 'tesla', SCORE: '1.00' }]
},
{
_id: '6',
_match: [{ FIELD: 'make', VALUE: 'tesla', SCORE: '1.00' }]
},
{
_id: '4',
_match: [{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' }]
},
{
FIELD: ['make']
_id: '5',
_match: [{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' }]
},
{
_id: '8',
_match: [{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' }]
}
]
})
.then(res => {
t.deepEqual(res, {
RESULT: [
{
_id: '1',
_match: [{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' }]
},
{
_id: '7',
_match: [{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' }]
},
{
_id: '9',
_match: [{ FIELD: 'make', VALUE: 'bmw', SCORE: '1.00' }]
},
{
_id: '0',
_match: [{ FIELD: 'make', VALUE: 'tesla', SCORE: '1.00' }]
},
{
_id: '2',
_match: [{ FIELD: 'make', VALUE: 'tesla', SCORE: '1.00' }]
},
{
_id: '3',
_match: [{ FIELD: 'make', VALUE: 'tesla', SCORE: '1.00' }]
},
{
_id: '6',
_match: [{ FIELD: 'make', VALUE: 'tesla', SCORE: '1.00' }]
},
{
_id: '4',
_match: [{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' }]
},
{
_id: '5',
_match: [{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' }]
},
{
_id: '8',
_match: [{ FIELD: 'make', VALUE: 'volvo', SCORE: '1.00' }]
}
],
RESULT_LENGTH: 10,
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 1, DOC_OFFSET: 0 }
})
],
RESULT_LENGTH: 10,
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 1, DOC_OFFSET: 0 }
})
})
})

// TODO: AND with nested OR

test('AND with no VALUE (JSON)', t => {
t.plan(1)
const { QUERY } = global[indexName]
QUERY({
const q = {
AND: [
'brand:volvo',
{
OR: ['make:bmw', 'make:volvo']
}
]
}).then(res => {
}
QUERY(q).then(res => {
t.deepEqual(res, {
QUERY: q,
OPTIONS: {},
RESULT: [
{
_id: '1',
Expand Down
36 changes: 23 additions & 13 deletions test/src/CACHE-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ test('query', t => {
)
.then(res => {
t.deepEqual(res, {
QUERY: { GET: 'brand:tesla' },
OPTIONS: {
BUCKETS: [{ FIELD: ['make'], VALUE: { GTE: 'volvo', LTE: 'volvo' } }]
},
BUCKETS: [
{ FIELD: ['make'], VALUE: { GTE: 'volvo', LTE: 'volvo' }, _id: [8] }
],
Expand All @@ -145,6 +149,10 @@ test('inspect cache', t => {
'{"funcLabel":"#parseJsonQuery","params":[{"GET":"brand:tesla"},{"BUCKETS":[{"FIELD":"make","VALUE":"volvo"}]}]}'
)
t.deepEquals(global[indexName]._CACHE.values().next().value, {
QUERY: { GET: 'brand:tesla' },
OPTIONS: {
BUCKETS: [{ FIELD: ['make'], VALUE: { GTE: 'volvo', LTE: 'volvo' } }]
},
RESULT: [
{
_id: 7,
Expand Down Expand Up @@ -184,22 +192,24 @@ test('inspect cache', t => {

test('run a duplicate query', t => {
t.plan(1)
const query = {
GET: 'brand:tesla'
}
global[indexName]
.QUERY(
{
GET: 'brand:tesla'
},
{
BUCKETS: [
{
FIELD: 'make',
VALUE: 'volvo'
}
]
}
)
.QUERY(query, {
BUCKETS: [
{
FIELD: 'make',
VALUE: 'volvo'
}
]
})
.then(res => {
t.deepEqual(res, {
QUERY: query,
OPTIONS: {
BUCKETS: [{ FIELD: ['make'], VALUE: { GTE: 'volvo', LTE: 'volvo' } }]
},
BUCKETS: [
{ FIELD: ['make'], VALUE: { GTE: 'volvo', LTE: 'volvo' }, _id: [8] }
],
Expand Down
Loading

0 comments on commit 31a4b78

Please sign in to comment.