Giter Club home page Giter Club logo

search-query-parser's Introduction

๐Ÿ” Search Query Parser

The Query String Parser library performs search query text parsing.

This library is perfect for integrating complex search (like Google search) into your application. Small part of base code (javascript) is come from https://github.com/nepsilon/search-query-parser

Example usage of Parser:

require (__DIR__."/vendor/autoload.php");

$parser = new dobron\SearchQueryParser\Parser([
  'keywords' => 'site,title,inurl'
]);
$result = $parser->parse('site:en.wikipedia.org/ title:Slovakia "cities and towns" -education inurl:wiki/');

echo json_encode($result, JSON_PRETTY_PRINT);

var_dump($result->getQueries());

Output:

{
    "text": [
        {
            "column": "text",
            "operator": "like",
            "negate": false,
            "value": "%cities and towns%"
        }
    ],
    "match": {
        "site": {
            "column": "site",
            "operator": "=",
            "negate": false,
            "value": "en.wikipedia.org\/"
        },
        "title": {
            "column": "title",
            "operator": "=",
            "negate": false,
            "value": "Slovakia"
        },
        "inurl": {
            "column": "inurl",
            "operator": "=",
            "negate": false,
            "value": "wiki\/"
        }
    },
    "excluded": {
        "text": [
            {
                "column": "text",
                "operator": "not like",
                "negate": true,
                "value": "%education%"
            }
        ]
    },
    "offsets": [
        {
            "keyword": "site",
            "value": "en.wikipedia.org\/",
            "offsetStart": 0,
            "offsetEnd": 22
        },
        {
            "keyword": "title",
            "value": "Slovakia",
            "offsetStart": 23,
            "offsetEnd": 37
        },
        {
            "text": "cities and towns",
            "offsetStart": 38,
            "offsetEnd": 54
        },
        {
            "keyword": "inurl",
            "value": "wiki\/",
            "offsetStart": 68,
            "offsetEnd": 79
        }
    ]
}
array(5) {
  [0]=>
  array(4) {
    ["column"]=>
    string(4) "site"
    ["operator"]=>
    string(1) "="
    ["negate"]=>
    bool(false)
    ["value"]=>
    string(17) "en.wikipedia.org/"
  }
  [1]=>
  array(4) {
    ["column"]=>
    string(5) "title"
    ["operator"]=>
    string(1) "="
    ["negate"]=>
    bool(false)
    ["value"]=>
    string(8) "Slovakia"
  }
  [2]=>
  array(4) {
    ["column"]=>
    string(5) "inurl"
    ["operator"]=>
    string(1) "="
    ["negate"]=>
    bool(false)
    ["value"]=>
    string(5) "wiki/"
  }
  [3]=>
  array(4) {
    ["column"]=>
    string(4) "text"
    ["operator"]=>
    string(8) "not like"
    ["negate"]=>
    bool(true)
    ["value"]=>
    string(11) "%education%"
  }
  [4]=>
  array(4) {
    ["column"]=>
    string(4) "text"
    ["operator"]=>
    string(4) "like"
    ["negate"]=>
    bool(false)
    ["value"]=>
    string(18) "%cities and towns%"
  }
}

Options:

  • 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.
  • offsets, a boolean 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.

Example usage of Compiler:

require(__DIR__."/vendor/autoload.php");

$parser = new dobron\SearchQueryParser\Compiler([
    // field, value
    ['site', 'en.wikipedia.org/'],

    // field, value
    ['title', 'Slovakia'],

    // value (array or string)
    [['cities and towns']],

    // or:
    // value, negate
    // ['cities and towns', false],

    // field, value, operator, negate
    [null, 'education', '=', true],

    // field, value, operator
    ['inurl', 'wiki/', '=']
]);

echo $parser->compile();

Output:

site:en.wikipedia.org/ title:Slovakia "cities and towns" -education inurl:wiki/

Options:

  • alwaysQuote, a boolean controls the behaviour of the searched query. If set to true, the query will contain the quotes. If set to false, the query will not contain the quotes. Defaults to false.

Queries:

Type Example
Query for tags cat
Query for multiple tags cat dog, "Hello World"
Exclude results containing a certain word cat -dog
Query for equality author:John Snow, author:"John Snow"
Query for multiple equality author:me,John Snow
Query for values in range date:2000/01/01-2020/01/01, price:5-50
Query for values greater than another value price:>10, price:>=10
Query for values less than another value price:<100, price:<=100
Mix query for tags and condition price:>10 price:<100
Filter qualifiers based on exclusion price:>10 -language:php

How to use it?

Install it using:

composer require dobron/search-query-parser

Test:

php vendor/bin/phpunit --testdox tests

search-query-parser's People

Contributors

richarddobron avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.