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

When searching for a query longer then 100 chars, Tweakwise gives a 400 error back. #103

Closed
igorwulff opened this issue Sep 29, 2023 · 3 comments · Fixed by #147
Closed

Comments

@igorwulff
Copy link
Contributor

igorwulff commented Sep 29, 2023

Go to a site utilizing Tweakwise Search and place the following in the query string:
https://.../catalogsearch/result/index?q=Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit.+Phasellus+pretium+magna+ac+metus+placerat+pdasdasdasdas

An error report page comes up and something like the following is written to the system log:

Next Tweakwise\Magento2Tweakwise\Exception\ApiException: Client error: `GET https://gateway.tweakwisenavigator.net/navigation-search/` resulted in a `400 Bad Request` response:
<?xml version="1.0"?>
<error xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-i (truncated...)
 in /vendor/tweakwise/magento2-tweakwise/Model/Client.php:163
Stack trace:
#0 /vendor/guzzlehttp/promises/src/Promise.php(209): Tweakwise\Magento2Tweakwise\Model\Client->Tweakwise\Magento2Tweakwise\Model\{closure}()
#1 /vendor/guzzlehttp/promises/src/Promise.php(158): GuzzleHttp\Promise\Promise::callHandler()
#2 /vendor/guzzlehttp/promises/src/TaskQueue.php(52): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
#3 /vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(163): GuzzleHttp\Promise\TaskQueue->run()
#4 /vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(189): GuzzleHttp\Handler\CurlMultiHandler->tick()
#5 /vendor/guzzlehttp/promises/src/Promise.php(251): GuzzleHttp\Handler\CurlMultiHandler->execute()
#6 /vendor/guzzlehttp/promises/src/Promise.php(227): GuzzleHttp\Promise\Promise->invokeWaitFn()
#7 /vendor/guzzlehttp/promises/src/Promise.php(272): GuzzleHttp\Promise\Promise->waitIfPending()
#8 /vendor/guzzlehttp/promises/src/Promise.php(229): GuzzleHttp\Promise\Promise->invokeWaitList()
#9 /vendor/guzzlehttp/promises/src/Promise.php(69): GuzzleHttp\Promise\Promise->waitIfPending()
#10 /vendor/tweakwise/magento2-tweakwise/Model/Client.php(171): GuzzleHttp\Promise\Promise->wait()
#11 /vendor/tweakwise/magento2-tweakwise/Model/Client.php(286): Tweakwise\Magento2Tweakwise\Model\Client->doRequest()
#12 /vendor/magento/framework/Interception/Interceptor.php(58): Tweakwise\Magento2Tweakwise\Model\Client->request()
#13 /vendor/magento/framework/Interception/Interceptor.php(138): Tweakwise\Magento2Tweakwise\Model\Client\Interceptor->___callParent()
#14 /vendor/magento/framework/Interception/Interceptor.php(153): Tweakwise\Magento2Tweakwise\Model\Client\Interceptor->Magento\Framework\Interception\{closure}()
#15 /generated/code/Tweakwise/Magento2Tweakwise/Model/Client/Interceptor.php(23): Tweakwise\Magento2Tweakwise\Model\Client\Interceptor->___callPlugins()
#16 /vendor/tweakwise/magento2-tweakwise/Model/Catalog/Layer/NavigationContext.php(156): Tweakwise\Magento2Tweakwise\Model\Client\Interceptor->request()
#17 /vendor/tweakwise/magento2-tweakwise/Model/Observer/CatalogSearchRedirect.php(63): Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\NavigationContext->getResponse()
#18 /vendor/magento/framework/Event/Invoker/InvokerDefault.php(88): Tweakwise\Magento2Tweakwise\Model\Observer\CatalogSearchRedirect->execute()
#19 /vendor/magento/framework/Event/Invoker/InvokerDefault.php(74): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod()
#20 /vendor/magento/module-staging/Model/Event/Manager.php(97): Magento\Framework\Event\Invoker\InvokerDefault->dispatch()
#21 /generated/code/Magento/Staging/Model/Event/Manager/Proxy.php(95): Magento\Staging\Model\Event\Manager->dispatch()
#22 /vendor/magento/framework/App/FrontController.php(279): Magento\Staging\Model\Event\Manager\Proxy->dispatch()
#23 /vendor/magento/framework/App/FrontController.php(211): Magento\Framework\App\FrontController->dispatchPreDispatchEvents()
#24 /vendor/magento/framework/App/FrontController.php(147): Magento\Framework\App\FrontController->processRequest()
#25 /vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch()
#26 /vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent()
#27 /vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(99): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}()
#30 /vendor/fastly/magento2/Model/FrontControllerPlugin.php(135): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}()
#31 /vendor/magento/framework/Interception/Interceptor.php(135): Fastly\Cdn\Model\FrontControllerPlugin->aroundDispatch()
#32 /vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(71): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}()
#33 /vendor/magento/framework/Interception/Interceptor.php(135): Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch()
#34 /vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}()
#35 /generated/code/Magento/Framework/App/FrontController/Interceptor.php(23): Magento\Framework\App\FrontController\Interceptor->___callPlugins()
#36 /vendor/magento/framework/App/Http.php(116): Magento\Framework\App\FrontController\Interceptor->dispatch()
#37 /vendor/magento/framework/App/Bootstrap.php(264): Magento\Framework\App\Http->launch()
#38 /pub/index.php(30): Magento\Framework\App\Bootstrap->run()

