citizenzet/elastic-query-builder

package under development

1.0.0 2021-09-06 09:23 UTC

This package is auto-updated.

Last update: 2024-10-18 15:10:28 UTC


README

<?php
        $client = ClientBuilder::create()->setHosts(['localhost:9200'])->build();
        $query = new QueryBuilder();
        $query->setIndex("_my_index");
        $query->setSize(10);
        $query->setFrom(0);
        
        //Выбор полей
        $query->setSource(['title', "categories", 'features_values.Материал рамы']);  
        
        //Запрос
        // поиск по строке
        $query->setQuery((new MultiMatchQuery())->setQuery("Oko Plus")->setFields(['title'])->setFuzziness(1)); 
        
        //ФИльтрация
        $query->addFilter(
            (new NestedQuery())->setNestedPath("features_values")
                ->should()
                ->addQuery((new TermsQuery())->setField("features_values.Материал рамы")->setValues(["сталь"]))
                ->addQuery((new TermsQuery())->setField("features_values.Сиденье")->setValues(["без спинки", "со спинкой"]))
        );
        $query->addFilter((new TermsQuery())->setField("features_values.Внутренний материал")->setValues(["байка"]));
        $query->addFilter((new TermsQuery())->setField("categories")->setValues([2879]));
        //Сортировка
        $query->addSorting("_id", "desc");
        
        //Агрегации
        // сумма
        $query->addAggregation((new SumAggregation("test_sum"))->setField('count')); 
        // максимальное значение
        $query->addAggregation((new MaxAggregation("max_price"))->setField('price')); 
         // минимальное значение
        $query->addAggregation((new MinAggregation("max_price"))->setField('price')); 
        
        //вложенные (не тестировано!!!)
        $query->addAggregation((new NestedAggregation("features"))->setNestedPath('features')->setAggregation(
            (new TermsAggregation("fffff"))->setField("features.1")->setSize(20))
        );
        
        $response = $client->search($query->build());
        
        $r = Response::instance($response);
        $productCodes = $r->getSourceKeyAsArray("code");
        $productIds = $r->getIds();