saxulum / saxulum-elasticsearch-querybuilder
A query builder for elasticsearch
Installs: 49 781
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 1
Requires
- php: ~7.0
Requires (Dev)
- phpunit/phpunit: ^6.5.8
Suggests
- saxulum/saxulum-elasticsearch-querybuilder-generator: Generates a query builder based on a existing json query
README
Features
- A simple to use, flexible query builder for elastic search.
Requirements
- php: ~7.0
Suggests
- saxulum/saxulum-elasticsearch-querybuilder-generator: ~1.0
Installation
Through Composer as saxulum/saxulum-elasticsearch-querybuilder.
composer require saxulum/saxulum-elasticsearch-querybuilder "~3.3"
Usage
Important: By default empty nodes get not serialized. NullNode forces null value serialization.
- ArrayNode (no elements)
- BoolNode (null)
- ObjectNode (no elements)
- FloatNode (null)
- IntNode (null)
- StringNode (null)
This works recursive, which means theoretically a complex query builder can lead into an empty string as json query.
Check the allowSerializeEmpty
argument to prevent this if needed.
QueryBuilder
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode; use Saxulum\ElasticSearchQueryBuilder\Node\FloatNode; use Saxulum\ElasticSearchQueryBuilder\Node\IntNode; use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode; use Saxulum\ElasticSearchQueryBuilder\Node\StringNode; $qb = ObjectNode::create() ->add('query', ObjectNode::create() ->add('bool', ObjectNode::create() ->add('must', ObjectNode::create() ->add('term', ObjectNode::create() ->add('user', StringNode::create('kimchy')) ) ) ->add('filter', ObjectNode::create() ->add('term', ObjectNode::create() ->add('tag', StringNode::create('tech')) ) ) ->add('must_not', ObjectNode::create() ->add('range', ObjectNode::create() ->add('age', ObjectNode::create() ->add('from', IntNode::create(10)) ->add('to', IntNode::create(20)) ) ) ) ->add('should', ArrayNode::create() ->add(ObjectNode::create() ->add('term', ObjectNode::create() ->add('tag', StringNode::create('wow')) ) ) ->add(ObjectNode::create() ->add('term', ObjectNode::create() ->add('tag', StringNode::create('elasticsearch')) ) ) ) ->add('minimum_should_match', IntNode::create(1)) ->add('boost', FloatNode::create(1.1)) ) ); echo $qb->json(true);