I've looked into setting the max query length to 100. And this does resolve direct input through the input field and "fixes" the issue: catalog/search/max_query_length

However direct input through the url still causes an error to be thrown. This could be fixed by also applying this max_query_length to the Tweakwise module:

Index: vendor/tweakwise/magento2-tweakwise/Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/vendor/tweakwise/magento2-tweakwise/Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php b/vendor/tweakwise/magento2-tweakwise/Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php
--- a/vendor/tweakwise/magento2-tweakwise/Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php	
+++ b/vendor/tweakwise/magento2-tweakwise/Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php	(date 1695988662625)
@@ -21,6 +21,7 @@
 use Magento\Catalog\Model\Category;
 use Magento\Framework\App\Request\Http as MagentoHttpRequest;
 use Magento\Framework\Stdlib\CookieManagerInterface;
+use Magento\Search\Helper\Data;
 
 class QueryParameterStrategy implements UrlInterface, FilterApplierInterface, CategoryUrlInterface
 {
@@ -93,7 +94,8 @@
         UrlModel $url,
         StrategyHelper $strategyHelper,
         CookieManagerInterface $cookieManager,
-        TweakwiseConfig $config
+        TweakwiseConfig $config,
+        private Data $searchConfig,
     ) {
         $this->url = $url;
         $this->strategyHelper = $strategyHelper;
@@ -450,7 +452,13 @@
      */
     protected function getSearch(MagentoHttpRequest $request)
     {
-        return $request->getQuery(self::PARAM_SEARCH);
+        $search = $request->getQuery(self::PARAM_SEARCH);
+
+        if ($maxQueryLength = $this->searchConfig->getMaxQueryLength()) {
+            $search = substr((string) $search, 0, $maxQueryLength);
+        }
+
+        return $search;
     }
 
     /**

This would mean that this is an configuration detail to be followed. An alternative option is to enforce this directly.

I'm also curious if this can't be resolved in Tweakwise. I understand a limit is used, but perhaps it could be handled more gracefully.

@ah-net
Copy link
Collaborator

ah-net commented Oct 18, 2023

@igorwulff Ithink this should be fixed with setting the query length on the magento side. I suspect that the api returns an 400 error because it doesn't want to change the request without letting the user know. But i'll discuss this internally and see what the reason is the api returns an 400 error.

@onepack
Copy link

onepack commented Oct 29, 2023

This is what we get:

[2023-10-29T21:06:11.465342+00:00] report.ERROR: Client error: GET https://gateway.tweakwisenavigator.net/navigation-search/26af8119?tn_ps=32&tn_parameters=visibility%3D3%26visibility%3D4&tn_ft=190&tn_q=enablink+240+W+USB+C+kabel+PD+3.1+5A+snellaadkabel+QC+4.0+naar+nylon+oplaadkabel+compatibel+met+MacBook+Pro+Air+iPad+Samsung+S22%2FS21%2FS20+Huawei+tablet+laptop&tn_cid=100012 resulted in a 400 Bad Request response:

<error xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-i (truncated...)
[] []
[2023-10-29T21:06:11.858366+00:00] report.CRITICAL: Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"query_shard_exception","reason":"No mapping found for [position_asc] in order to sort on","index_uuid":"BUritSOyTkW_Vc5Oi7Ja4A","index":"magento2_product_1_v25"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"magento2_product_1_v25","node":"2voWvyLqRO-9JasoZgc_kQ","reason":{"type":"query_shard_exception","reason":"No mapping found for [position_asc] in order to sort on","index_uuid":"BUritSOyTkW_Vc5Oi7Ja4A","index":"magento2_product_1_v25"}}]},"status":400} in /dir/dirname/public_html/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:693
Stack trace:
#0 /dir/dirname/public_html/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(333): Elasticsearch\Connections\Connection->process4xxError()
#1 /dir/dirname/public_html/vendor/react/promise/src/FulfilledPromise.php(28): Elasticsearch\Connections\Connection->Elasticsearch\Connections{closure}()
#2 /dir/dirname/public_html/vendor/ezimuel/ringphp/src/Future/CompletedFutureValue.php(65): React\Promise\FulfilledPromise->then()

@ah-net
Copy link
Collaborator

ah-net commented Nov 16, 2023

@onepack @igorwulff I've discussed this internally and were not changing this on the api side. But I'll make an change and restrict the request length on the magento side. I've added this to our backlog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants