{"id":16758232,"url":"https://github.com/foo123/advanced-search","last_synced_at":"2025-03-16T07:25:47.471Z","repository":{"id":143020633,"uuid":"59564842","full_name":"foo123/advanced-search","owner":"foo123","description":"transform any search box / search term query into an advanced multi-field search with custom search operators (PHP, Node/XPCOM/JS, Python)","archived":false,"fork":false,"pushed_at":"2016-05-29T13:31:12.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-22T19:48:43.736Z","etag":null,"topics":["search-query","searchbox"],"latest_commit_sha":null,"homepage":"https://foo123.github.io/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/foo123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-24T10:47:53.000Z","updated_at":"2022-12-17T21:10:07.000Z","dependencies_parsed_at":"2023-05-22T00:30:35.936Z","dependency_job_id":null,"html_url":"https://github.com/foo123/advanced-search","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Fadvanced-search","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Fadvanced-search/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Fadvanced-search/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foo123%2Fadvanced-search/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foo123","download_url":"https://codeload.github.com/foo123/advanced-search/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243838366,"owners_count":20356019,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["search-query","searchbox"],"created_at":"2024-10-13T04:04:34.703Z","updated_at":"2025-03-16T07:25:47.466Z","avatar_url":"https://github.com/foo123.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# advanced-search\n\nTransform any search box / search term query into an advanced multi-field search with custom search operators (PHP, Node/XPCOM/JS, Python)\n\n\n**What it does**\n\nThis enables out-of-the-box search boxes (actualy the search term query) to **textualy** encode complex (customizable) search queries, including `AND`/`OR` operators, `greater-than`, `less-than`, `like`, reference multiple different `search fields` and so on.. Similar to advanced search queries in search engines. **Note**, this **does not include a GUI for that** (although, one, can indeed use a GUI to build the complex search query), only the functionality to interpret the search term as complex query and get the parsed parts to be used then to do the actual query. The advantage is that one can use this to leverage **ANY** search form/search term to be able to encode a complex query without additional GUI or anything else extraneous and extravagant.\n\n\n**Example** (see `test/` folder)\n\n```javascript\nvar AdvancedSearch = require('../src/js/AdvancedSearch.js');\n\nfunction echo( s )\n{\n    console.log( \"object\" === typeof s ? JSON.stringify(s, null, 4) : s );\n}\n\necho(\"AdvancedSearch.VERSION = \" + AdvancedSearch.VERSION);\n\necho(['term',AdvancedSearch().query('term')]);\necho(['~\"term term2\"',AdvancedSearch().query('~\"term term2\"')]);\necho(['field=term',AdvancedSearch().query('field=term')]);\necho(['field\u003e=term field\u003c=term2',AdvancedSearch().query('field\u003e=term field\u003c=term2')]);\necho(['field\u003e=term field\u003c=term2, field2~term3',AdvancedSearch().query('field\u003e=term field\u003c=term2, field2~term3')]);\necho(['field\u003e=\"term term2\" field\u003c=term2, field2~\"field2~term3\", \"field2~term3\"',AdvancedSearch().query('field\u003e=\"term term2\" field\u003c=term2, field2~\"field2~term3\", \"field2~term3\"')]);\n```\n\n**output**\n\n```text\nAdvancedSearch.VERSION = 0.2.0\n[\n    \"term\",\n    [\n        [\n            {\n                \"field\": null,\n                \"op\": null,\n                \"term\": \"term\"\n            }\n        ]\n    ]\n]\n[\n    \"~\\\"term term2\\\"\",\n    [\n        [\n            {\n                \"field\": null,\n                \"op\": \"~\",\n                \"term\": \"term term2\"\n            }\n        ]\n    ]\n]\n[\n    \"field=term\",\n    [\n        [\n            {\n                \"field\": \"field\",\n                \"op\": \"=\",\n                \"term\": \"term\"\n            }\n        ]\n    ]\n]\n[\n    \"field\u003e=term field\u003c=term2\",\n    [\n        [\n            {\n                \"field\": \"field\",\n                \"op\": \"\u003e=\",\n                \"term\": \"term\"\n            },\n            {\n                \"field\": \"field\",\n                \"op\": \"\u003c=\",\n                \"term\": \"term2\"\n            }\n        ]\n    ]\n]\n[\n    \"field\u003e=term field\u003c=term2, field2~term3\",\n    [\n        [\n            {\n                \"field\": \"field\",\n                \"op\": \"\u003e=\",\n                \"term\": \"term\"\n            },\n            {\n                \"field\": \"field\",\n                \"op\": \"\u003c=\",\n                \"term\": \"term2\"\n            }\n        ],\n        [\n            {\n                \"field\": \"field2\",\n                \"op\": \"~\",\n                \"term\": \"term3\"\n            }\n        ]\n    ]\n]\n[\n    \"field\u003e=\\\"term term2\\\" field\u003c=term2, field2~\\\"field2~term3\\\", \\\"field2~term3\\\"\",\n    [\n        [\n            {\n                \"field\": \"field\",\n                \"op\": \"\u003e=\",\n                \"term\": \"term term2\"\n            },\n            {\n                \"field\": \"field\",\n                \"op\": \"\u003c=\",\n                \"term\": \"term2\"\n            }\n        ],\n        [\n            {\n                \"field\": \"field2\",\n                \"op\": \"~\",\n                \"term\": \"field2~term3\"\n            }\n        ],\n        [\n            {\n                \"field\": null,\n                \"op\": null,\n                \"term\": \"field2~term3\"\n            }\n        ]\n    ]\n]\n```\n\nThe output consists of a tree representing the complex query, where the first level\nrepresents `OR` clauses and the second level `AND` clauses (inside each `OR` clause) including operators (default `null` interpret as you like) and specific search `field` references (see above example to get an idea).\n\nOne could then contruct an actual query out of this (note how the original GUI of the application did not change at all, it was simply leveraged in functionality) like the following:\n\n```javascript\nvar sql = \"SELECT field1,field2,field3 FROM table\";\nvar query = AdvancedSearch().query(GET['search']);\nvar OR = [];\nfor(var i=0; i\u003cquery.length; i++)\n{\n    var AND = [], factor = query[i];\n    for(var j=0; j\u003cfactor.length; j++)\n    {\n        var term = factor[j];\n        if ( null == term.field )\n        {\n            // default search field\n            term.field = 'field1';\n        }\n        if ( null == term.op )\n        {\n            // default search operator\n            term.op = '=';\n        }\n        if ( '~' == term.op )\n        {\n            term.op = 'LIKE';\n        }\n        AND.push(term.field+' '+term.op+' '+db.esc(term.term));\n    }\n    OR.push('(' + AND.join(') AND (') + ')');\n}\nsql += ' WHERE ' + OR.join(' OR ');\ndb.exec(sql, function(results){\n    // get search results here\n});\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoo123%2Fadvanced-search","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoo123%2Fadvanced-search","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoo123%2Fadvanced-search/lists"}