mirror of
https://github.com/krisk/Fuse.git
synced 2026-01-18 16:16:25 +00:00
413 lines
6.6 KiB
Markdown
413 lines
6.6 KiB
Markdown
# Examples
|
|
|
|
<TwitterFollow />
|
|
|
|
---
|
|
|
|
## Search String Array
|
|
|
|
:::: tabs
|
|
::: tab List
|
|
|
|
```json
|
|
["Old Man's War", "The Lock Artist"]
|
|
```
|
|
|
|
:::
|
|
::: tab JS
|
|
|
|
```javascript
|
|
const options = {
|
|
includeScore: true
|
|
}
|
|
|
|
const fuse = new Fuse(list, options)
|
|
|
|
const result = fuse.search('od man')
|
|
```
|
|
|
|
:::
|
|
|
|
::: tab Output
|
|
|
|
```json
|
|
[
|
|
{
|
|
"item": "Old Man's War",
|
|
"refIndex": 0,
|
|
"score": 0.35
|
|
}
|
|
]
|
|
```
|
|
|
|
:::
|
|
::::
|
|
|
|
## Search Object Array
|
|
|
|
:::: tabs
|
|
::: tab List
|
|
|
|
```json
|
|
[
|
|
{
|
|
"title": "Old Man's War",
|
|
"author": "John Scalzi",
|
|
"tags": ["fiction"]
|
|
},
|
|
{
|
|
"title": "The Lock Artist",
|
|
"author": "Steve",
|
|
"tags": ["thriller"]
|
|
}
|
|
]
|
|
```
|
|
|
|
:::
|
|
::: tab JS
|
|
|
|
```javascript
|
|
const options = {
|
|
includeScore: true,
|
|
// Search in `author` and in `tags` array
|
|
keys: ['author', 'tags']
|
|
}
|
|
|
|
const fuse = new Fuse(list, options)
|
|
|
|
const result = fuse.search('tion')
|
|
```
|
|
|
|
:::
|
|
|
|
::: tab Output
|
|
|
|
```json
|
|
[
|
|
{
|
|
"item": {
|
|
"title": "Old Man's War",
|
|
"author": "John Scalzi",
|
|
"tags": ["fiction"]
|
|
},
|
|
"refIndex": 0,
|
|
"score": 0.03
|
|
}
|
|
]
|
|
```
|
|
|
|
:::
|
|
::::
|
|
|
|
## Nested Search
|
|
|
|
You can search through nested values with different ways:
|
|
|
|
- define the path with dot notation (`.`)
|
|
- define the path with array notation (`[]`)
|
|
- Define a per-key `getFn` function
|
|
|
|
:::: tabs
|
|
::: tab List
|
|
|
|
```json
|
|
[
|
|
{
|
|
"title": "Old Man's War",
|
|
"author": {
|
|
"name": "John Scalzi",
|
|
"tags": [
|
|
{
|
|
"value": "American"
|
|
}
|
|
]
|
|
}
|
|
},
|
|
{
|
|
"title": "The Lock Artist",
|
|
"author": {
|
|
"name": "Steve Hamilton",
|
|
"tags": [
|
|
{
|
|
"value": "English"
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
```
|
|
|
|
:::
|
|
::: tab JS (dot notation)
|
|
|
|
```javascript
|
|
const options = {
|
|
includeScore: true,
|
|
keys: ['author.tags.value']
|
|
}
|
|
|
|
const fuse = new Fuse(list, options)
|
|
|
|
const result = fuse.search('engsh')
|
|
```
|
|
|
|
:::
|
|
|
|
::: tab JS (array notation)
|
|
|
|
```javascript
|
|
const options = {
|
|
includeScore: true,
|
|
keys: [['author', 'tags', 'value']]
|
|
}
|
|
|
|
const fuse = new Fuse(list, options)
|
|
|
|
const result = fuse.search('engsh')
|
|
```
|
|
|
|
:::
|
|
|
|
::: tab JS (get function)
|
|
|
|
```javascript
|
|
const options = {
|
|
includeScore: true,
|
|
keys: [
|
|
{ name: 'title', getFn: (book) => book.title },
|
|
{ name: 'authorName', getFn: (book) => book.author.name }
|
|
]
|
|
})
|
|
|
|
const fuse = new Fuse(list, options)
|
|
|
|
const result = fuse.search({ authorName: 'Steve' })
|
|
```
|
|
|
|
:::
|
|
|
|
::: tab Output
|
|
|
|
```json
|
|
[
|
|
{
|
|
"item": {
|
|
"title": "The Lock Artist",
|
|
"author": {
|
|
"name": "Steve Hamilton",
|
|
"tags": [
|
|
{
|
|
"value": "English"
|
|
}
|
|
]
|
|
}
|
|
},
|
|
"refIndex": 1,
|
|
"score": 0.4
|
|
}
|
|
]
|
|
```
|
|
|
|
:::
|
|
::::
|
|
|
|
::: warning IMPORTANT
|
|
The path has to eventually point to a string, otherwise you will not get any results.
|
|
:::
|
|
|
|
## Weighted Search
|
|
|
|
You can allocate a weight to keys to give them higher (or lower) values in search results. The `weight` value has to be greater than `0`.
|
|
|
|
:::: tabs
|
|
::: tab List
|
|
|
|
```json
|
|
[
|
|
{
|
|
"title": "Old Man's War fiction",
|
|
"author": "John X",
|
|
"tags": ["war"]
|
|
},
|
|
{
|
|
"title": "Right Ho Jeeves",
|
|
"author": "P.D. Mans",
|
|
"tags": ["fiction", "war"]
|
|
}
|
|
]
|
|
```
|
|
|
|
:::
|
|
::: tab JS
|
|
|
|
```javascript
|
|
const options = {
|
|
includeScore: true,
|
|
keys: [
|
|
{
|
|
name: 'title',
|
|
weight: 0.3
|
|
},
|
|
{
|
|
name: 'author',
|
|
weight: 0.7
|
|
}
|
|
]
|
|
}
|
|
|
|
// Create a new instance of Fuse
|
|
const fuse = new Fuse(books, options)
|
|
|
|
// Now search for 'Man'
|
|
const result = fuse.search('Man')
|
|
```
|
|
|
|
:::
|
|
|
|
::: tab Output
|
|
|
|
```json
|
|
[
|
|
{
|
|
"item": {
|
|
"title": "Right Ho Jeeves",
|
|
"author": "P.D. Mans",
|
|
"tags": ["fiction", "war"]
|
|
},
|
|
"refIndex": 1, // index in the original list
|
|
"score": 0.12282280261157906
|
|
},
|
|
{
|
|
"item": {
|
|
"title": "Old Man's War fiction",
|
|
"author": "John X",
|
|
"tags": ["war"]
|
|
},
|
|
"refIndex": 0,
|
|
"score": 0.3807307877431757
|
|
}
|
|
]
|
|
```
|
|
|
|
:::
|
|
::::
|
|
|
|
### Default `weight`
|
|
|
|
When a `weight` isn't provided, it will default to `1`. In the following example, while `author` has been given a weight of `2`, `title` will be assigned a weight of `1`.
|
|
|
|
```js
|
|
const fuse = new Fuse(books, {
|
|
keys: [
|
|
'title', // will be assigned a `weight` of 1
|
|
{
|
|
name: 'author',
|
|
weight: 2
|
|
}
|
|
]
|
|
})
|
|
```
|
|
|
|
Note that internally Fuse will normalize the weights to be within `0` and `1` exclusive.
|
|
|
|
## Extended Search
|
|
|
|
This form of advanced searching allows you to fine-tune results.
|
|
|
|
White space acts as an **AND** operator, while a single pipe (`|`) character acts as an **OR** operator. To escape white space, use double quote ex. `="scheme language"` for exact match.
|
|
|
|
| Token | Match type | Description |
|
|
| ----------- | -------------------------- | -------------------------------------- |
|
|
| `jscript` | fuzzy-match | Items that fuzzy match `jscript` |
|
|
| `=scheme` | exact-match | Items that are `scheme` |
|
|
| `'python` | include-match | Items that include `python` |
|
|
| `!ruby` | inverse-exact-match | Items that do not include `ruby` |
|
|
| `^java` | prefix-exact-match | Items that start with `java` |
|
|
| `!^earlang` | inverse-prefix-exact-match | Items that do not start with `earlang` |
|
|
| `.js$` | suffix-exact-match | Items that end with `.js` |
|
|
| `!.go$` | inverse-suffix-exact-match | Items that do not end with `.go` |
|
|
|
|
White space acts as an **AND** operator, while a single pipe (`|`) character acts as an **OR** operator.
|
|
|
|
:::: tabs
|
|
::: tab List
|
|
|
|
```json
|
|
[
|
|
{
|
|
"title": "Old Man's War",
|
|
"author": "John Scalzi"
|
|
},
|
|
{
|
|
"title": "The Lock Artist",
|
|
"author": "Steve"
|
|
},
|
|
{
|
|
"title": "Artist for Life",
|
|
"author": "Michelangelo"
|
|
}
|
|
]
|
|
```
|
|
|
|
:::
|
|
::: tab JS
|
|
|
|
```javascript
|
|
const options = {
|
|
includeScore: true,
|
|
useExtendedSearch: true,
|
|
keys: ['title']
|
|
}
|
|
|
|
const fuse = new Fuse(books, options)
|
|
|
|
// Search for items that include "Man" and "Old",
|
|
// OR end with "Artist"
|
|
fuse.search("'Man 'Old | Artist$")
|
|
```
|
|
|
|
:::
|
|
|
|
::: tab Output
|
|
|
|
```json
|
|
[
|
|
{
|
|
"item": {
|
|
"title": "Old Man's War",
|
|
"author": "John Scalzi"
|
|
},
|
|
"refIndex": 0,
|
|
"score": 2.220446049250313e-16
|
|
},
|
|
{
|
|
"item": {
|
|
"title": "The Lock Artist",
|
|
"author": "Steve"
|
|
},
|
|
"refIndex": 1,
|
|
"score": 2.220446049250313e-16
|
|
}
|
|
]
|
|
```
|
|
|
|
:::
|
|
::::
|
|
|
|
<!-- ```js
|
|
const result = fuse.search({
|
|
$and: [
|
|
{ title: 'old war' }, // Fuzzy "old war"
|
|
{ color: "'blue" }, // Exact match for blue
|
|
{
|
|
$or: [
|
|
{ title: '!arts' }, // Does not have "arts"
|
|
{ title: '^lock' } // Starts with "lock"
|
|
]
|
|
}
|
|
]
|
|
})
|
|
``` -->
|
|
|
|
<Donate />
|