Skip to content

Commit

Permalink
new API
Browse files Browse the repository at this point in the history
  • Loading branch information
fergiemcdowall committed Apr 14, 2016
1 parent 6b1828d commit 254a9c3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 62 deletions.
4 changes: 4 additions & 0 deletions doc/EXAMPLES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Examples



(**WARNING: These examples are currently outdated- be aware of changes to the API in the latest version**)

This folder contains examples of how you can use `search-index` to make cool apps.

[search-index-indexing-in-browser](https://github.com/fergiemcdowall/search-index-indexing-in-browser) indexes data from the browser, into the browser.
Expand Down
162 changes: 103 additions & 59 deletions doc/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ You can search in your index by passing a query object to the search
function like so:

```javascript
q.query = {'*': ['usa']}; //search for string 'usa' in all ('*') fields
q.query = {
AND: [{'*': ['usa']}] //search for string 'usa' in all ('*') fields
}
si.search(q, function (err, searchResults) {
//do something with searchResults
})
Expand All @@ -19,7 +21,7 @@ Use the wildcard token ('*') to do simple searches across all fields

```javascript
q.query = {
'*': ['reagan']
AND: {'*': ['reagan']}
}
```

Expand All @@ -34,9 +36,40 @@ results. In the example below, only documents containing the word *reagan*
in the `title` field **AND** the word *usa* in the `body` field will be returned.

```javascript
q.query = {
'title': ['reagan'],
'body':['usa']
q.query =
{
AND: [
{'title': ['reagan']},
{'body':['usa']}
]
}
}
```

### Boolean Search (AND, OR, NOT)

You can construct boolean AND, OR and NOT queries:

```javascript
q.query = [ // Each array element is an OR condition
{
AND: [
{'title': ['reagan']}, // 'reagan' AND 'ussr'
{'body':['ussr']}
],
NOT: [
{'body':['usa']} // but NOT 'usa' in the body field
]
},
{ // OR this condition
AND: [
{'title': ['gorbachev']},// 'gorbachev' AND 'ussr'
{'body':['ussr']}
],
NOT: [
{'body':['usa']} // NOT 'usa' in the body field
]
}
}
```

Expand All @@ -51,7 +84,7 @@ all of `[<searchterms>]` appear in `<fieldName>`

```javascript
q.query = {
'*': ['usa', 'reagan']
AND: {'*': ['usa', 'reagan']}
}
```

Expand All @@ -62,32 +95,38 @@ phrases if desired.

```javascript
q.query = {
'*': ['ronald reagan']
AND: {'*': ['ronald reagan']}
}
```

##Faceting and filtering
##Categories, Buckets and Filters

You can add facets or filters onto any given search.
You can add categories and buckets onto any given search. You can use
the results to then create filters.

**Facets** display agregations on the resultset. This could typically
be price, metadata, source, or some other form of categorisation
that can be used as a filter
**Categories** display totals for each category in the resultset

**Buckets** display totals for ranges defined in the query

**Filters** are a way of limiting the resultset. Think of them as the
query that must be called when you select facets.
query that must be called when you select buckets/categories.


### Simple categories

### Simple facets
To use categories, you must define filters when you index the data
(TODO: describe indexing more here)

The following query will display a count for every single place in the
resultset

```javascript
q.query = {
'*': ['reagan']
};
q.facets = {places: {}};
AND: {'*': ['reagan']}
}
q.categories = {
name: 'places'
}
```

### Limiting facet length
Expand All @@ -96,9 +135,12 @@ The following query will limit facet length

```javascript
q.query = {
'*': ['reagan']
};
q.facets = {places: {limit: 10}};
AND: {'*': ['reagan']}
}
q.categories = {
name: 'places',
limit: 10
}
```

### Sorting facets
Expand All @@ -109,45 +151,44 @@ order. Available sorts are `keyAsc`,`keyDesc`, `valueAsc`, and

```javascript
q.query = {
'*': ['reagan']
};
q.facets = {places: {sort: 'keyDesc'}};
AND: {'*': ['reagan']}
}
q.categories = {
name: 'places',
limit: 10,
sort: keyDesc
}
```

### Range facets
### Buckets

The following query will display a count for every range of values
that is defined in the query
within the given buckets

```javascript

