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

AppSearch how to use sort? #11

Closed
kamil-karkus opened this issue Feb 7, 2022 · 11 comments
Closed

AppSearch how to use sort? #11

kamil-karkus opened this issue Feb 7, 2022 · 11 comments
Labels
bug Something isn't working
Milestone

Comments

@kamil-karkus
Copy link

kamil-karkus commented Feb 7, 2022

I cannot figure out how to assing correct SimpleObject in SearchRequestParams to sort results by e.g. fieldA and then fieldB.
In previous package versions it wasn't type-hinted property so I could do something like this:
$searchRequestParams->sort = [['fieldA' => 'desc'], ['fieldB' => 'desc']];
But now it's type-hinted property so I can't do this.

Is it possible to provide OAS for enterprise search or OAS isn't flexible enough?

@ezimuel
Copy link
Contributor

ezimuel commented Feb 10, 2022

@kamil-karkus the sort parameter is now a Elastic\EnterpriseSearch\AppSearch\Schema\SimpleObject object (this is actually an empty class used only for namespace reason). This means you can pass the fieldA and fieldB as property of SimpleObject as follows:

use Elastic\EnterpriseSearch\AppSearch\Schema\SimpleObject;

$sort = new SimpleObject();
$sort->fieldA = 'desc';
$sort->fieldB = 'desc';
$searchRequestParams->sort = $sort;

@ezimuel ezimuel added the question Further information is requested label Feb 10, 2022
@kamil-karkus
Copy link
Author

kamil-karkus commented Feb 10, 2022

Already tried this and got 400 bad request, I'll provide more details later today

@ezimuel
Copy link
Contributor

ezimuel commented Feb 10, 2022

Ok, please paste the full PHP code including the endpoint usage, thanks.

@kamil-karkus
Copy link
Author

kamil-karkus commented Feb 12, 2022

@ezimuel I'm sorry for the delay.

Package version

elastic/enterprise-search            v7.14.0   Enterprise Search official PHP client

Code snippet

        $appSearch = $this->client->appSearch();

        $searchRequestParams = new SearchRequestParams();
        $searchRequestParams->query = $query;
        $searchRequestParams->precision = 8;
        $searchRequestParams->sort = [[Enum::KEY_IN_STOCK => 'desc'], [Enum::KEY_NET_PRICE => 'desc']];
        $current = max(1, $page);
        $size = min($limit, 1000);

        $pagination = new \stdClass();
        $pagination->current = $current;
        $pagination->size = $size;
        $searchRequestParams->page = $pagination;
        $response = $appSearch
            ->search(new Search(Enum::ENGINE_NAME_PRODUCTS, $searchRequestParams))
            ->asArray();

Request body

{"page":{"current":1,"size":100},"query":"long","sort":[{"in_stock":"desc"},{"net_price":"desc"}],"precision":8}

Response code 200


Package version

elastic/enterprise-search            v7.16.0   Enterprise Search official PHP client

Code snippet

         $appSearch = $this->client->appSearch();

        $searchRequestParams = new SearchRequestParams($query);
        $searchRequestParams->precision = 8;
        $sort = new SimpleObject();
        $key1 = Enum::KEY_IN_STOCK;
        $sort->$key1 = 'desc';
        $key2 = Enum::KEY_NET_PRICE;
        $sort->$key2 = 'desc';
        $searchRequestParams->sort = $sort;
        $current = max(1, $page);
        $size = min($limit, 1000);

        $pagination = new PaginationResponseObject();
        $pagination->current = $current;
        $pagination->size = $size;
        $searchRequestParams->page = $pagination;
        $response = $appSearch
            ->search(new Search(Enum::ENGINE_NAME_PRODUCTS, $searchRequestParams))
            ->asArray();

Request body (sort isn't serialized correctly)

{"query":"long","page":{"current":1,"size":100},"sort":{"in_stock":"desc","net_price":"desc"},"precision":8}

Response code 400

Response body

{"errors":["Sort contains an object with more than one key: in_stock and net_price"]}

@jdear
Copy link

jdear commented Feb 15, 2022

@kamil-karkus the sort parameter is now a Elastic\EnterpriseSearch\AppSearch\Schema\SimpleObject object (this is actually an empty class used only for namespace reason). This means you can pass the fieldA and fieldB as property of SimpleObject as follows:

use Elastic\EnterpriseSearch\AppSearch\Schema\SimpleObject;

$sort = new SimpleObject();
$sort->fieldA = 'desc';
$sort->fieldB = 'desc';
$searchRequestParams->sort = $sort;

I'm seeing the same issue as @kamil-karkus. This code works when only sorting by one field, but I get a 400 Bad Request as soon as I add more than one field as @ezimuel did in this example.

@ezimuel
Copy link
Contributor

ezimuel commented Feb 16, 2022

I found the issue, the sort parameter can be an object or an array of objects. This feature is documented as sorting on multiple fields. I will fix the issue with 7.17.0 release soon. I'm sorry for the inconvenience.

@kamil-karkus
Copy link
Author

@ezimuel I don't want to rush you but is there any news?

@ezimuel
Copy link
Contributor

ezimuel commented May 11, 2022

Fixed in v7.17.0, sorry for the delay 😰

@ezimuel ezimuel closed this as completed May 11, 2022
@ezimuel ezimuel added bug Something isn't working and removed question Further information is requested labels May 11, 2022
@ezimuel ezimuel added this to the 7.17.0 milestone May 11, 2022
@myleshyson
Copy link

myleshyson commented May 25, 2022

This is still broken for me. I'm using the dev-master branch and App Search 8.2 but still getting this error.

{
  "errors": [
    "Sort contains an object with more than one key: title and title2"
  ]
}

here's a basic test I did.

        $params = new SearchRequestParams('hey');
        $params->sort = new SimpleObject();
        $params->sort->title = 'asc';
        $params->sort->title2 = 'asc';
        $response = $this->client->search(new Search('local-elements', $params));  

not sure if it makes sense for sort to be typed as a SimpleObject since the request can accept an array of objects. I think this can be fixed by just typing it as an array as well.

To be on the safe side all the SimpleObject types should probably just be typed as SimpleObject|array, if that property supports both structures.

@myleshyson
Copy link

ah you know what, looks like master is out of sync with the changes made to 7.17 to fix this. Any chance those can get merged in to the master branch?

@ezimuel
Copy link
Contributor

ezimuel commented May 26, 2022

@myleshyson we did not release the enterprise-search-php for App Search 8. We are working on it and release it soon. In the meantime, you can try using 7.17.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants