Skip to content

Commit

Permalink
Improve search
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Mar 12, 2024
1 parent ac62779 commit cf7c021
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/Controller/ElasticsearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Webgriffe\SyliusElasticsearchPlugin\Generator\IndexNameGeneratorInterface;
use Webgriffe\SyliusElasticsearchPlugin\Helper\SortHelperInterface;
use Webgriffe\SyliusElasticsearchPlugin\Mapper\QueryResultMapperInterface;
use Webgriffe\SyliusElasticsearchPlugin\Model\ResponseInterface;
use Webgriffe\SyliusElasticsearchPlugin\Pagerfanta\ElasticsearchSearchQueryAdapter;
use Webgriffe\SyliusElasticsearchPlugin\Pagerfanta\ElasticsearchTaxonQueryAdapter;
use Webgriffe\SyliusElasticsearchPlugin\Provider\DocumentTypeProviderInterface;
Expand Down Expand Up @@ -103,7 +104,14 @@ public function searchAction(Request $request, ?string $query = null): Response
$size,
);
// This prevents Pagerfanta from querying ES from a template
$paginator->getCurrentPageResults();
$results = $paginator->getCurrentPageResults();
if (count($results) === 1) {
$result = $results[0];
Assert::isInstanceOf($result, ResponseInterface::class);

return $this->redirectToRoute($result->getRouteName(), $result->getRouteParams());
}


return $this->render('@WebgriffeSyliusElasticsearchPlugin/Search/results.html.twig', [
'query' => $query,
Expand Down
4 changes: 2 additions & 2 deletions src/DocumentType/ProductDocumentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private function taxonProperties(): array
'sylius-id' => $this->keyword(),
'code' => $this->keyword(false),
'position' => $this->integer(false),
'name' => $this->nestedTranslationKeywords(false),
'name' => $this->nestedTranslationKeywords(),
];
}

Expand Down Expand Up @@ -360,7 +360,7 @@ private function optionValueProperties(): array
'sylius-id' => $this->keyword(false),
'code' => $this->keyword(),
'value' => $this->keyword(false),
'name' => $this->nestedTranslationKeywords(false),
'name' => $this->nestedTranslationKeywords(),
];
}

Expand Down
159 changes: 159 additions & 0 deletions templates/query/search/query.json.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
}
],
"should": [
{
"term": {
"code": "{{ searchTerm }}"
}
},
{
"nested": {
"path": "variants",
"query": {
"term": {
"variants.code": "{{ searchTerm }}"
}
}
}
},
{
"nested": {
"path": "name",
Expand All @@ -24,12 +39,76 @@
}
}
},
{
"nested": {
"path": "variants",
"query": {
"match": {
"variants.name.{{ localeCode }}": {
"query": "{{ searchTerm }}",
"boost": 9,
"fuzziness": "AUTO",
"fuzzy_transpositions": true,
"operator": "OR"
}
}
}
}
},
{
"nested": {
"path": "description",
"query": {
"match": {
"description.{{ localeCode }}": {
"query": "{{ searchTerm }}",
"boost": 8,
"fuzziness": "AUTO",
"fuzzy_transpositions": true,
"operator": "OR"
}
}
}
}
},
{
"nested": {
"path": "short-description",
"query": {
"match": {
"short-description.{{ localeCode }}": {
"query": "{{ searchTerm }}",
"boost": 7,
"fuzziness": "AUTO",
"fuzzy_transpositions": true,
"operator": "OR"
}
}
}
}
},
{
"nested": {
"path": "slug",
"query": {
"match": {
"slug.{{ localeCode }}": {
"query": "{{ searchTerm }}",
"boost": 6,
"fuzziness": "AUTO",
"fuzzy_transpositions": true,
"operator": "OR"
}
}
}
}
},
{
"nested": {
"path": "meta-description",
"query": {
"match": {
"meta-description.{{ localeCode }}": {
"query": "{{ searchTerm }}",
"boost": 5,
"fuzziness": "AUTO",
Expand All @@ -39,6 +118,86 @@
}
}
}
},
{
"nested": {
"path": "meta-keywords",
"query": {
"match": {
"meta-keywords.{{ localeCode }}": {
"query": "{{ searchTerm }}",
"boost": 4,
"fuzziness": "AUTO",
"fuzzy_transpositions": true,
"operator": "OR"
}
}
}
}
},
{
"nested": {
"path": "taxons",
"query": {
"match": {
"taxons.name.{{ localeCode }}": {
"query": "{{ searchTerm }}",
"boost": 3,
"fuzziness": "AUTO",
"fuzzy_transpositions": true,
"operator": "OR"
}
}
}
}
},
{
"nested": {
"path": "translated-attributes",
"query": {
"match": {
"translated-attributes.values.{{ localeCode }}.text-value": {
"query": "{{ searchTerm }}",
"boost": 3,
"fuzziness": "AUTO",
"fuzzy_transpositions": true,
"operator": "OR"
}
}
}
}
},
{
"nested": {
"path": "attributes",
"query": {
"match": {
"attributes.values.text-value": {
"query": "{{ searchTerm }}",
"boost": 3,
"fuzziness": "AUTO",
"fuzzy_transpositions": true,
"operator": "OR"
}
}
}
}
},
{
"nested": {
"path": "product-options",
"query": {
"match": {
"product-options.values.name.{{ localeCode }}": {
"query": "{{ searchTerm }}",
"boost": 3,
"fuzziness": "AUTO",
"fuzzy_transpositions": true,
"operator": "OR"
}
}
}
}
}
]
}
Expand Down

0 comments on commit cf7c021

Please sign in to comment.