All Projects → nepsilon → Search Query Parser

nepsilon / Search Query Parser

Licence: mit
A simple parser for advanced search query syntax

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Search Query Parser

Open Semantic Search
Open Source research tool to search, browse, analyze and explore large document collections by Semantic Search Engine and Open Source Text Mining & Text Analytics platform (Integrates ETL for document processing, OCR for images & PDF, named entity recognition for persons, organizations & locations, metadata management by thesaurus & ontologies, search user interface & search apps for fulltext search, faceted search & knowledge graph)
Stars: ✭ 386 (+114.44%)
Mutual labels:  search, search-interface
Search Dialog
An easy to use, yet very customizable search dialog
Stars: ✭ 503 (+179.44%)
Mutual labels:  search, search-interface
Calaca
Search UI for Elasticsearch
Stars: ✭ 318 (+76.67%)
Mutual labels:  search, search-interface
Ransack
Object-based searching.
Stars: ✭ 5,020 (+2688.89%)
Mutual labels:  search, search-interface
Typesense Instantsearch Adapter
A JS adapter library to build rich search interfaces with Typesense and InstantSearch.js
Stars: ✭ 56 (-68.89%)
Mutual labels:  search, search-interface
Search Ui
Search UI. Libraries for the fast development of modern, engaging search experiences.
Stars: ✭ 1,294 (+618.89%)
Mutual labels:  search, search-interface
Instantsearch Ios
⚡️ A library of widgets and helpers to build instant-search applications on iOS.
Stars: ✭ 498 (+176.67%)
Mutual labels:  search, search-interface
Instantsearch Ios Examples
Example apps built with InstantSearch iOS
Stars: ✭ 55 (-69.44%)
Mutual labels:  search, search-interface
Open Semantic Search Apps
Python/Django based webapps and web user interfaces for search, structure (meta data management like thesaurus, ontologies, annotations and named entities) and data import (ETL like text extraction, OCR and crawling filesystems or websites)
Stars: ✭ 55 (-69.44%)
Mutual labels:  search, search-interface
Awesome Solr
A curated list of Awesome Apache Solr links and resources.
Stars: ✭ 69 (-61.67%)
Mutual labels:  search, search-interface
Instantsearch Android
A library of widgets and helpers to build instant-search applications on Android.
Stars: ✭ 129 (-28.33%)
Mutual labels:  search, search-interface
Jkes
A search framework and multi-tenant search platform based on java, kafka, kafka connect, elasticsearch
Stars: ✭ 173 (-3.89%)
Mutual labels:  search
Astpath
A command-line search utility for Python ASTs using XPath syntax.
Stars: ✭ 167 (-7.22%)
Mutual labels:  search
Modernsearchbar
The famous iOS search bar with auto completion feature implemented.
Stars: ✭ 167 (-7.22%)
Mutual labels:  search
Angular Search Experience
Algolia + Angular = 🔥🔥🔥
Stars: ✭ 167 (-7.22%)
Mutual labels:  search
Mcfly
Fly through your shell history. Great Scott!
Stars: ✭ 3,206 (+1681.11%)
Mutual labels:  search
Keyvi
Keyvi - a key value index that powers Cliqz search engine. It is an in-memory FST-based data structure highly optimized for size and lookup performance.
Stars: ✭ 171 (-5%)
Mutual labels:  search
Algoliasearch Client Swift
⚡️ A fully-featured and blazing-fast Swift API client to interact with Algolia.
Stars: ✭ 166 (-7.78%)
Mutual labels:  search
Eclipse Instasearch
Eclipse plug-in for fast code search
Stars: ✭ 165 (-8.33%)
Mutual labels:  search
Query Translator
Query Translator is a search query translator with AST representation
Stars: ✭ 165 (-8.33%)
Mutual labels:  search

Search Query Syntax Parser

A simple parser for advanced search query syntax.

Build Status

It parses a string like this:

from:[email protected],[email protected] to:me subject:vacations date:1/10/2013-15/04/2014 photos

And turns it into an object like this:

