diff --git a/index.d.ts b/index.d.ts index d3e8fa9..edbb852 100644 --- a/index.d.ts +++ b/index.d.ts @@ -46,10 +46,7 @@ export class Document { options?: SearchOptions ): any // The shape of the resulting object can vary widely, // so we will put off typing it for now - search( - query: string, - options?: SearchOptions - ): Promise // The shape of the resulting object can vary widely, + contain(id: any): boolean; // TODO add async methods @@ -66,6 +63,7 @@ interface SearchOptions { bool?: "and" | "or" | "not"; query?: string; enrich?: boolean; + context?: boolean; //TODO: Sorting } @@ -104,6 +102,12 @@ export type CreateOptions = { filter?: FilterFn | string | false; rtl?: boolean; document?: CreateDocumentOptions; + context?: { + bidirectional?: boolean; + resolution?: number; + depth?: number + } + minlength?: number; }; // limit number Sets the limit of results. diff --git a/package.json b/package.json index b847558..5cb6e9c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@skiff-org/trawler", - "version": "0.1.2", + "version": "0.1.6", "description": "A modern search library for Skiff", "homepage": "https://github.com/skiff-org/trawler/", "author": "Thomas Wilkerling", diff --git a/src/index.js b/src/index.js index 67da068..74a6590 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ */ import { encode as default_encoder } from './lang/latin/default.js'; -import { create_object, create_object_array, concat, sort_by_length_down, is_array, is_object, parse_option } from './common.js'; +import { create_object, create_object_array, concat, sort_by_length_down, is_array, parse_option } from './common.js'; import { init_stemmer_or_matcher, init_filter } from './lang.js'; import apply_async from './async.js'; import { intersect } from './intersect.js'; @@ -212,24 +212,14 @@ export class Index { } /** * @param {string|Object} query - * @param {number|Object=} limit * @param {Object=} options * @returns {Array} */ - search(query, limit, options) { - if (!options) { - if (!limit && is_object(query)) { - options = /** @type {Object} */ (query); - query = options.query; - } - else if (is_object(limit)) { - options = /** @type {Object} */ (limit); - } - } - + search(query, options) { let result = []; let length; let context, suggest, offset = 0; + let limit = 100; if (options) { limit = options.limit; @@ -272,19 +262,14 @@ export class Index { return result; } - limit || (limit = 100); - let depth = this.depth && (length > 1) && (context !== false); let index = 0, keyword; if (depth) { keyword = query[0]; index = 1; - } - else { - if (length > 1) { - query.sort(sort_by_length_down); - } + } else if (length > 1) { + query.sort(sort_by_length_down); } for (let arr, term; index < length; index++) { @@ -303,8 +288,7 @@ export class Index { if (!suggest || (arr !== false) || !result.length) { keyword = term; } - } - else { + } else { arr = this.add_result(result, suggest, limit, offset, length === 1, term); } @@ -321,13 +305,11 @@ export class Index { // fallback to non-contextual search when no result was found depth = 0; index = -1; - continue; } return result; - } - else if (length === 1) { + } else if (length === 1) { // fast path optimization return single_result(result[0], limit, offset); }