wieni/wmsearch

Elasticsearch API

维护者

详细信息

github.com/wieni/wmsearch

源代码

问题

安装数: 12,158

依赖: 0

建议者: 0

安全性: 0

星标: 0

关注者: 7

分支: 1

开放问题: 6

类型:drupal-module

0.12.3 2024-03-16 12:11 UTC

README

管理单个文档类型 page 的索引

API 允许添加不同类型的文档,但只有 page 被管理。

Elasticsearch 支持

API

注入 wmsearch.api.index

索引

$api->createIndex();

$api->deleteIndex();

文档

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

$api->delDoc($id);

$api->getDoc($id);

搜索

$api->search(QueryInterface) : SearchResult

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

其他

$api->health(); // 简单的 http elastic 健康检查

$api->refresh() // 重新打开 lucene 索引

$api->flush() // fsync lucene 索引

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

配置

# The elastic endpoint uri
wmsearch.elastic.endpoint: 'https://: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'

许可证

GPL

示例

ElasticEntityInterface

这假设 wieni/wmmodel 或类似的东西。

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(),
            // ...
        ];
    }
}

程序化索引

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

查询

搜索

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

待办事项

  • DocumentInterface 示例
  • wmmodel 实现 + 重新索引