opensearch-project / opensearch-php

Official PHP Client for OpenSearch
Other
109 stars 58 forks source link

[FEATURE] Ignore unmapped or missing fields in sort #125

Open Dgame opened 1 year ago

Dgame commented 1 year ago

Is your feature request related to a problem?

I would like to ignore unmapped or missing field, which is currently not possible (at least not according to the documentation)

What solution would you like?

Allow to ignore missing / unmapped fields

What alternatives have you considered?

There is currently no alternative

Do you have any additional context?

The documentation states that the sort works like this

  • $params['sort'] = (list) A comma-separated list of : pairs

But there is currently not way to express something like this:

"sort" : [
       { "rating": {"order" : "desc" , "ignore_unmapped" : true} },
       { "price": {"order" : "asc" , "missing" : "_last" , "ignore_unmapped" : true} }
]

missing and ignore_unmapped are not possible according to the documentation.

shyim commented 1 year ago

Hey,

I am not sure if this is an SDK problem. For me it sounds like a general OpenSearch issue than an SDK here. Is that right?

Dgame commented 1 year ago

No, I guess the problem is that the sort parameter (like everything else except the body) is send in the query. If sort is present, it should be sent in the body too.

shyim commented 1 year ago

Ahh okay, can you show a small example in PHP how you do it?

Dgame commented 1 year ago

I'm using the client with an array like in your tests. 🙂

$client = OpenSearch\ClientBuilder::create()->setHosts(["localhost:1"])->setRetries(0)->build();
$searchParams = [
      'index' => 'test',
      "sort" =>"rating:desc,price:asc",
      'body' => [
          'query' => [
              'match_all' => []
          ]
      ]
];
$client->search($searchParams);

But what I would like to do is:

$client = OpenSearch\ClientBuilder::create()->setHosts(["localhost:1"])->setRetries(0)->build();
$searchParams = [
      'index' => 'test',
      "sort" => [
             [ "rating" => ["order" => "desc" , "ignore_unmapped" => true] ],
             [ "price" => ["order" => "asc" , "missing" => "_last" , "ignore_unmapped" => true] ]
      ],
      'body' => [
          'query' => [
              'match_all' => []
          ]
      ]
];
$client->search($searchParams);
harshavamsi commented 1 year ago

@Dgame I think this is a fair request, let me see if OpenSearch supports these kinds of sort parameters and i'll update this issue.

shyim commented 1 year ago

@Dgame You should be able to move the sort into body and then it should work as expected. The sort in the first level is the query string based sorting which does not support that