lmc/cqrs-solr

A library containing base implementations to help with Solr Queries and Commands

3.1.0 2024-03-06 13:23 UTC

This package is auto-updated.

Last update: 2024-12-06 15:05:29 UTC


README

cqrs-types Latest Stable Version Tests and linting Coverage Status

A library containing base implementations to help with Solr Queries and Commands. This library is an extension for CQRS/Bundle and adds support for Solarium requests.

Table of contents

Installation

composer require lmc/cqrs-solr

NOTE: It also requires Solarium and Solarium bundle, which are directly required by this library.

Query

Query is a request which fetch a data without changing anything. See more here

It is allowed and recommended to use a InjectSolrClientInterface with a Solr queries, so you don't need to worry about a Solarium Client, you will simply get it automatically.

AbstractSolrQuery

A base Solr Query, it abstracts and predefine some of a most used features with this kind of a Query.

It implements a ProfileableInterface and CacheableInterface features.

AbstractSolrSelectQuery

A base Solr Select Query, it abstracts and predefine some of a most used features with this kind of a Query.

It extends a base AbstractSolrQuery and predefine some abstract methods. It also adds InjectSolrClientInterface feature, since it needs a Solarium Client to create a Select Request.

BuilderPrototypeQuery

This is a special type of a SolrSelectQuery it also extends an AbstractSolrSelectQuery and implement prepareSelect method with a usage of QueryBuilder applicators.

It is a Query, which QueryBuilder creates for you.

Query Handlers

It is responsible for handling a specific Query request and passing a result into OnSuccess callback. See more here.

Solr Query Handler

This handler supports Lmc\Cqrs\Solr\ValueObject\SolrRequest (see SolrRequest) and handles it into Solarium\Core\Query\Result\ResultInterface.

It also prepares a Query implementing a InjectSolrClientInterface by injecting a Solarium Client into a Query so it is not required for you to inject a Solarium Client into a query by yourself.

Value Objects

SolrField

It is a representation of any data passed by user to Solr (eg. in query or a list of fields returned by Solr)

SolrRequest

It is a simple Value object containing an Abstract Solarium Query and optionally a Solr endpoint.

Query Builder

Query builder is an abstraction above a Solarium Select Query set up.

The idea is that you have just a data, used in select, stored in an entity. The entity stands for what you want to select and how. And according to interfaces, that this entity implements, the data is passed into a Select Query.

Query Builder builds a BuilderPrototypeQuery which is an instance of QueryInterface and is useable in CQRS/QueryFetcher with all supported features.

Example

Imagine you need to select 30 Persons with fields Name and Age by search input, stored in a Solr, you would have something like:

$searchInput = $_GET['search'];

$selectPersons = $client->createSelect();
$selectPersons->getEDisMax()->setQueryFields('name^100 age^50');
$selectPersons->setQuery($searchInput);
$selectPersons->setNumberOfRows(30);

$result = $client->execute($selectPersons);

With direct Solarium usage you need to create a Select Query yourself and remember all setters and stuff.

Now Query Builder offers a predefined applicators, which knows how the Select query is built and just need a data for the select query.

Example above using a Query Builder would look something like:

class PersonSearch implements FulltextInterface
{
    private string $searchInput;

    public function __construct(string $searchInput)
    {
        $this->searchInput = $searchInput;
    }

    public function getKeywords(): array
    {
        return explode(' ', $this->searchInput);
    }

    public function getNumberOfRows(): int
    {
        return 30;
    }

    public function getQueryFields(): array
    {
        return [
            'name^100',
            new SolrField('age', '', 0, 50),     // you can also use a SolrField value object, so you don't need to remember how is a prioritized value built
        ];
    }

    public function isEDisMaxEnabled(): bool
    {
        return true;
    }

    public function useEDisMaxGlobally(): bool
    {
        return true;
    }

    // Note: there are more methods, you need to implement, but we want to keep this example simple as possible. If you don't need other functionality, simply return null or empty variant from a method.
}

$searchInput = $_GET['search'];

$selectPersonsEntity = new PersonSearch($searchInput);
$selectPersonsQuery = $queryBuilder->buildQuery($selectPersonsEntity);

$result = $queryFetcher->fetchAndReturn($selectPersonsQuery);

Entity Interface

It is a definition interface for a specific feature set you want a query to have.

Note: It is not a complete set of Solr/Solarium Select features, it is just our most used features.

  • EntityInterface
    • A base interface for all features, which adds a getFields and getNumberOfRows base methods. |
  • FacetsInterface
  • FilterInterface
  • FiltersInterface
  • FulltextBigramInterface
  • FulltextBoostInterface
  • FulltextInterface
  • GroupingFacetInterface
  • GroupingInterface
  • ParameterizedInterface
  • SortInterface
  • StatsInterface

Applicators

Applicator is a service which can apply a specific set of data into a Solarium select query based on implemented Interface. It must implement a ApplicatorInterface.

You can implement your own applicator if you want to mix features or simply use a feature, which does not have an applicator yet.

ApplicatorInterface

It is an interface, which all applicators must implement. It specifies which Entity is current applicator supporting and it can apply its data into Solarium request. It should be able to skip setting a value, if it is empty.

Applicator Factory

It is a service with all defined applicators, its purpose is to return all applicators supporting a given Entity. It is used inside a QueryBuilder to get a list of applicators, which needs to be applied on Select Query.

List of all applicators

  • EntityApplicator
  • FacetsApplicator
  • FilterApplicator
  • FiltersApplicator
  • FulltextApplicator
  • FulltextBigramApplicator
  • FulltextBoostApplicator
  • GroupingApplicator
  • GroupingFacetApplicator
  • ParameterizedApplicator
  • SortApplicator
  • StatsApplicator