wieni / wmsearch
Elasticsearch api
Installs: 12 486
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 7
Forks: 1
Open Issues: 6
Type:drupal-module
Requires
- php: >=7.0.0
- guzzlehttp/guzzle: ^6.1 || ^7.4.4
Conflicts
- drupal/core: <8.8
- dev-main
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.0
- 0.10.9
- 0.10.8
- 0.10.7
- 0.10.6
- 0.10.5
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.16
- 0.9.15
- 0.9.14
- 0.9.13
- 0.9.12
- 0.9.11
- 0.9.10
- 0.9.9
- 0.9.8
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.7
- 0.8.6
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.0
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.2
- 0.1.1
- 0.1.0
- dev-feature/drupal-10
- dev-update-guzzle
- dev-feature/api-timeout-injection
- dev-release/v0.9
- dev-feature/elastic-6.x
This package is auto-updated.
Last update: 2024-10-16 13:49:57 UTC
README
Manages an index with a single document type page
The api allows adding different document types but only page
is 'managed'.
Elasticsearch support
API
inject wmsearch.api.index
Index
$api->createIndex();
$api->deleteIndex();
Document
$api->addDoc(ElasticEntityInterface $entity); // upsert
$api->delDoc($id);
$api->getDoc($id);
Search
$api->search(QueryInterface) : SearchResult
// Identical to ->search() but strips html from highlights
$api->highlightSearch(QueryInterface) : SearchResult
Misc
$api->health(); // simple http elastic healthcheck
$api->refresh() // reopen lucene index
$api->flush() // fsync the lucene index
JSON
GET /search/json?q=lorem%20ipsum&o=0&a=10
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
Examples
ElasticEntityInterface
This assumes wieni/wmmodel or something similar.
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
$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
$query = new PageQuery(); ->setSource('') ->complete($input, 2); $formatter->format($api->search($query));
TODO
- DocumentInterface examples
- wmmodel implementation + reindex