Search in index

@param array $data

@return array
549204
public function search(array $data = [])
    {
        // schema should be set
        if (!isset($data['schema'])) {
            return false;
        }

        $model = $data['schema'];
        // check schema data
        $path = cradle()->package('global')->path('config') . '/admin/schema/' . $model . '.php';
        if (!file_exists($path)) {
            return false;
        }

        // 4. Process Data
        $schema = SystemSchema::i($model);
        
        if(is_null($schema)) {
            throw SystemException::forNoSchema();
        }
        
        $searchable = $schema->getSearchableFieldNames();
        $primary = $schema->getPrimaryFieldName();
        
        //set the defaults
        $filter = [];
        $range = 50;
        $start = 0;
        $order = [$primary => 'asc'];
        $count = 0;

        //merge passed data with default data
        if (isset($data['filter']) && is_array($data['filter'])) {
            $filter = $data['filter'];
        }

        if (isset($data['range']) && is_numeric($data['range'])) {
            $range = $data['range'];
        }

        if (isset($data['start']) && is_numeric($data['start'])) {
            $start = $data['start'];
        }

        if (isset($data['order']) && is_array($data['order'])) {
            $order = $data['order'];
        }

        //prepare the search model
        $search = [];

        //keyword search
        if (isset($data['q'])) {
            if (!is_array($data['q'])) {
                $data['q'] = [$data['q']];
            }

            foreach ($data['q'] as $keyword) {
                $search['query']['bool']['filter'][]['query_string'] = [
                    'query' => $keyword . '*',
                    'fields' => $searchable,
                    'default_operator' => 'AND'
                ];
            }
        }


        //generic full match filters

        //model_active
        $activeField = $schema->getActiveFieldName();
        if ($activeField) {
            if (!isset($filter[$activeField])) {
                $filter[$activeField] = 1;
            }
        }

        foreach ($filter as $key => $value) {
            $search['query']['bool']['filter'][]['term'][$key] = $value;
        }

        //add sorting
        foreach ($order as $sort => $direction) {
            $search['sort'] = [$sort => $direction];
        }
        
        try {
            $results = $this->resource->search([
                'index' => $model,
                'type' => static::INDEX_TYPE,
                'body' => $search,
                'size' => $range,
                'from' => $start
            ]);
        } catch (NoNodesAvailableException $e) {
            return false;
        } catch (\Throwable $e) {
            // catch throwable,
            // this means it will use sql as source of data
            return false;
        }

        // fix it
        $rows = array();

        foreach ($results['hits']['hits'] as $item) {
            $rows[] = $item['_source'];
        }
        
        //return response format
        return [
            'rows' => $rows,
            'total' => $results['hits']['total']
        ];
    }
840613
public function getRelationQuery(array $data) {
        foreach ($this->links as $key => $val) {
            $this->query->whereIn(
                $val, $this->getKeys($data, $key)
            );
        }
        return $this->query;
    }
700745
public function search(array $data)
    {
        $name = iconv('utf-8', 'cp1251', $data['name']);
        $url = str_replace('#NAME#', urlencode($name), self::SEARH_URL);
        $url = str_replace('#SECTOR#', (isset($data['type']) ? $data['type'] : self::DEFAULT_SECTOR), $url);
        // get list from xpath
        $dom = $this->browser->getDom($url);
        $xpath = new \DOMXPath($dom);

        // if for request is found only one result is produced forwarding
        $refresh = $xpath->query('//meta[@http-equiv="Refresh"]/@content');
        if ($refresh->length) {
            list(, $url) = explode('url=', $refresh->item(0)->nodeValue, 2);
            // add http if need
            if ($url[0] == '/') {
                $url = $this->browser->getHost().$url;
            }
            $name = iconv('cp1251', 'utf-8', $name);
            if (!preg_match('/id=(?<id>\d+)/', $url, $mat) || !($type = $this->filler->getItemType($url))) {
                throw new NotFoundHttpException('Incorrect URL for found item');
            }

            return [
                new ItemSearch(
                    $name,
                    $this->getLinkForFill($url),
                    $this->filler->getCoverUrl($mat['id'], $type),
                    ''
                ),
            ];
        }

        $rows = $xpath->query(self::XPATH_FOR_LIST);

        $list = [];
        foreach ($rows as $el) {
            $link = $xpath->query('a', $el);
            // has link on source
            if ($link->length &&
                ($href = $link->item(0)->getAttribute('href')) &&
                ($name = $link->item(0)->nodeValue) &&
                preg_match('/id=(?<id>\d+)/', $href, $mat) &&
                ($type = $this->filler->getItemType($href))
            ) {
                $list[] = new ItemSearch(
                    str_replace(["\r\n", "\n"], ' ', $name),
                    $this->getLinkForFill($this->browser->getHost().'/'.$href),
                    $this->filler->getCoverUrl($mat['id'], $type),
                    trim(str_replace($name, '', $el->nodeValue)),
                    $this->browser->getHost().'/'.$href
                );
            }
        }

        return $list;
    }
