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

feat(catalog) : improve category or product page resolution #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions services/CatalogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseServiceElastic } from './BaseServiceElastic'
export class CatalogService extends BaseServiceElastic {
async search(body: any): Promise<CatalogResult> {
body = {
...body || {},
...(body || {}),
collapse: {
field: 'url_key',
inner_hits: [
Expand Down Expand Up @@ -33,20 +33,23 @@ export class CatalogService extends BaseServiceElastic {
return { hits, total, aggregations, rawsHits }
}

getByURLKey(urlKey: string, sku: string | null): Promise<CatalogResult> {
const bool: any = {
should: [
{
/**
* Get product or category by URL key or redirect URL key
*/
getByURLKey(urls: string | string[], sku: string | null): Promise<CatalogResult> {
urls = Array.isArray(urls) ? urls : [urls]
const bool: any = { should: [] }
let boost = urls?.length
for (const field of ['url_key', 'redirect_url_key']) {
for (const url of urls) {
bool.should.push({
terms: {
url_key: [urlKey]
[field]: [url],
boost
}
},
{
terms: {
redirect_url_key: [urlKey]
}
}
]
})
}
boost--
}
let query: any = { bool }
if (sku) {
Expand All @@ -68,10 +71,26 @@ export class CatalogService extends BaseServiceElastic {
}

async getEntityByURLKey(urlKey: string, sku: string | null): Promise<Product | Category | null> {
const result = await this.getByURLKey(urlKey, sku)
return result?.hits?.[0] || null
const urls = [urlKey]
// Clean URL (remove end slash and index.html) to get the right entity
const cleanedURL = this.cleanURL(urlKey)
if (cleanedURL !== urlKey) {
urls.push(cleanedURL)
}

const result = await this.getByURLKey(urls, sku)
const entity = result?.hits?.[0] || null
if (entity && cleanedURL !== urlKey) {
// Add URL to redirectUrlKey to redirect to the cleaned URL
entity.redirectUrlKey.push(urlKey)
}
return entity
}

/** Remove URL end slash and index.html and index.htm page name */
cleanURL(url: string): string {
return url.replace(/\/(index\.html?|)\/?$/, '')
}
find(field: string, value: string[] | number[]): Promise<CatalogResult> {
const terms: any = {}
terms[field] = value
Expand Down