q.query = {'*': ['africa', 'bank']};
q.facets = {
totalamt: {
ranges:[
[
'000000000000000',
'000000050000000'
],
[
'000000050000001',
'100000000000000'
]
]
q.buckets = [
{
field: 'totalamt',
gte: '000000000000000',
lte: '000000050000000'
},
mjtheme: {
ranges: [
[
'A',
'J'
],
[
'K',
'Z'
]
]
}
}
{
field: 'totalamt',
gte: '000000050000001',
lte: '100000000000000'
},
{
field: 'mjtheme',
gte: 'A',
lte: 'J'
},
{
field: 'mjtheme',
gte: 'K',
lte: 'Z'
}
]
```

## Some other query stuff
Expand All @@ -160,8 +201,8 @@ page size is 50:

```javascript
q.query = {
'*': ['usa']
};
AND:{'*': ['usa']}
}
q.offset = 100;
q.pageSize = 50;
```
Expand All @@ -174,25 +215,28 @@ Simply specify the field to generate the teaser on like so:

```javascript
q.query = {
'*': ['usa']
AND:{'*': ['usa']}
};
q.teaser = 'body';
```

### Common error when test-querying your search solution

When you have indexed some documents, you want to check if your search is working, and do a couple of test queries. Remember then, to not use any of the stopwords. These are words that hold little meaning and are therefore left out of the index. This is [search-index' stopword list for English](https://github.com/fergiemcdowall/stopword/blob/master/lib/stopwords_en.js#L25-L38), and here are all the [available stopword languages](https://github.com/fergiemcdowall/stopword/tree/master/lib).
When you have indexed some documents, you want to check if your search
is working, and do a couple of test queries. Remember then, to not use
any of the stopwords. These are words that hold little meaning and are
therefore left out of the index. This is the [search-index stopword list for English](https://github.com/fergiemcdowall/stopword/blob/master/lib/stopwords_en.js#L25-L38), and here are all the [available stopword languages](https://github.com/fergiemcdowall/stopword/tree/master/lib).

```javascript
q.query = {
'*': ['and']
};
AND: {'*': ['and']}
}
```

And since there's an inherent logical `AND` when having multiple words in your query, it only takes one word from the stopword-list to get zero results back.
And since there iss an inherent logical `AND` when having multiple words in your query, it only takes one word from the stopword-list to get zero results back.

```javascript
q.query = {
'*': ['and', 'meaningful']
AND: {'*': ['and', 'meaningful']}
};
```
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "search-index",
"description": "A persistent full text search engine for the browser and Node.js",
"version": "0.7.14",
"version": "0.8.0",
"homepage": "https://github.com/fergiemcdowall/search-index",
"engines": {
"node": ">=3"
Expand All @@ -13,12 +13,12 @@
"leveldown": "^1.4.3",
"levelup": "^1.3.1",
"lodash.defaults": "^4.0.1",
"search-index-adder": "0.0.16",
"search-index-adder": "0.0.17",
"search-index-deleter": "0.0.6",
"search-index-getter": "0.0.6",
"search-index-matcher": "0.0.6",
"search-index-replicator": "0.0.5",
"search-index-searcher": "0.0.10",
"search-index-searcher": "0.0.11",
"stopword": "0.0.6"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions test/browser/selenium-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ const fs = require('fs')
const test = require('tape')
var server

test('check size of bundle', function (t) {
t.plan(1)
fs.stat('./test/sandbox/bundle.js', function(err, stats) {
console.log(stats.size)
t.ok((stats.size < 1100000), 'bundle should be less than 1mb')
})
})


test('start server', function (t) {
t.plan(1)
server = require('http').createServer(function (req, res) {
Expand Down
2 changes: 2 additions & 0 deletions test/node/general-tests/facet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ it('should be able to search in indexed data with faceting', function (done) {
}
]
si.search(q, function (err, results) {
// console.log(JSON.stringify(results.categories, null, 2))
should.exist(results)
;(err === null).should.be.exactly(true)
results.hits.length.should.be.exactly(6)
Expand Down Expand Up @@ -305,6 +306,7 @@ it('should be able to search in indexed data with faceting, using a different so
}
]
si.search(q, function (err, results) {
// console.log(JSON.stringify(results.categories, null, 2))
should.exist(results)
;(err === null).should.be.exactly(true)
results.hits.length.should.be.exactly(6)
Expand Down

0 comments on commit 254a9c3

Please sign in to comment.