Skip to content

Commit 441763b

Browse files
committed
Added tesseract project
1 parent 118e723 commit 441763b

File tree

12 files changed

+4765
-0
lines changed

12 files changed

+4765
-0
lines changed

elasticsearch-js/.babelrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"node": "current"
8+
}
9+
}
10+
]
11+
],
12+
"plugins": ["@babel/plugin-transform-async-to-generator"]
13+
}
14+

elasticsearch-js/.gitignore

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# dotenv environment variables file
55+
.env
56+
57+
# gatsby files
58+
.cache/
59+
public
60+
61+
# Mac files
62+
.DS_Store
63+
64+
# Yarn
65+
yarn-error.log
66+
.pnp/
67+
.pnp.js
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
lib

elasticsearch-js/README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Introduction
2+
3+
Elasticsearch might be used for `primary` document database.
4+
In this project we try setup a local `elasticsearch server` and index documents by using `bulk` method.
5+
6+
# Objectives
7+
8+
- Setup local `elasticsearch server`
9+
- Use `javascript client` to index documents
10+
11+
# Implementation
12+
13+
- Start `elasticsearch server` locally
14+
15+
```sh
16+
npm install @elastic/elasticsearch --save
17+
# or
18+
yarn add @elastic/elasticsearch
19+
```
20+
21+
```
22+
import { Client } from '@elastic/elasticsearch'
23+
24+
const ES_URL = 'http://localhost:9200'
25+
const client = new Client({ node: ES_URL })
26+
27+
const init = async () => {
28+
try {
29+
const info = await client.info()
30+
console.log(JSON.stringify(info))
31+
} catch (e) {
32+
console.error(e)
33+
}
34+
}
35+
36+
const body = [
37+
{ index: { _index: 'test', _id: 1 }},
38+
{ address: '162 N. Lafayette Dr.Leland', zip: '28451', city: 'NC' },
39+
40+
{ index: { _index: 'test', _id: 2 }},
41+
{ address: '221 Rockaway St. Smyrna', zip: '30080', city: 'GA' },
42+
43+
{ index: { _index: 'test', _id: 3 }},
44+
{ address: '922 Grant Avenue Marquette', zip: '49855', city: 'MI' },
45+
]
46+
47+
const bulk = async () => {
48+
try {
49+
const resp = await client.bulk({ refresh: true, body })
50+
51+
console.log(JSON.stringify(resp))
52+
} catch (e) {
53+
console.error(e)
54+
}
55+
}
56+
57+
(async() => {
58+
await init()
59+
await bulk()
60+
})()
61+
```
62+
63+
- `info` response
64+
65+
```json
66+
...
67+
"name": "es1",
68+
"cluster_name": "docker-cluster",
69+
"cluster_uuid": "mRjvZ7x1T4yGKfEUgJJPrg"
70+
...
71+
```
72+
73+
- `bulk` response
74+
75+
```json
76+
{
77+
"body": {
78+
"took": 737,
79+
"errors": false,
80+
"items": [...]
81+
}
82+
}
83+
```
84+
85+
# Usages
86+
87+
```javascript
88+
// matching
89+
const match = async () => {
90+
const { body } = await client.search({
91+
index: 'test',
92+
body: {
93+
query: {
94+
match: {
95+
city: 'NC'
96+
}
97+
}
98+
}
99+
})
100+
101+
console.log(body.hits.hits)
102+
}
103+
```
104+
105+
```javascript
106+
// missing property
107+
const missing = async () => {
108+
const { body } = await client.search({
109+
index: 'test',
110+
body: {
111+
query: {
112+
bool: {
113+
must_not: {
114+
exists: {
115+
field: 'district'
116+
}
117+
}
118+
}
119+
}
120+
}
121+
})
122+
123+
console.log(body.hits.hits)
124+
}
125+
```
126+
127+
```javascript
128+
// prefix matching
129+
const prefix = async () => {
130+
const { body } = await client.search({
131+
index: 'test',
132+
body: {
133+
query: {
134+
prefix: {
135+
address: {
136+
value: '92'
137+
}
138+
}
139+
}
140+
}
141+
})
142+
143+
console.log(body.hits.hits)
144+
}
145+
```
146+
147+
# References
148+
- [elasticsearch-js](https://github.com/elastic/elasticsearch-js)
149+
- [Elasticsearch Bulk](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html)
150+
- [Bulk Example](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/bulk_examples.html)
151+
- [Term level queries](https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '3'
2+
services:
3+
es:
4+
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.1
5+
container_name: es
6+
ports:
7+
- 9200:9200
8+
- 9300:9300
9+
volumes:
10+
- ./data:/elasticsearch_data
11+
- /tmp/elasticsearch:/tmp/elasticsearch
12+
restart: always
13+
environment:
14+
- node.name=es1
15+
- discovery.type=single-node
16+
- bootstrap.memory_lock=true
17+

elasticsearch-js/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "elasticsearch-js",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"build": "./node_modules/.bin/babel --version && ./node_modules/.bin/babel src -d lib",
8+
"start": "node lib/index.js"
9+
},
10+
"devDependencies": {
11+
"@babel/cli": "^7.7.7",
12+
"@babel/core": "^7.7.7",
13+
"@babel/plugin-transform-async-to-generator": "^7.7.4",
14+
"@babel/preset-env": "^7.7.7"
15+
},
16+
"dependencies": {
17+
"@elastic/elasticsearch": "^7.5.0"
18+
}
19+
}

elasticsearch-js/src/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Client } from '@elastic/elasticsearch'
2+
3+
const ES_URL = 'http://localhost:9200'
4+
const client = new Client({ node: ES_URL })
5+
6+
const init = async () => {
7+
try {
8+
const info = await client.info()
9+
console.log(JSON.stringify(info))
10+
} catch (e) {
11+
console.error(e)
12+
}
13+
}
14+
15+
const body = [
16+
{ index: { _index: 'test', _id: 1 } },
17+
{ address: '162 N. Lafayette Dr.Leland', zip: '28451', city: 'NC' },
18+
19+
{ index: { _index: 'test', _id: 2 } },
20+
{ address: '221 Rockaway St. Smyrna', zip: '30080', city: 'GA' },
21+
22+
{ index: { _index: 'test', _id: 3 } },
23+
{ address: '922 Grant Avenue Marquette', zip: '49855', city: 'MI' },
24+
]
25+
26+
const bulk = async () => {
27+
try {
28+
const resp = await client.bulk({ refresh: true, body })
29+
30+
console.log(JSON.stringify(resp))
31+
} catch (e) {
32+
console.error(e)
33+
}
34+
}
35+
36+
(async() => {
37+
await init()
38+
await bulk()
39+
})()

0 commit comments

Comments
 (0)