-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
78 lines (70 loc) · 1.27 KB
/
example.js
File metadata and controls
78 lines (70 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const tantivy = require('.')
const mkdirp = require('mkdirp')
const fs = require('fs')
const p = require('path')
const schema = [
{
name: 'title',
type: 'text',
options: {
indexing: {
record: 'position',
tokenizer: 'en_stem'
},
stored: true
}
},
{
name: 'body',
type: 'text',
options: {
indexing: {
record: 'position',
tokenizer: 'en_stem'
},
stored: true
}
},
{
name: 'url',
type: 'text',
options: {
indexing: null,
stored: true
}
}
]
let path = '/tmp/tantivy-test'
if (!fs.existsSync(p.join(path, 'meta.json'))) {
mkdirp(path, create)
} else {
open()
}
function create (err) {
console.log(`create new index at ${path}`)
if (err) exit(err)
const index = tantivy.createInDir(path, schema)
start(index)
}
function open () {
console.log(`open index at ${path}`)
const index = tantivy.openInDir(path)
start(index)
}
function start (index) {
let docs = [
{
body: 'Now for real.',
title: 'Hello, world!'
},
{
body: 'The future',
title: 'Looks like a bright bright world'
},
]
index.writer()
index.addDocuments(docs)
index.commit()
let res = index.query('world')
console.log(res)
}