2017 © Pedro Peláez
 

drupal-module wmsearch

Elasticsearch api

image

wieni/wmsearch

Elasticsearch api

  • Thursday, July 19, 2018
  • by Wieni
  • Repository
  • 9 Watchers
  • 0 Stars
  • 555 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 26 Versions
  • 54 % Grown

The README.md

WMSearch

Manages an index with a single document type page, (*1)

The api allows adding different document types but only page is 'managed'., (*2)

Elasticsearch support

Elasticsearch version Last release Development branch
5.x 0.9.6 release/v0.9
6.x / feature/elastic-6.x
7.x 0.11.0 main

API

inject wmsearch.api.index, (*3)

Index

$api->createIndex();, (*4)

$api->deleteIndex();, (*5)

Document

$api->addDoc(ElasticEntityInterface $entity); // upsert, (*6)

$api->delDoc($id);, (*7)

$api->getDoc($id);, (*8)

$api->search(QueryInterface) : SearchResult, (*9)

// Identical to ->search() but strips html from highlights
$api->highlightSearch(QueryInterface) : SearchResult

Misc

$api->health(); // simple http elastic healthcheck, (*10)

$api->refresh() // reopen lucene index, (*11)

$api->flush() // fsync the lucene index, (*12)

JSON

GET /search/json?q=lorem%20ipsum&o=0&a=10, (*13)

q  string The query
o  int    The offset
a int    Amount of items

Drush

drush wmsp                      # Recreate the current index, removing all documents. Alias for wmsearch:purge
drush wmsq                      # Queue content for indexing. Alias for wmsearch:queue
drush queue:run wmsearch.index  # Index queued content
drush wmsc                      # Create a new index. Alias for wmsearch-index-create
drush wmsri                     # Rename an existing index. Alias for wmsearch-reindex

Config

# The elastic endpoint uri
wmsearch.elastic.endpoint: 'http://localhost:9200'

# Name of the index
wmsearch.elastic.index: 'mysite-staging'

# Serve a quick'n dirty site search on /simple-search
wmsearch.simple_search: false

# Default JSON endpoint
wmsearch.json.path: '/search/json'

# Formatter class for the JSON endpoint
wmsearch.json.formatter.class: 'Drupal\wmsearch\Service\ResultFormatter'

# Query provider for the JSON endpoint
wmsearch.json.query_builder.class: 'Drupal\wmsearch\Service\QueryBuilder'

License

GPL, (*14)

Examples

ElasticEntityInterface

This assumes wieni/wmmodel or something similar., (*15)

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

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

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

EntityDocumentCollectionInterface

# mymodule.services.yml
services:
    mymodule.elastic.article.collection:
        class: Drupal\mymodule\Service\Elastic\Collection\ArticleCollection
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(),
            // ...
        ];
    }
}

Programmatically index

$article = $nodeStorage->load(123);
$api->addDoc($dish);

Query

Search, (*16)

$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));

Completion, (*17)

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

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

TODO

  • DocumentInterface examples
  • wmmodel implementation + reindex

The Versions

19/07 2018

dev-master

9999999-dev

Elasticsearch api

  Sources   Download

GPLv3 GPL-3.0-only

The Requires

 

by Avatar Wieni

19/07 2018

0.5.3

0.5.3.0

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

26/06 2018

0.5.2

0.5.2.0

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

09/04 2018

0.5.1

0.5.1.0

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

20/03 2018

0.5.0

0.5.0.0

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

19/03 2018

dev-feature/0.4/drush

dev-feature/0.4/drush

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

13/02 2018

0.4.4

0.4.4.0

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

12/02 2018

0.4.3

0.4.3.0

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

12/02 2018

dev-feature/0.4/multiword-queries

dev-feature/0.4/multiword-queries

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

09/02 2018

0.4.2

0.4.2.0

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

09/02 2018

dev-issue/0.4/store_date_as_epoch

dev-issue/0.4/store_date_as_epoch

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

31/01 2018

0.4.1

0.4.1.0

Elasticsearch api

  Sources   Download

GPL-3.0-only

The Requires

 

by Avatar Wieni

11/11 2017

0.4.0

0.4.0.0

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

10/11 2017

0.3.1

0.3.1.0

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

11/09 2017

0.3.0

0.3.0.0

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

05/09 2017

dev-feature/v1/cross-type-queries

dev-feature/v1/cross-type-queries

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

21/08 2017

0.2.2

0.2.2.0

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

21/08 2017

dev-feature/v1/aggregation

dev-feature/v1/aggregation

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

18/08 2017

dev-feature/v1/mapping-event-with-index-name

dev-feature/v1/mapping-event-with-index-name

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

28/06 2017

0.2.1

0.2.1.0

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

07/06 2017

0.2.0

0.2.0.0

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

10/05 2017

0.1.2

0.1.2.0

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

10/05 2017

dev-feature/v0/decay

dev-feature/v0/decay

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

09/05 2017

dev-feature/v0/highlight-full-sentences

dev-feature/v0/highlight-full-sentences

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

08/05 2017

0.1.1

0.1.1.0

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni

05/05 2017

0.1.0

0.1.0.0

Elasticsearch api

  Sources   Download

GPLv3

The Requires

 

by Avatar Wieni