PHP code example of wieni / wmsearch

1. Go to this page and download the library: Download wieni/wmsearch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wieni / wmsearch example snippets


class Article extends Node implements WmModelInterface, ElasticEntityInterface
{
    use EntityPageTrait;

    public function getElasticTypes()
    {
        return ['page'];
    }

    public function getElasticDocumentCollection($type)
    {
        return 'mymodule.elastic.article.collection';
    }
}

namespace Drupal\mymodule\Service\Elastic\Collection;

use Drupal\wmsearch\Entity\Document\EntityDocumentCollection;
use Drupal\wmsearch\Exception\NotIndexableException;

class ArticleCollection extends EntityDocumentCollection
{
    /** @var \Drupal\mymodule\Entity\Node\Article */
    protected $entity;

    public function toElasticArray($elasticId)
    {
        if (!$this->entity->isPublished()) {
            throw new NotIndexableException();
        }

        return [
            'id' => $this->entity->id(), // this isn't the elasticId
            'title' => $this->entity->getTitle(),
            'url' => $this->entity->toUrl()->toString(),
            'language' => $this->entity->language()->getId(),
            // ...
        ];
    }
}

$perPage = 10;
$page = (int) $req->query->get('page');
$input = $req->query->get('q', '');

$query = new PageQuery();
$query->from($perPage * $page)
    ->size($perPage)
    ->setHighlight(1, 120, ['title', 'intro'], '<em>', '</em>')
    ->addMultiMatch($input, ['title', 'intro', 'body']);

$formatter->format($api->highlightSearch($query));

$query = new PageQuery();
    ->setSource('')
    ->complete($input, 2);

$formatter->format($api->search($query));