60074
public function search(array $post)
    {
        $sort = (isset($post['sort'])) ? $post['sort'] : '';
        unset($post['sort'], $post['count'], $post['offset'], $post['page']);

        $searches = [];

        foreach ($post as $key => $value) {
            $searches[] = [
                'searchString' => $value,
                'columns' => explode('-', $key),
            ];
        }

        $models = $this->getMapper()->search($searches, $sort);

        return $models;
    }
904168
function filter($data = array())
    {
        if (! is_array($data)) {
            return $data;
        }
        // first apply the filters to the ROOT
        if (isset($this->filters[self::SELECTOR_ROOT])) {
            /* @var $rootFilters FilterSet */
            $rootFilters = $this->filters[self::SELECTOR_ROOT];
            $data = $rootFilters->applyFilters($data);
        }
        foreach ($data as $key => $value) {
            $data[$key] = $this->filterItem($data, $key);
        }
        return $data;
    }
815106
function find(array $data)
    {
        if ($data == array_values($data))
            throw new \InvalidArgumentException('Data tags must be associative array.');

        if (isset($data['etag']) && $hash = $data['etag']) {
            ## ETags is unique and if present only search for etag match
            #
            if ($this->has($hash))
                yield $hash => $this->_objs[$hash]['object'];

        } else {
            ## Find data match
            #
            foreach($this->_objs as $hash => $obAr) {
                $obData = $obAr['data'];
                if ($data == array_intersect_assoc($obData, $data))
                    yield $hash => $this->_objs[$hash]['object'];
            }
        }
    }
558983
protected final function getWhere($data) {
        $con = [];
        foreach ($this->primaryKeys as $f) {
            if (array_key_exists($f, $data)) {
                if (isset($data[ $f ])) {
                    $con [ $f ] = $data [ $f ];
                } else {
                    $con [ $f . ' $' ] = null;
                }
            }
        }

        return $con;
    }
867569
public function searchIndex($value)
    {
        foreach ($this->getGenerator() as $keyFromArray => $valueFromArray) {
            if ($value === $valueFromArray) {
                return $keyFromArray;
            }
        }

        return false;
    }
811357
public function filerBySearch($data) {
        $filter = $data['search'];
        $condition = array(
            'OR' => array(
                $this->alias . '.nombre LIKE' => '%' . $filter . '%',
                $this->alias . '.nrodocumento LIKE' => '%' . $filter . '%',
                $this->alias . '.telefono LIKE' => '%' . $filter . '%',
                $this->alias . '.domicilio LIKE' => '%' . $filter . '%',
                $this->alias . '.codigo' => $filter,
            )
        );
        return $condition;
    }
801091
protected function filterData($data): array
    {
        if (empty($this->getFields())) {
            return $data;
        }
        return array_intersect_key($data, array_flip($this->getFields()));
    }
