sulu / SuluHeadlessBundle

Bundle that provides controllers and services for using Sulu as headless content management system
MIT License
45 stars 25 forks source link

Fix search with wildcards and fuzzy keywords #37

Closed ahoek closed 4 years ago

ahoek commented 4 years ago

This is a fix for issue #36

niklasnatter commented 4 years ago

Hey, first of all - thanks a lot for the contribution! Could you give some examples how you use wildcard and fuzzy keywords?

This part of the bundle is actually implemented very similar to the original Sulu search controller: https://github.com/sulu/sulu/blob/4d5543623b2c45adb2dbc3a9b95809aac651344f/src/Sulu/Bundle/SearchBundle/Controller/WebsiteSearchController.php#L79-L87

I am not sure if we should diverge from the default Sulu behaviour in the bundle. But maybe we should change this in the default Sulu search controller too?

ahoek commented 4 years ago

I haven't used the original Sulu search controller, but the issue I encountered was as follows:

If I search on the term keywor, the prepareQuery function creates the following query for the Massive search bundle (I use the Elastic adapter): +("keywor" OR "keywor*" OR "keywor~")

Because the terms "keywor*" and "keywor~" are enclosed in ", Elastic will search on the literal terms instead of using the wildcard (*) or fuzzy (~) modifiers.

When I remove the double quotes, documents which contain keyword or keywords for example will be found. +("keywor" OR keywor* OR keywor~)

niklasnatter commented 4 years ago

Thanks for the explanation! I just tested the behaviour with the original Sulu search controller and can verify the behaviour there. I think this is not intended and created a PR for the original search controller: https://github.com/sulu/sulu/pull/5407

I would like to wait and see if the Sulu PR is accepted before merging this to keep things consistent, I hope that is okay for you. You should be able to overwrite the controller inside of your project via decoration in the meantime: https://symfony.com/doc/current/bundles/override.html#controllers

Thanks again for your effort!

ahoek commented 4 years ago

Ok, that seems ok that first the functionality in Sulu itself is reviewed.

While working on this system, I noticed an additional bug. When the search query begins or ends in a space, the generated query is invalid and gives a 500 error when executed.

For example: search query: "keywor " (with a space at the end) generated query string: +("keywor" OR keywor* OR keywor~) +()

This would be solved if empty values from the exploded string would be filtered out.

alexander-schranz commented 4 years ago

@ahoek Thank you for report and fixing this!