Giter Club home page Giter Club logo

elasticsearch-query-builder's Introduction

ElasticSearch Query Builder

img php

This is a PHP library which helps you build query for an ElasticSearch client by using a fluent interface.

WARNING: This branch contains the next 3.x release. Check the corresponding issue for the roadmap.

Installation

composer require erichard/elasticsearch-query-builder "^3.0@beta"

Usage

use Erichard\ElasticQueryBuilder\QueryBuilder;
use Erichard\ElasticQueryBuilder\Aggregation\Aggregation;
use Erichard\ElasticQueryBuilder\Filter\Filter;

$qb = new QueryBuilder();

$qb
    ->setIndex('app')
    ->setSize(10)
;

// Add an aggregation
$qb->addAggregation(Aggregation::terms('agg_name', 'my_field'));
$qb->addAggregation(Aggregation::terms('agg_name_same_as_field'));

// Set query
$qb->setQuery(Query::terms('field', 'value'));

// I am using a client from elasticsearch/elasticsearch here
$results = $client->search($qb->build());

with PHP 8.1 you can use named arguments like this:

$query = new BoolQuery(must: [
    new RangeQuery(
        field: 'price',
        gte: 100
    ),
    new RangeQuery(
        field: 'stock',
        gte: 10
    ),
]);

or with the factory

$query = Query::bool(must: [
    Query::range(
        field: 'price',
        gte: 100
    ),
    Query::range(
        field: 'stock',
        gte: 10
    ),
]);

Contribution

  • Use PHPCS fixer and PHPStan
    • composer lint
  • Update tests (PHPUnit)
    • composer test

elasticsearch-query-builder's People

Contributors

craftlogan avatar erichard avatar igoooor avatar ilyes512 avatar jacbac avatar janaculenova avatar pionl avatar thrashzone13 avatar wucdbm avatar xsuchy09 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elasticsearch-query-builder's Issues

Roadmap 3.0

I've started a full rewrite with better terminology and wider elastic support.

It will also include test and documentation.

Check the UPGRADE.md for more information

Features

  • Query/Filter
    • Boolean
    • GeoDistance
    • GeoBoundingBox (thanks to @pionl)
    • GeoShape (thanks to @jaetooledev)
    • MatchPhrasePrefix
    • MatchPhrase
    • Match
    • MultiMatch
    • Nested
    • Prefix
    • Range
    • Term
    • Terms
    • Wildcard
    • Exists (thanks to @wucdbm )
    • QueryString
  • Aggregation
    • Filter
    • Max
    • Min
    • Nested
    • Reverse
    • Terms
    • TopHits
    • Cardinality
    • DateHistogram
    • Histogram (thanks to @pionl)
    • Range (thanks to @pionl)
    • Stat (thanks to @pionl)
    • Sum
    • WidthHistogram (thanks to @pionl)

Possible bug (or unclear mechanics)

This is the deer again!

I think i've found possible pug. Or i am just missing the logic in this code:
src/QueryBuilder.php:112

if (!empty($this->filters)) {
    $query['body']['query'] = [];
    foreach ($this->filters as $filter) {
        $query['body']['query'] = $filter->build();
    }
}

This part makes me feel weird.
$this->filters is an array, we iterate it, but rewriting $query['body']['query'] instead of appending (or whatever it should be).

Constructor arguments and argument setters throughout

As per #7 I have created a subtask for the below:

Every QueryInterface MUST have a constructor with all arguments needed to build a valid query
All others arguments MUST have a setter.

In addition, we need to ensure that the tests are updated to reflect this.

Feature request: Composite Aggregation

Hello there,

I believe that "composite aggregations" are currently missing in v3 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html

This is currently the class I built to handle that, it is very basic at the moment so all cases might not be covered. And I'm not sure if it follows the logic you have in your other aggregations but at least it might give an idea

<?php

declare(strict_types=1);

namespace Core\Search\Model\Search\DQL;

use Erichard\ElasticQueryBuilder\Aggregation\AbstractAggregation;

class CompositeAggregation extends AbstractAggregation
{
    public function __construct(
        string $name,
        private array $aggregations,
        private int $size = 10,
    ) {
        parent::__construct($name, []);
    }

    protected function buildAggregation(): array
    {
        $build = [];
        foreach ($this->aggregations as $aggregation) {
            $build[] = [
                $aggregation => [
                    'terms' => [
                        'field' => $aggregation,
                    ],
                ],
            ];
        }

        return [
            'sources' => $build,
            'size' => $this->size,
        ];
    }

    protected function getType(): string
    {
        return 'composite';
    }
}

v3 current status

Sorry for that silly question but I am a bit confused.
What we see in the README.md on main branch is not implemented fully yet right?
For example I don't find that method yet:

$query->addFilter($boolFilter);

I'm just asking to be sure I get it correctly. I don't intend to put pressure or anything ๐Ÿ™Œ

Add average aggregation

consider the following elastic query

`"aggs" => [
                "read_ratio_avg" => [
                    "avg" => [
                        "field" => "read-ratio",
                    ],
                ],
            ],
            "query" => [
                "bool" => [
                    "must" => [
                        [
                            "range" => [
                                "read-ratio" => [
                                    "gt" => 0,
                                ],
                            ],
                        ],
                        [
                            "match" => [
                                "url" => $hash,
                            ],
                        ],
                    ],
                ],
            ],`

your library handle every section of this query but there is no average aggregation in your library

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.