How do I sort of filter based on _score?

If I follow the syntax of https://swiftype.com/documentation/site-search/searching/sorting
and use

sort_field: {page: "_score"}

I get the error {sort_field: {page: ["DocumentType 'page' does not have a field '_score'."]}}

If I try to filter with filters: {page: {_score: '!1'}} as in https://swiftype.com/documentation/site-search/searching/filtering

I get the error {filters: {page: ["DocumentType 'page' does not have a field '_score'."]}}

Hey Michael ~

Thanks for posting within the Site Search community.

First of all, I have fixed the wording within the sorting docs. It was not at all clear, and I think it's the source of the confusion. I have corrected it, but I think it could still use some more substance.

Sorting, by default, will use the _score field. This is somewhat of a "hidden" field that represents the overall relevance.

To only use the score field, you can just use the sort_direction parameter.

Feel free to run these example queries -- they use the same Engine as the documentation.

This would sort by LEAST to MOST relevant (lowest to highest `_score):

curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
  -H 'Content-Type: application/json' \
  -d '{
        "engine_key": "5jZG1gmmCTFYbSSDjpqq",
        "q":"meta",
        "sort_direction":{"page":"asc"},
        "page": 1,
        "per_page": 2
      }'

This would sort by MOST to LEAST relevant (highest to lowest _score):

curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
  -H 'Content-Type: application/json' \
  -d '{
        "engine_key": "5jZG1gmmCTFYbSSDjpqq",
        "q":"meta",
        "sort_direction":{"page":"desc"},
        "page": 1,
        "per_page": 2
      }'

The sort_field parameter would then allow you to use a field to sort, ascending or descending, depending on the type of data in the field.

  • A number field will sort numerically.
  • A date field will sort historically.
  • ... etc.

This will override the _score value.

Hope this helps.

I shall polish up those docs today.

Enjoy the week,

Kellen

When I sort results by title search doesnt work as I would expect when the query/title consists two words eg . "nice b", should return:

nice b
nice boo
nice books

Is it possible to achieve that with site search?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.