Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve grouped results #6

Open
keichinger opened this issue Oct 12, 2017 · 0 comments
Open

Retrieve grouped results #6

keichinger opened this issue Oct 12, 2017 · 0 comments

Comments

@keichinger
Copy link
Contributor

The problem

Given the following data structure:

use Becklyn\SearchBundle\Mapping as Search;

@Search\Item()
class A {}

@Search\Item()
class B extends A {}

@Search\Item()
class C extends A {}

@Search\Item()
class D extends B {}

To retrieve all search results one would have the following options:

$searchResults = $searchClient->search($query, $language);

// Option a: Work with the Entity Results Lists
$allResults = $searchResults->getEntityResultLists();

// Option b: Retrieve each individual result
$aResults = $searchResult->getResult(A::class);
$bResults = $searchResult->getResult(B::class);
$cResults = $searchResult->getResult(C::class);
$dResults = $searchResult->getResult(D::class);

The downside for the a approach is that you have to do the grouping yourself. Passing down the entity results list directly into the template can also get quite messy real fast since you need to have (probably multiple) places where you keep track of your hierarchy and which you probably want grouped together.

Approach b is more explicit and therefore better per se, though the pain points start when you add a new class E extends A and forget to add it to the various places where you're explicitly retrieving results for the given data structure.

The proposals

Automatic data hierarchy analysis

One potential solution here would be to have a look at the entity's data hierarchy while indexing so one could easily do $allAResults = $searchResults->getResult(A::class) and retrieve results for A, B, C, D and in the future even E.

While this proposal may sound like a good and intuitive way to solve the problem, this would mean that indexing and outputting the results gets even more expensive and than it already is since we would have to search through the entity's hierarchy and then saving it alongside its metadata.

Tagging

The more promising solution here would be allowing the developer to logically group entities together by extending the @Search\Item to contain an $tags-Array.

The above data structure could then look like this:

@Search\Item("tags"={"a"})
class A {}

@Search\Item("tags"={"a", "b"})
class B extends A {}

@Search\Item("tags"={"a"})
class C extends A {}

@Search\Item("tags"={"a", "b"})
class D extends B {}

The SearchResult class would then need a new method like getTaggedResults(string) to fetch such results:

$allAResults = $searchResults->getTaggedResults("a");

This would return me all relevant a-tagged Entities. This makes using the search bundle even future proof as I can easily add new classes with the same tag and my existing logic would automatically pick them up and return their results without me having to do anything:

@Search\Item("tags"={"a"})
class X {}

@Search\Item("tags"={"b"})
class Y {}

@Search\Item("tags"={"c"})
class Z {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant