drupal-graphql / graphql-search-api

GraphQL & Drupal Search API integration.
10 stars 22 forks source link

Sort the result set by relevance #51

Closed only4kaustav closed 2 years ago

only4kaustav commented 2 years ago

Hello,

We want to sort the results by relevance but not able to do so. We tried with different field names like "relevance", "score", "_score", "search_api_relevance" but every time getting error response. Can any one let me know what should be the field name here to use?

Thanks in advance.

kwiechmann commented 2 years ago

Been working the same issue with @only4kaustav.

GraphQL is returning:

{
  "errors": [
    {
      "message": "Internal server error",
      "category": "internal",
      "locations": [{"line": 8," column": 3}],
      "path": ["searchAPISearch"]
    }
  ],
  "data": {
    "searchAPISearch": null
  }
}

Watchdog actually provides a more specific error:

An error occurred while trying to search with Solr: {"error":@"msg":"can not use FieldCache on a field which is neither indexed nor has doc values: boost_document","code":400} `

graphql: 8.x-3.1 graphql_search_api: 8.x-1.2 search_api: 8.x-1.2 search_api_solr: 4.2.7 Solr: 4.5.1

duartegarin commented 2 years ago

Hi folks, I didn't specifically support this use case so it's likely it doesn't work and needs to be added via a PR. But, it shouldn't be too hard. Under the hood all this module is doing is using Search API and in this case passing whatever the sort parameter is, like so:

// Adding sort parameters to the query.
    if ($args['sort']) {
      foreach ($args['sort'] as $sort) {
        $this->query->sort($sort['field'], $sort['value']);
      }
    }

Do you know what parameter should be passed here for relevance? In fact from what I can see you can maybe get away without any changes to code at all. See this: https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code

Passing a sort with relevance seems to be done like so:

// Add sorting.
$query->sort('search_api_relevance', 'DESC');

In which case all you'd need to do is add that to the GraphQL query like this:

sort:{field:"search_api_relevance", value:"desc"}

Let me know if that helps!

only4kaustav commented 2 years ago

Hi @duartegarin,

Thanks for your comment.

As I said we already tried different field names like "relevance", "score", "_score", "search_api_relevance" with the GraphQL sort format you provided, but every time we are getting error.

The same format is working fine with any indexed field without any issue.

duartegarin commented 2 years ago

Hi @only4kaustav , I'm not getting any error with sort:{field:"search_api_relevance", value:"desc"} Is the error the same on all the fields?

only4kaustav commented 2 years ago

Hi @duartegarin,

As specified in https://github.com/drupal-graphql/graphql-search-api/issues/51#issuecomment-1061763509, we are getting An error occurred while trying to search with Solr: {"error":@"msg":"can not use FieldCache on a field which is neither indexed nor has doc values: boost_document","code":400} in drupal watchdog while using search_api_relevance as field name.

All indexed field working fine in sort.

duartegarin commented 2 years ago

Can't seem to replicate this. Can you post the entire query please?

duartegarin commented 2 years ago

I suspect this might not be related to GraphQL Search API: https://www.drupal.org/project/search_api_solr/issues/3056971

only4kaustav commented 2 years ago

Can't seem to replicate this. Can you post the entire query please?

Please find the query below:

query ($key: [String]!, $offset: Int!, $limit: Int!, $filters: ConditionGroupInput, $sort: SortInput) {
  searchAPISearch(index_id: "myindex", condition_group: $filters, range: {offset: $offset, limit: $limit}, fulltext: {keys: $key}, facets: [{field: "content__content_type", limit: 0, operator: "=", min_count: 0, missing: true}], sort:[$sort]) {
    count: result_count
    facets {
      name
      values {
        count
        filter
      }
    }
    documents {
      ... on MyindexDoc {
        url
        relevance
        content__nid
        content__uuid
        content__content_type
        content__changed
        content__created
        content__content_title
      }
    }
  }
}

And the query variable: {"offset":"0","limit":50,"key":"","filters":{"conjunction":"AND","groups":[{"conjunction":"OR","conditions":[{"operator":"=","name":"content__content_type","value":"article"}]}]},"sort":{"field":"search_api_relevance","value":"desc"}}

When I execute above getting the error specified on https://github.com/drupal-graphql/graphql-search-api/issues/51#issuecomment-1062729630

But if the query variable is: {"offset":"0","limit":50,"key":"","filters":{"conjunction":"AND","groups":[{"conjunction":"OR","conditions":[{"operator":"=","name":"content__content_type","value":"article"}]}]},"sort":{"field":"content__content_title","value":"desc"}}

Then it is working fine and I am getting the relevance in result set.

duartegarin commented 2 years ago

Hi I am not being able to replicate this.

Have you looked at the issue I linked? Perhaps try to see if you can programmatically call Search API and sort by relevance, I suspect you might encounter the same issue.

only4kaustav commented 2 years ago

Hi @duartegarin, thanks for your time to looking at the issue. We able to figure out the issue. This was happening due to old config schema. I have updated the schema and now everything is working fine including relevance sort.

duartegarin commented 2 years ago

No worries, glad to help!