Skip to content

Commit

Permalink
Sorting Selector + Improvements to ComboBox (#784)
Browse files Browse the repository at this point in the history
* WIP for sorting

* eslint

* add sortBy filtering to resolvers

* prettier update

* WIP

* update next example with sorting selector

* fix eslint errors

* do not fix node version

* do not run lib tests

* add sortBy tests on searchkit/client

* exclude transpiled tests

* update SortingSelector component

* fix ComboBox component

* fix issue with HitsResolver page on ES 6.x

* update packages

* update readme for searchkit/client

* SortingOption defaultOption to be optional

* build before testing

* adding documentation for sorting

* rename stage to reflect activity

* update documentation

* feature (#784): Sorting

* update yarn lock

Co-authored-by: Joseph McElroy <joe@josephs-air.lan>
  • Loading branch information
joemcelroy and Joseph McElroy committed Dec 1, 2020
1 parent cfb8ce2 commit 7f32920
Show file tree
Hide file tree
Showing 51 changed files with 1,227 additions and 225 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -42,6 +42,7 @@
],
"dot-notation":"error",
"import/namespace": "off",
"import/no-cycle": "off",
"import/order": [
"error",
{
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -7,5 +7,7 @@ jobs:
- uses: actions/checkout@v2
- name: Install modules
run: yarn
- name: Build Packages
run: yarn build
- name: Run tests
run: yarn test
2 changes: 0 additions & 2 deletions .vscode/launch.json
Expand Up @@ -14,7 +14,6 @@
"--runInBand", "--forceExit", "--detectOpenHandles", "--watch"
],
"cwd": "${workspaceRoot}/packages/searchkit-apollo-resolvers",
"runtimeVersion": "12.8.0",
"preLaunchTask": null,
"runtimeExecutable": null,
"env": {
Expand All @@ -34,7 +33,6 @@
"--runInBand", "--forceExit", "--detectOpenHandles", "--watch"
],
"cwd": "${workspaceRoot}/packages/searchkit-elastic-ui",
"runtimeVersion": "12.8.0",
"preLaunchTask": null,
"runtimeExecutable": null,
"env": {
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,17 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [3.0.0-canary.14](https://github.com/searchkit/searchkit/compare/v3.0.0-canary.12...v3.0.0-canary.14) (2020-12-01)


### Bug Fixes

* link ([#783](https://github.com/searchkit/searchkit/issues/783)) ([55f6f6f](https://github.com/searchkit/searchkit/commit/55f6f6f541f274f4c9249da63f98088be3f6ba4e))





# [3.0.0-canary.13](https://github.com/searchkit/searchkit/compare/v3.0.0-canary.12...v3.0.0-canary.13) (2020-11-18)

**Note:** Version bump only for package searchkit
Expand Down
60 changes: 0 additions & 60 deletions docs/.eslintrc.js

This file was deleted.

70 changes: 70 additions & 0 deletions docs/docs/searchkit-apollo-resolvers.md
Expand Up @@ -46,6 +46,76 @@ Query will be applied to results & facets
| :------------- | :----------- |
| fields | fields to be queried. See elasticsearch documentation for more information on options |

## Sorting
```javascript
const searchkitConfig = {
sortOptions: [
{ id: 'relevance', label: 'Relevance', field: '_score' },
{ id: 'released', label: 'Recent Releases', field: { released: 'desc' } },
{ id: 'multiple_sort', label: 'Multiple sort', field: [
{ "post_date" : {"order" : "asc"}},
"user",
{ "name" : "desc" },
{ "age" : "desc" },
"_score"
]},
]
}
```

Within SearchkitConfig, declare all the available sorting options that you want your search to support, each with a unique id. Above is an example of how sorting option fields can be declared. Field is provided to elasticsearch so supports all the options that elasticsearch supports. See [Elasticsearch sorting options](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/sort-search-results.html) for available options.

#### GraphQL Query Example
Once configured, you are able to:
1. query available sort options via `sortOptions` within the summary node
2. sortedBy will tell you the current sort option for hits

```gql
query {
results(query: "") {
summary {
total
sortOptions {
id
label
}
}
hits(sortBy: "relevance") {
sortedBy <--- the current sortId
}
}
}


```

then you will be able to specify how you want hits to be sorted by within the hits node using the id

```gql
{
results(query: "bla") {
hits(sortBy: "relevance") {
sortedBy
items {
id
fields {
writers
actors
}
}
}
}
}
```

#### Options
| Option | Description |
| :------------- | :----------- |
| id | Unique id. Used to specify what to sort by |
| label | label to be displayed in frontend |



## Facets
Facets are configured together on the Searchkit Config within the facets array. Each facet can support a range of experiences from a simple list to date filtering

Expand Down
9 changes: 6 additions & 3 deletions docs/docs/searchkit-client.md
Expand Up @@ -80,6 +80,9 @@ Adds filter to applied filters
#### toggleFilter(filter: Filter): void
If the filter already exists in applied filters then it will remove the filter. If doesn't exist, will add the filter

#### setSortBy(id: string): void
Set sorting id. Sort field is returned in the query variables

## withSeachkit HOC
Wraps component with Searchkit Provider with an instantiated SearchkitClient. Useful for NextJS Pages.

Expand All @@ -102,7 +105,7 @@ Provides to child components access to the shared SearchkitClient instance
### Usage

```javascript
import { SearchkitClient, SearchkitProvider } from '@searchkit/client
import { SearchkitClient, SearchkitProvider } from '@searchkit/client'

const client = new SearchkitClient()

Expand Down Expand Up @@ -152,7 +155,7 @@ Query contains all data requirements needed to power the search.
import { useSearchkitQuery } from '@searchkit/client'

const query = gql`
query resultSet($query: String, $filters: [FiltersSet], $page: PageInput) {
query resultSet($query: String, $filters: [FiltersSet], $page: PageInput, $sortBy: String) {
results(query: $query, filters: $filters) {
hits(page: $page) {
items {
Expand All @@ -175,4 +178,4 @@ const Index = () => {
### Options
| Option | Description |
| :------------- | :----------- |
| query | GQL query that has query, filters and page variables |
| query | GQL query that has query, filters, sorting & page variables |
41 changes: 41 additions & 0 deletions docs/docs/searchkit-elastic-ui.md
Expand Up @@ -163,6 +163,47 @@ import {
| :------------- | :----------- |
| data | Boolean. Loading value from GQL query |
### Sorting Selector
Pagination.mov
#### Usage
Requires sorting configuration within the resolvers. See [sorting documentation](/docs/reference/apollo-resolvers#sorting).
```javascipt
import {
SortingSelector
} from '@searchkit/elastic-ui'
```
```javascript
<SortingSelector data={data?.results} loading={loading} />
```
### GraphQL Query
Relies on GraphQL query
```
results(query: "") {
summary {
total
sortOptions { <--- Required: sortOptions
id
label
}
}
hits(sortBy: "relevance") {
sortedBy <--- Required: the current sortId
}
}

```
#### Options
| Option | Description |
| :------------- | :----------- |
| data | data response from graphql request |
| loading | Boolean. |
## Facets
### FacetsList
Expand Down
54 changes: 34 additions & 20 deletions examples/next/components/index.jsx
@@ -1,16 +1,16 @@
import { useSearchkitQuery } from '@searchkit/client'
import { gql } from '@apollo/client'
import { useState } from 'react'
import { HitsList, HitsGrid } from './searchkit/Hits'
import {
FacetsList,
SearchBar,
Pagination,
ResetSearchButton,
SelectedFilters
SelectedFilters,
SortingSelector
} from '@searchkit/elastic-ui'

import React, { useState } from 'react'

import {
EuiPage,
EuiPageBody,
Expand All @@ -24,11 +24,12 @@ import {
EuiTitle,
EuiHorizontalRule,
EuiButtonGroup,
EuiFlexGroup
EuiFlexGroup,
EuiFlexItem
} from '@elastic/eui'

const query = gql`
query resultSet($query: String, $filters: [FiltersSet], $page: PageInput) {
query resultSet($query: String, $filters: [FiltersSet], $page: PageInput, $sortBy: String) {
results(query: $query, filters: $filters) {
summary {
total
Expand All @@ -37,16 +38,21 @@ const query = gql`
label
value
}
sortOptions {
id
label
}
query
}
hits(page: $page) {
hits(page: $page, sortBy: $sortBy) {
page {
total
totalPages
pageNumber
from
size
}
sortedBy
items {
id
fields {
Expand Down Expand Up @@ -103,20 +109,28 @@ const Page = () => {
</EuiTitle>
</EuiPageContentHeaderSection>
<EuiPageContentHeaderSection>
<EuiButtonGroup
options={[
{
id: `grid`,
label: 'Grid'
},
{
id: `list`,
label: 'List'
}
]}
idSelected={viewType}
onChange={(id) => setViewType(id)}
/>
<EuiFlexGroup>
<EuiFlexItem grow={1}>
<SortingSelector data={data?.results} loading={loading} />
</EuiFlexItem>
<EuiFlexItem grow={2}>
<EuiButtonGroup
legend=""
options={[
{
id: `grid`,
label: 'Grid'
},
{
id: `list`,
label: 'List'
}
]}
idSelected={viewType}
onChange={(id) => setViewType(id)}
/>
</EuiFlexItem>
</EuiFlexGroup>
</EuiPageContentHeaderSection>
</EuiPageContentHeader>
<EuiPageContentBody>
Expand Down
4 changes: 4 additions & 0 deletions examples/next/pages/api/graphql.js
Expand Up @@ -16,6 +16,10 @@ const searchkitConfig = {
hits: {
fields: ['title', 'actors', 'writers', 'plot', 'poster']
},
sortOptions: [
{ id: 'relevance', label: "Relevance", field: [{"_score": "desc"}], defaultOption: true},
{ id: 'released', label: "Released", field: [{"released": "desc"}]},
],
query: new MultiMatchQuery({ fields: ['actors', 'writers', 'title^4', 'plot'] }),
facets: [
new RefinementSelectFacet({ field: 'type.raw', id: 'type', label: 'Type' }),
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "3.0.0-canary.13",
"version": "3.0.0-canary.14",
"includeMergedTags": true,
"command": {
"publish": {
Expand Down

1 comment on commit 7f32920

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for searchkit-docs ready!

✅ Preview
https://searchkit-docs-e6iah5orc.vercel.app

Built with commit 7f32920.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.