added docs to the repo

This commit is contained in:
Spencer Alger
2013-12-27 16:41:38 -07:00
parent 11c976e9f8
commit 65f9cc7e99
101 changed files with 3907 additions and 63 deletions

View File

@ -0,0 +1,21 @@
.Perform three operations in a single request
[source,js]
---------
client.bulk({
body: [
// action description
{ index: { _index: 'myindex', _type: 'mytype', _id: 1 } },
// the document to index
{ title: 'foo' },
// action description
{ update: { _index: 'myindex', _type: 'mytype', _id: 2 } },
// the document to update
{ doc: { title: 'foo' } },
// action description
{ delete: { _index: 'myindex', _type: 'mytype', _id: 3 } },
// no document needed for this delete
]
}, function (err, resp) {
// ...
});
---------

View File

@ -0,0 +1,11 @@
.Return 10 hottest threads
[source,js]
---------
client.cluster.nodeHotThreads({
threads: 10
nodeId: 'mymisbehavingnode',
maxRetries: 10
}, function (error, response) {
console.log(response);
})
---------

View File

@ -0,0 +1,10 @@
.Return information about JVM
[source,js]
---------
client.cluster.nodeInfo({ jvm: true })
.then(function (response) {
// enjoy your sweet info!
}, function (error) {
// scream!
})
---------

View File

@ -0,0 +1,37 @@
.Get the number of all documents in the cluster
[source,js]
---------
client.count(function (error, response, status) {
// check for and handle error
var count = response.count;
});
---------
.Get the number of documents in an index
[source,js]
---------
client.count({
index: 'index_name'
}, function (error, response) {
// ...
});
---------
.Get the number of documents matching a query
[source,js]
---------
client.count(
index: 'index_name',
body: {
filtered: {
filter: {
terms: {
foo: ['bar']
}
}
}
}
}, function (err, response) {
// ...
});
---------

View File

@ -0,0 +1,18 @@
.Create a document
[source,js]
---------
client.create({
index: 'myindex',
type: 'mytype',
id: '1',
body: {
title: 'Test 1',
tags: ['y', 'z'],
published: true,
published_at: '2013-01-01',
counter: 1
}
}, function (error, response) {
// ...
});
---------

View File

@ -0,0 +1,11 @@
.Delete the document `/myindex/mytype/1`
[source,js]
---------
client.delete({
index: 'myindex',
type: 'mytype',
id: '1'
}, function (error, response) {
// ...
});
---------

View File

@ -0,0 +1,23 @@
.Deleting documents with a simple query
[source,js]
---------
client.deleteByQuery({
index: 'myindex',
q: 'test'
}, function (error, response) {
// ...
});
---------
.Deleting documents using the Query DSL
[source,js]
---------
client.delete_by_query({
index: 'posts',
body: {
term: { published: false }
}
}, function (error, response) {
// ...
});
---------

View File

@ -0,0 +1,15 @@
.Check that the document `/myindex/mytype/1` exits
[source,js]
---------
client.exists({
index: 'myindex',
type: 'mytype',
id: 1
}, function (error, exists) {
if (exists === true) {
// ...
} else {
// ...
}
});
---------

View File

@ -0,0 +1,32 @@
.See how a document is scored against a simple query
[source,js]
---------
client.explain({
// the document to test
index: 'myindex',
type: 'mytype',
id: '1',
// the query to score it against
q: 'field:value'
}, function (error, response) {
// ...
});
---------
.See how a document is scored against a query written in the Query DSL
[source,js]
---------
client.explain({
index: 'myindex',
type: 'mytype',
id: '1',
body: {
query: {
match: { title: 'test' }
}
}
}, function (error, response) {
// ...
});
---------

View File

@ -0,0 +1,11 @@
.Get `/myindex/mytype/1`
[source,js]
---------
client.get({
index: 'myindex',
type: 'mytype',
id: 1
}, function (error, response) {
// ...
});
---------

View File

@ -0,0 +1,16 @@
.Create or update a document
[source,js]
---------
client.index({
index: 'myindex',
type: 'mytype',
id: '1',
body: {
title: 'Test 1',
tags: ['y', 'z'],
published: true,
}
}, function (error response) {
});
---------

View File

@ -0,0 +1,29 @@
.An array of doc locations. Useful for getting documents from different indices.
[source,js]
---------
client.mget({
body: {
docs: [
{ _index: 'indexA', _type: 'typeA', _id: '1' },
{ _index: 'indexB', _type: 'typeB', _id: '1' },
{ _index: 'indexC', _type: 'typeC', _id: '1' }
]
}
}, function(error, response){
// ...
});
---------
.An array of ids. You must also specify the `index` and `type` that apply to all of the ids.
[source,js]
---------
client.mget({
index: 'myindex',
type: 'mytype',
body: {
ids: [1, 2, 3]
}
}, function(error, response){
// ...
});
---------

