-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
394 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Client } from "../mod.ts"; | ||
|
||
const client = new Client(); | ||
await client.connect("http://localhost:9200/"); | ||
|
||
async function run() { | ||
// await client.indices.create({ | ||
// index: "tweets", | ||
// body: { | ||
// mappings: { | ||
// properties: { | ||
// id: { type: "integer" }, | ||
// text: { type: "text" }, | ||
// user: { type: "keyword" }, | ||
// time: { type: "date" }, | ||
// }, | ||
// }, | ||
// }, | ||
// }, { ignore: [400] }); | ||
|
||
const dataset = [{ | ||
id: 1, | ||
text: "If I fall, don't bring me back.", | ||
user: "jon", | ||
date: new Date(), | ||
}, { | ||
id: 2, | ||
text: "Winter is coming", | ||
user: "ned", | ||
date: new Date(), | ||
}, { | ||
id: 3, | ||
text: "A Lannister always pays his debts.", | ||
user: "tyrion", | ||
date: new Date(), | ||
}, { | ||
id: 4, | ||
text: "I am the blood of the dragon.", | ||
user: "daenerys", | ||
date: new Date(), | ||
}, { | ||
id: 5, // change this value to a string to see the bulk response with errors | ||
text: "A girl is Arya Stark of Winterfell. And I'm going home.", | ||
user: "arya", | ||
date: new Date(), | ||
}]; | ||
|
||
const body = dataset.flatMap((doc) => [{ index: { _index: "tweets" } }, doc]); | ||
|
||
const bulkResponse = await client.bulk({ refresh: true, body }); | ||
|
||
if (bulkResponse.errors) { | ||
const erroredDocuments: any[] = []; | ||
// The items array has the same order of the dataset we just indexed. | ||
// The presence of the `error` key indicates that the operation | ||
// that we did for the document has failed. | ||
bulkResponse.items.forEach((action: any, i: number) => { | ||
const operation = Object.keys(action)[0]; | ||
if (action[operation].error) { | ||
erroredDocuments.push({ | ||
// If the status is 429 it means that you can retry the document, | ||
// otherwise it's very likely a mapping error, and you should | ||
// fix the document before to try it again. | ||
status: action[operation].status, | ||
error: action[operation].error, | ||
operation: body[i * 2], | ||
document: body[i * 2 + 1], | ||
}); | ||
} | ||
}); | ||
console.log(erroredDocuments); | ||
} | ||
|
||
const { count } = await client.count({ index: "tweets" }); | ||
console.log(count); | ||
} | ||
|
||
run().catch(console.log); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { StatInfo } from "./types.ts"; | ||
import { ajax, Method } from "./utils/ajax.ts"; | ||
|
||
export class Indices { | ||
// TODO use options | ||
create(params: { | ||
index: string; | ||
body: any; | ||
method?: Method; | ||
}, options?: { | ||
ignore: number[]; | ||
}) { | ||
const { index, method = "put", body } = params; | ||
const path = "/" + encodeURIComponent(index); | ||
return ajax<StatInfo>({ | ||
url: path, | ||
method, | ||
data: body, | ||
}); | ||
} | ||
|
||
stats(params: { | ||
index?: string; | ||
method?: Method; | ||
metric?: string; | ||
}): Promise<StatInfo> { | ||
const { index, method = "get", metric } = params; | ||
let path = ""; | ||
|
||
if (index != null && metric != null) { | ||
path = "/" + encodeURIComponent(index) + "/" + "_stats" + "/" + | ||
encodeURIComponent(metric); | ||
} else if (metric != null) { | ||
path = "/" + "_stats" + "/" + encodeURIComponent(metric); | ||
} else if (index != null) { | ||
path = "/" + encodeURIComponent(index) + "/" + "_stats"; | ||
} else { | ||
path = "/" + "_stats"; | ||
} // build request object | ||
return ajax<StatInfo>({ | ||
url: path, | ||
method, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.