saxulum/saxulum-elasticsearch-querybuilder

This package is abandoned and no longer maintained. No replacement package was suggested.

A query builder for elasticsearch

3.3.0 2018-07-20 08:52 UTC

README

Build Status Total Downloads Latest Stable Version Scrutinizer Code Quality Code Coverage

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);

Other samples

Converter