View File

@ -0,0 +1,12 @@
.Search for similar documents using the `title` property of document `myindex/mytype/1`
[source,js]
---------
client.mlt({
index: 'myindex',
type: 'mytype',
id: 1,
mlt_fields: 'title'
}, function (errors, response) {
// ...
});
---------

View File

@ -0,0 +1,15 @@
.Perform multiple different searches, the body is made up of meta/data pairs
[source,js]
---------
client.msearch({
body: [
// match all query, on all indices and types
{}
{ query: { match_all: {} } },
// query_string query, on index/mytype
{ index: 'myindex', type: 'mytype' },
{ query: { query_string: { query: '"Test 1"' } } }
]
});
---------

View File

@ -0,0 +1,69 @@
.First, Register queries named “alert-1” and “alert-2” for the “myindex” index
[source,js]
---------
client.index({
index: '_percolator',
type: 'myindex',
id: 'alert-1',
body: {
// This query will be run against documents sent to percolate
query: {
query_string: {
query: 'foo'
}
}
}
}, function (error, response) {
// ...
});
client.index({
index: '_percolator',
type: 'myindex',
id: 'alert-2',
body: {
// This query will also be run against documents sent to percolate
query: {
query_string: {
query: 'bar'
}
}
}
}, function (error, response) {
// ...
});
---------
.Then you can send documents to learn which query `_percolator` queries they match
[source,js]
---------
client.percolate({
index: 'myindex',
body: {
doc: {
title: "Foo"
}
}
}, function (error, response) {
// response would equal
// {
// ok:true,
// matches: [ "alert-1" ]
// }
});
client.percolate({
index: 'myindex',
body: {
doc: {
title: "Foo Bar"
}
}
}, function (error, response) {
// response would equal
// {
// ok:true,
// matches: [ "alert-1", "alert-2" ]
// }
});
---------

View File

@ -0,0 +1,29 @@
.Collect every title in the index that contains the word "test"
[source,js]
---------
var allTitles = [];
// first we do a search, and specify a scroll timeout
client.search({
index: 'myindex',
// Set to 30 seconds because we are calling right back
scroll: '30s',
fields: ['title'],
q: 'title:test'
}, function getMoreUntilDone(error, response) {
// collect the title from each response
response.hits.hists.forEach(function (hit) {
allTitles.push(hit.fields.title);
});
if (response.hits.total !== allTitles.length) {
// now we can call scroll over and over
client.scroll({
scrollId: response._scroll_id,
scroll: '30s'
}, getMoreUntilDone);
} else {
console.log('every "test" title', allTitles);
}
});
---------

View File

@ -0,0 +1,34 @@
.Search with a simple query string query
[source,js]
---------
client.search({
index: 'myindex',
q: 'title:test'
}, function (error, response) {
// ...
});
---------
.Passing a full request definition in the Elasticsearch's Query DSL as a `Hash`
[source,js]
---------
client.search({
index: 'myindex',
body: {
query: {
match: {
title: 'test'
}
},
facets: {
tags: {
terms: {
field: 'tags'
}
}
}
}
}, function (error, response) {
// ...
}):
---------

View File

@ -0,0 +1,34 @@
.Return query terms suggestions (“auto-correction”)
[source,js]
---------
client.suggest({
index: 'myindex',
body: {
mysuggester: {
text: 'tset',
term: {
field: 'title'
}
}
}
}, function (error, response) {
// response will be formatted like so:
//
// {
// ...
// mysuggester: [
// {
// text: "tset",
// ...
// options: [
// {
// text: "test",
// score: 0.75,
// freq: 5
// }
// ]
// }
// ]
// }
});
---------

View File

@ -0,0 +1,69 @@
.Update document title using partial document
[source,js]
---------
client.update({
index: 'myindex',
type: 'mytype',
id: '1',
body: {
// put the partial document under the `doc` key
doc: {
title: 'Updated'
}
}
}, function (error, response) {
// ...
})
---------
.Add a tag to document `tags` property using a `script`
[source,js]
---------
client.update({
index: 'myindex',
type: 'mytype',
id: '1',
body: {
script: 'ctx._source.tags += tag',
params: { tag: 'some new tag' }
}
}, function (error, response) {
// ...
});
---------
.Increment a document counter by 1 or initialize it, when the document does not exist
[source,js]
---------
client.update({
index: 'myindex',
type: 'mytype',
id: '666',
body: {
script: 'ctx._source.counter += 1',
upsert: {
counter: 1
}
}
}, function (error, response) {
// ...
})
---------
.Delete a document if it's tagged “to-delete”
[source,js]
---------
client.update({
index: 'myindex',
type: 'mytype',
id: '1',
body: {
script: 'ctx._source.tags.contains(tag) ? ctx.op = "delete" : ctx.op = "none"',
params: {
tag: 'to-delete'
}
}
}, function (error, response) {
// ...
});
---------