nzo/elastic-query-bundle

用于基于简单查询语言执行Elasticsearch系统搜索的Symfony包

安装: 767

依赖者: 0

建议者: 0

安全: 0

星标: 2

关注者: 1

分支: 0

开放问题: 1

类型:symfony-bundle

v3.1.1 2021-12-01 09:09 UTC

README

Build Status Latest Stable Version

版本及依赖

包含功能
  • 搜索: matchnotmatchisnullinnotingtegtlteltrangewildcard
  • 排序
  • 限制
  • 分页
此包与 Symfony >= 4.4 兼容

安装

安装包

$ composer require nzo/elastic-query-bundle

在 config/bundles.php 中注册包(不使用Flex)

// config/bundles.php

return [
    // ...
    Nzo\ElasticQueryBundle\ElasticQueryBundle::class => ['all' => true],
];

配置包

# config/packages/nzo_elastic_query.yaml

nzo_elastic_query:
    elastic_index_prefix:  # optional (the index prefix)
    default_page_number:   # optional (default 1)
    limit_per_page:        # optional (default 100)
    items_max_limit:       # optional (default 1000)
    show_score:            # optional (default false)
    

使用

use Nzo\ElasticQueryBundle\Query\ElasticQuerySearch;

class MyClass
{
    /**
     * @var ElasticQuerySearch
     */
    private $elasticQuerySearch;
    
    public function __construct(ElasticQuerySearch $elasticQuerySearch)
    {
        $this->elasticQuerySearch = $elasticQuerySearch;
    }
    
    /**
     * @param string|array $query  (json or array)
     * @param string $entityNamespace The FQCN (fully qualified class name) of the entity to execute the search on.
     * @param null|int $page
     * @param null|int $limit
     * @return array
     */
    public funtion foo($query, $entityNamespace, $page = null, $limit = null)
    {
        // $entityNamespace === 'App\Entity\FooBar'
        
        return $this->elasticQuerySearch->search($query, $entityNamespace, $page, $limit);
        
        // check access permission on the search
        return $this->elasticQuerySearch->search(
            $query,
            $entityNamespace,
            $page,
            $limit,
            ['role' => 'ROLE_SEARCH', 'message' => 'Search not authorized'] // 'message' is optional
        );
    }
}

show_score 启用时,必须将字段 _scoreElastic 添加到所需的实体中,并添加字段 gettersetter(getScoreElastic & setScoreElastic)

    private float $_scoreElastic;

    public function getScoreElastic(): float
    {
        return $this->_scoreElastic;
    }

    public function setScoreElastic(float $scoreElastic): self
    {
        $this->_scoreElastic = $scoreElastic;

        return $this;
    }

配置索引

fos_elastica:
    indexes:
        user:
            properties:
                id:
                    type: keyword
                    index: true
                createdAt:
                    type:   date
            persistence:
                driver: orm
                model: App\Entity\User
                provider: ~
                finder: ~
                repository: Nzo\ElasticQueryBundle\Repository\SearchRepository

填充索引

$ bin/console fos:elastica:populate

有效载荷示例

POST http://example.fr/search/myEnitity?page=1&limit=2

{
    "query": {
        "search": {
            "or": [
                {
                    "field": "status",
                    "match": "foo"
                },
                {
                    "field": "entity.title",
                    "notmatch": "bar"
                },
                {
                    "field": "entity.title",
                    "wildcard": "*toto*"
                },
                {
                    "and": [
                        {
                            "field": "lastname",
                            "notin": [
                                "titi",
                                "tata"
                            ]
                        },
                        {
                            "or": [
                                {
                                    "field": "age",
                                    "range": [
                                        20,
                                        30
                                    ]
                                },
                                {
                                    "field": "parent.age",
                                    "gte": 25
                                },
                                {
                                    "and": [
                                        {
                                            // This check is needed to make sure the 'parent' is not Null
                                            "field": "parent.id",
                                            "isnull": false
                                        },
                                        {
                                            "field": "parent.text",
                                            "isnull": true
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "sort": [
            {
                "field": "createdAt",
                "order": "ASC"
            }
        ]
    }
}

许可证

此包采用MIT许可证。请参阅包中的完整许可证

查看 LICENSE