- A simple to use, flexible query builder for elastic search.
- php: ~7.0
- saxulum/saxulum-elasticsearch-querybuilder-generator: ~1.0
Through Composer as saxulum/saxulum-elasticsearch-querybuilder.
composer require saxulum/saxulum-elasticsearch-querybuilder "~3.3"
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.
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);