{
  from: ['[email protected]', '[email protected]'],
  to: 'me',
  subject: 'vacations',
  date: {
    from: '1/10/2013',
    to: '15/04/2014'
    },
  text: 'photos',
  offsets: 
   [ { keyword: 'from', value: '[email protected],[email protected]', offsetStart: 0, offsetEnd: 32 },
     { keyword: 'to', value: 'me', offsetStart: 33, offsetEnd: 38 },
     { keyword: 'subject', value: 'vacations', offsetStart: 39, offsetEnd: 56 },
     { keyword: 'date', value: '1/10/2013-15/04/2014', offsetStart: 57, offsetEnd: 82 },
     { text: 'photos', offsetStart: 83, offsetEnd: 89 } ]
}

Installation

$ npm install search-query-parser

Usage

var searchQuery = require('search-query-parser');

var query = 'from:[email protected],[email protected] to:me subject:vacations date:1/10/2013-15/04/2014 photos';
var options = {keywords: ['from', 'to', 'subject'], ranges: ['date']}

var searchQueryObj = searchQuery.parse(query, options);

// searchQueryObj.from is now ['[email protected]', '[email protected]']
// searchQueryObj.to is now 'me'
// searchQueryObj.date is now {from: '1/10/2013', to: '15/04/2014'}
// searchQueryObj.text is now 'photos'

You can configure what keywords and ranges the parser should accept with the options argument. It accepts 5 values:

  • keywords, that can be separated by commas (,). Accepts an array of strings.
  • ranges, that can be separated by a hyphen (-). Accepts an array of strings.
  • tokenize, that controls the behaviour of text search terms. If set to true, non-keyword text terms are returned as an array of strings where each term in the array is a whitespace-separated word, or a multi-word term surrounded by single- or double-quotes.
  • alwaysArray, a boolean that controls the behaviour of the returned query. If set to true, all matched keywords would always be arrays instead of strings. If set to false they will be strings if matched a single value. Defaults to false.
  • offsets, a boolean that controls the behaviour of the returned query. If set to true, the query will contain the offsets object. If set to false, the query will not contain the offsets object. Defaults to true.

If no keywords or ranges are specified, or if none are present in the given search query, then searchQuery.parse will return a string if tokenize is false, or an array of strings under the key text if tokenize is true.

var searchQuery = require('search-query-parser');

var query = 'a query with "just text"';
var parsedQuery = searchQuery.parse(query);
// parsedQuery is now 'a query with "just text"'

var options = {keywords: ['unused']};
var parsedQueryWithOptions = searchQuery.parse(query, options);
// parsedQueryWithOptions is now 'a query with "just text"'

var options2 = {tokenize: true};
var parsedQueryWithTokens = searchQuery.parse(query, options2);
// parsedQueryWithTokens is now: ['a', 'query', 'with', 'just text']

You can also use exclusion syntax, like -from:[email protected] name:hello,world. This also works with non-keyword text terms when tokenize is set to true.

{
  name: ['hello', 'world'],
  exclude: {
    from: ['[email protected]']
  }
}

Sometimes checking against whether a keyword holds string or not can be excessive and prone to errors; it's often easier to simply expect everything is an array even if it means doing 1-iteration loops often.

var searchQuery = require('search-query-parser');

var query = 'test:helloworld fun:yay,happy';
var options = {keywords: ['test', 'fun']};
var parsedQueryWithOptions = searchQuery.parse(query, options);
// parsedQueryWithOptions is now:
// {
//   test: 'helloworld',
//   fun: ['yay', 'happy']
// }

var optionsAlwaysArray = {keywords: ['test', 'fun'], alwaysArray: true};
var parsedQueryWithOptions = searchQuery.parse(query, options);
// parsedQueryWithOptions is now:
// {
//   test: ['helloworld'], //No need to check whether test is a string or not!
//   fun: ['yay', 'happy']
// }

The offsets object could become pretty huge with long search queries which could be an unnecessary use of space if no functionality depends on it. It can simply be turned off using the option offsets: false

Typescript

Typescript types are available for this library in the docs directory. Browse type documentation here.

Documentation is generated using node_modules/.bin/typedoc index.d.ts

Testing

The 29 tests are written using the BDD testing framework should.js, and run with mocha.

Run npm install should and npm install -g mocha to install them both.

Run tests with make test.

License

The MIT License (MIT)

Copyright (c) 2014

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].