contao / search

Contao Search Library
MIT License
0 stars 0 forks source link

Generalized search interfaces #1

Open ausi opened 3 years ago

ausi commented 3 years ago

To make it possible to share code and don’t have to do our work for every CMS again and again, I think it would best If we could agree on a set of interfaces for the search.

The frontends for every CMS could then be based on those interfaces which would make it possible to share search backends across different projects.

For Contao we would create a MySQL backend for the search as this is our main use case.

A first draft of interfaces I have in mind (these are just a starting point to get the discussion going):

interface SearchInterface
{
    public function query(string $query, SearchOptionsInterface $options): SearchResultCollectionInterface;

    public function index(string $identifier, array $texts, ?MetaDataInterface $metadata = null): void;

    public function getFromIndex(string $identifier): SearchResultInterface;

    public function removeFromIndex(string $identifier): void;
}
interface SearchResultCollectionInterface extends \Iterator, \Countable
{
    public function current(): SearchResultInterface;

    public function getQuery(): string;
}
interface SearchResultInterface
{
    public function getIdentifier(): string;

    public function getMetaData(): ?MetaDataInterface;

    /**
     * @return array<string>
     */
    public function getMatches(): array;

    /**
     * @return array<string>
     */
    public function getTexts(): array;

    public function getContext(int $length): string;

    /**
     * @return array<array<string>>
     */
    public function getTextsWithMatches(): array;

    /**
     * @return array<string>
     */
    public function getContextWithMatches(int $length): array;
}
interface MetaDataInterface
{
    public function serialize(): string;

    public static function fromSerialized(string $serialized): self;
}
interface SearchOptionsInterface
{
    // ...
}

Instead of array $texts we should probably create a DocumentInterface and for string $query a SearchQueryInterface?

leofeyer commented 3 years ago

+1 for DocumentInterface. Not sure about the SearchQueryInterface though; isn't the query always a string?

leofeyer commented 3 years ago

What is the difference between getTextsWithMatches() and getContextWithMatches()?

ausi commented 3 years ago

What is the difference between getTextsWithMatches() and getContextWithMatches()?

getTextsWithMatches() would return the whole document text(s) where all matching words are marked. getContextWithMatches() would only return a substring of speficied length relevant to the query.

But If we use a DocumentInterface getTexts() and getTextsWithMatches() would probably be combined to a single method getDocument().