Skip to content

Conversation

@rhoerr
Copy link

@rhoerr rhoerr commented Sep 17, 2025

Problem

I noticed on some sites that category search results wouldn't work, returning the same generic categories every time.

I found this was caused by the category search running with query product rather than the actual search query, because the query structure differed from this code that should replace it with the real query:

        if (isset($query['body']['query']['bool']['must']['bool']['should'])) {
            foreach ($query['body']['query']['bool']['must']['bool']['should'] as &$should) {
                $should['multi_match']['query'] = $searchString;
            }
        }

Solution

For better and broader support of Elasticsearch/Opensearch versions:

  • I changed the placeholder to a more generic and identifiable value, stored in a const
  • I changed the value search and replacement to happen against the query JSON, via PHP str_replace, rather than looking at specific array structure

This should still be very fast, and broadly compatible with any query structure returned by $this->requestBuilder->create(...).

        $searchRequestJson = str_replace(
            self::QUERY_PLACEHOLDER,
            $this->queryFactory->get()->getQueryText(),
            $searchRequestJson
        );

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 this pull request may close these issues.

1 participant