algoliasearch-client-javascript icon indicating copy to clipboard operation
algoliasearch-client-javascript copied to clipboard

SDK API, Issue of Default sort by object id instead sort by facet display : count

Open suraz37 opened this issue 3 years ago • 3 comments

Algolia APP ID : T3TPSRKPQT. [UAT] algolia sdk function search

readonly search:

<TObject>(queries: readonly MultipleQueriesQuery[], requestOptions?: RequestOptions & MultipleQueriesOptions) => Readonly<Promise<MultipleQueriesResponse<TObject>>>
[
    {
        "params": {
            "facets": [
                "*"
            ],
            "facetFilters": [],
            "query": "adidas",
            "hitsPerPage": 10,
            "maxValuesPerFacet": 20,
            "clickAnalytics": true,
            "page": 1
        },
        "indexName": "uat_mdc_cds_th_products"
    }
]

in response we have params

 "params": "facets=%5B%22*%22%5D&facetFilters=%5B%5D&query=adidas&hitsPerPage=10&maxValuesPerFacet=20&clickAnalytics=true&page=1&facetFilters=%5B%22brand_name_tokenized%3Aadidas%22%5D",
            "index": "uat_mdc_cds_th_products",

and i just get the response to call direct algolia

url: https://t3tpsrkpqt-dsn.algolia.net/1/indexes/*/queries call direct

{
    "requests": [
        {
            "indexName": "uat_mdc_cds_th_products",
            "params": "facets=%5B%22*%22%5D&facetFilters=%5B%5D&query=adidas&hitsPerPage=10&maxValuesPerFacet=20&clickAnalytics=true&page=1&facetFilters=%5B%22brand_name_tokenized%3Aadidas%22%5D"
        }
    ]
}

but response it different, [ ON SDK, Default sort by object id instead from facet display : count]

Example of response :

From SDK response : correct sort by count

 "category_ids": {
14          "3052": 332,
15          "3053": 982,
16          "3054": 264,
17          "3062": 1318,
18          "3565": 180,
19          "4040": 425,
20          "4041": 207,
21          "4047": 197,
22          "4062": 300,
23          "5634": 631,
24          "5957": 359,
25          "5958": 336,
26          "5965": 177,
27          "21614": 721,
28          "25594": 387,
29          "26686": 167,
30          "1838140": 395,
31          "2782732": 171,
32          "3131223": 568,
33          "3132225": 568
34        },

from post main : correct sort by count

"category_ids": {
17          "3062": 1318,
18          "3053": 982,
19          "21614": 721,
20          "5634": 631,
21          "3131223": 568,
22          "3132225": 568,
23          "4040": 425,
24          "1838140": 395,
25          "25594": 387,
26          "5957": 359,
27          "5958": 336,
28          "3052": 332,
29          "4062": 300,
30          "3054": 264,
31          "4041": 207,
32          "4047": 197,
33          "3565": 180,
34          "5965": 177,
35          "2782732": 171,
36          "26686": 167

Expected Result :

From SDK response : should sort by count

suraz37 avatar Feb 07 '22 08:02 suraz37

Facet display is used to sort the facet values again on the frontend, it doesn't affect how the facets get returned by the engine, as an object doesn't technically have an ordering. You can use sortFacetValuesBy to control the ordering of items in the facets object before they're displayed.

Our InstantSearch and algoliasearch-helper libraries do this sorting based on facet ordering out of the box

Haroenv avatar Feb 07 '22 08:02 Haroenv

we need to handle sort of facet values from facet display setting on Algolia so we couldn't use function to control the ordering .

Currently SKU doesn't return data ordering as set on config Facet Ordering .

suraz37 avatar Feb 07 '22 10:02 suraz37

As said, facet ordering has no impact on the facets result. It gets returned in the response and is intended to sort those values in the frontend. You can see more info here: https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/facet-display/react/#building-the-ui-without-instantsearch

Haroenv avatar Feb 07 '22 10:02 Haroenv