1224468
protected function getWhereClause(array $data): array
    {
        $result = [];

        if (empty($data['criteria'])) {
            return $result;
        }

        // get current module searchable fields.
        $moduleFields = $this->filterFields(array_keys($this->searchFields));

        foreach ($data['criteria'] as $fieldName => $criterias) {
            if (empty($criterias)) {
                continue;
            }

            $fieldName = $this->table->aliasField($fieldName);
            if (!in_array($fieldName, $moduleFields)) {
                continue;
            }

            foreach ($criterias as $criteria) {
                $condition = $this->getWhereCondition($fieldName, $criteria);

                if (empty($condition)) {
                    continue;
                }
                $result[] = $condition;
            }
        }

        return $result;
    }
191288
private function itemsFromArray(array $data): array
    {
        $objects = [];
        foreach ($data[$this->key()] as $objectData) {
            $objects[] = $this->item->from($objectData);
        }
        return $objects;
    }
253958
public function search(array $search)
    {
        $rows = new Collection();

        if (empty($search)) {
            return $rows;
        }

        foreach ($this->getCart() as $item) {
            if (array_intersect_assoc($item->intersect($search)->toArray(), $search)) {
                $rows->put($item->__raw_id, $item);
            }
        }

        return $rows;
    }
287335
public function search(array $data = [])
    {
        //set the defaults
        $filter = [];
        $range = 50;
        $start = 0;
        $order = ['model_id' => 'asc'];
        $count = 0;

        //merge passed data with default data
        if (isset($data['filter']) && is_array($data['filter'])) {
            $filter = $data['filter'];
        }

        if (isset($data['range']) && is_numeric($data['range'])) {
            $range = $data['range'];
        }

        if (isset($data['start']) && is_numeric($data['start'])) {
            $start = $data['start'];
        }

        if (isset($data['order']) && is_array($data['order'])) {
            $order = $data['order'];
        }

        //prepare the search object
        $search = [];


        //keyword search
        if (isset($data['q'])) {
            if (!is_array($data['q'])) {
                $data['q'] = [$data['q']];
            }

            foreach ($data['q'] as $keyword) {
                $search['query']['bool']['filter'][]['query_string'] = [
                    'query' => $keyword . '*',
                    'fields' => [
                        'model_singular','model_plural','model_detail',
                    ],
                    'default_operator' => 'AND'
                ];
            }
        }


        //generic full match filters

        //model_active
        if (!isset($filter['model_active'])) {
            $filter['model_active'] = 1;
        }


        foreach ($filter as $key => $value) {
            $search['query']['bool']['filter'][]['term'][$key] = $value;
        }

        //add sorting
        foreach ($order as $sort => $direction) {
            $search['sort'] = [$sort => $direction];
        }

        try {
            $results = $this->resource->search([
                'index' => static::INDEX_NAME,
                'type' => static::INDEX_TYPE,
                'body' => $search,
                'size' => $range,
                'from' => $start
            ]);
        } catch (NoNodesAvailableException $e) {
            return false;
        }

        // fix it
        $rows = array();

        foreach ($results['hits']['hits'] as $item) {
            $rows[] = $item['_source'];
        }

        //return response format
        return [
            'rows' => $rows,
            'total' => $results['hits']['total']
        ];
    }
1328323
public function filterData(array $data)
    {
        // Filter the sparse field-set if we have a specific list of properties
        // defined that we want.
        $filterProps = $this->filterProps();
        if (!empty($filterProps)) {
            $filteredData = array_filter($data, function ($key) use ($filterProps) {
                return \in_array($key, $filterProps, true);
            }, ARRAY_FILTER_USE_KEY);

            // We must always have some data after filtering, so if our filtered
            // data is empty, we should just ignore it ...
            if (!empty($filteredData)) {
                $data = $filteredData;
            }
        }

        return $data;
    }
387172
public function find($data)
    {
        foreach ($data as $key => $value) {
            if ($this->$key === $value) {
                return $this;
            }
        }
    }
866785
private function getUrl(array $data)
    {
        $urlParams = ['q' => $data['value'], $data['attribute_code'] => $data['value']];

        return $this->urlBuilder->getUrl('catalogsearch/result', ['_query' => $urlParams]);
    }