makinacorpus/elasticsearch-query

ElasticSearch/OpenSearch query builder

1.0.0-alpha3 2023-03-15 14:08 UTC

This package is auto-updated.

Last update: 2024-04-08 14:46:54 UTC


README

This is very small query builder for ElasticSearch and OpenSearch, mostly opiniated around building bool queries.

A simple example:

// Create a query.
$query = new RootBoolQuery();

// Add some filters.
$query->must()->term('statut', 20);

// Create a nested bool query, for nested objects.
$nested = $query->must()->createNestedBool('statut_histo');
$nested->must()->term('statut_histo.statut', 23);

// Set some query options.
$query->size(100);
$query->from(0);
$query->trackTotalHits();

// Add some sorts.
$query->sort('pushed_at');
$query->sort('created_at');

\json_encode($query->build(), JSON_PRETTY_PRINT);

Which will generated the follwoing JSON:

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "statut": 20
                    }
                },
                {
                    "nested": {
                        "path": "statut_histo",
                        "query": {
                            "bool": {
                                "must": {
                                    "term": {
                                        "statut_histo.statut": 23
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 100,
    "track_total_hits": true,
    "sort": {
        "pushed_at": {
            "order": "asc"
        },
        "created_at": {
            "order": "asc"
        }
    }
}

More documentation may come later.