Php library that converts search queries into words, phrases, hashtags, mentions, etc.
This library supports a simple search query standard. It is meant to support the most common search combinations that a user would likely enter into your website search box or dashboard application. It intentionally limits the more complex nested capabilities that you might expect from SQL builders, Lucene, etc.
Tokens are split on whitespace unless enclosed in double quotes. The following tokens are extracted by the Tokenizer:
class Token implements \JsonSerializable
{
constT_EOI = 0; // end of inputconstT_WHITE_SPACE = 1;
constT_IGNORED = 2; // an ignored token, e.g. #, !, etc. when found by themselves, don't do anything with them.constT_NUMBER = 3; // 10, 0.8, .64, 6.022e23constT_REQUIRED = 4; // '+'constT_PROHIBITED = 5; // '-'constT_GREATER_THAN = 6; // '>'constT_LESS_THAN = 7; // '<'constT_EQUALS = 8; // '='constT_FUZZY = 9; // '~'constT_BOOST = 10; // '^'constT_RANGE_INCL_START = 11; // '['constT_RANGE_INCL_END = 12; // ']'constT_RANGE_EXCL_START = 13; // '{'constT_RANGE_EXCL_END = 14; // '}'constT_SUBQUERY_START = 15; // '('constT_SUBQUERY_END = 16; // ')'constT_WILDCARD = 17; // '*'constT_AND = 18; // 'AND' or '&&'constT_OR = 19; // 'OR' or '||'constT_TO = 20; // 'TO' or '..'constT_WORD = 21;
constT_FIELD_START = 22; // The "field:" portion of "field:value".constT_FIELD_END = 23; // when a field lexeme ends, i.e. "field:value". This token has no value.constT_PHRASE = 24; // Phrase (one or more quoted words)constT_URL = 25; // a valid urlconstT_DATE = 26; // date in the format YYYY-MM-DDconstT_HASHTAG = 27; // #hashtagconstT_MENTION = 28; // @mentionconstT_EMOTICON = 29; // see https://en.wikipedia.org/wiki/EmoticonconstT_EMOJI = 30; // see https://en.wikipedia.org/wiki/EmojiThe T_WHITE_SPACE and T_IGNORED tokens are removed before the output is returned by the scan process.
The default query parser produces a ParsedQuery object which can be used with a builder to produce a query
for a given search service.
<?phpuseGdbots\QueryParser\QueryParser;
useGdbots\QueryParser\Builder\XmlQueryBuilder;
$parser = newQueryParser();
$builder = (newXmlQueryBuilder())->setHashtagFieldName('tags');
$result = $parser->parse('hello^5 planet:earth +date:2015-12-25 #omg');
echo$builder->addParsedQuery($result)->toXmlString();Produces the following xml:
<?xml version="1.0"?>
<query>
<wordboost="5"rule="should_match">hello</word>
<fieldname="planet">
<wordrule="should_match_term">earth</word>
</field>
<fieldname="date"bool_operator="required"cacheable="true">
<daterule="must_match_term">2015-12-25</date>
</field>
<fieldname="tags"bool_operator="required"cacheable="true">
<hashtagrule="must_match_term">omg</hashtag>
</field>
</query>To get a list of Node objects by type, use:
<?phpuseGdbots\QueryParser\Node\Hashtag;
$result = $parser->parse('#hashtag1 AND #hashtag2');
$hashtags = $result->getNodesOfType(Hashtag::NODE_TYPE);