Skip to content

Instantly share code, notes, and snippets.

@radu-gheorghe
radu-gheorghe / log_backup.bash
Created July 26, 2012 08:31
Optimize&Backup Elasticsearch index. And restore.
#!/usr/bin/env bash
###############FUNCTIONS############
function prepare {
#optimize the index
echo -n "Optimizing index $INDEX_NAME..."
curl -XPOST "$ADDRESS/$INDEX_NAME/_optimize" 2>/dev/null| grep 'failed":0' >/dev/null
if [ $? -eq 0 ]; then
echo "done"
@radu-gheorghe
radu-gheorghe / _alltest.sh
Created October 30, 2012 15:27
Elasticsearch - include in _all with index=no
echo ===Removing everything===
curl -XDELETE localhost:9200/test
echo
echo ===Putting empty index===
curl -XPUT localhost:9200/test
echo
echo ====Putting mapping===
curl -XPUT localhost:9200/test/test/_mapping -d '{
"test": {
"properties": {
@radu-gheorghe
radu-gheorghe / copy_es_index.bash
Created November 5, 2012 09:30
copy one Elasticesarch index to another
#!/bin/bash
CURRENTINDEX="test"
NEWINDEX="newindex"
#where the indices are stored within the DATADIR
INDICESDIR=/var/lib/elasticsearch/elasticsearch/nodes/0/indices/
#get the metadata for the current index
curl localhost:9200/$CURRENTINDEX/_settings?pretty=true > /tmp/settings
curl localhost:9200/$CURRENTINDEX/_mapping?pretty=true > /tmp/mappings
@radu-gheorghe
radu-gheorghe / sort_array.bash
Created November 7, 2012 11:41
Elasticsearch - sort by the sum of elements within an array
#!/bin/bash
#delete the test type
curl -XDELETE localhost:9200/test/test2/
#insert some docs######
curl -XPOST localhost:9200/test/test2/ -d '{
"id": 123,
"a": 10,
"b": 12,
@radu-gheorghe
radu-gheorghe / id.sh
Created November 9, 2012 20:19
child object ID search
#!/bin/bash
#delete the test index
curl -XDELETE localhost:9200/testing/
echo
#put the document
curl -XPOST localhost:9200/testing/testing/ -d '{
"document": {
"title": "foo",
"id": 1,
@radu-gheorghe
radu-gheorghe / simple_ES_performance
Created November 14, 2012 13:51
simple ES performance test
$ cat inserter.py
import pyes
import sys
conn = pyes.ES('127.0.0.1:9200', bulk_size=int(sys.argv[2]))
for i in range(int(sys.argv[1])):
conn.index({"name":"Joe Tester", "parsedtext":"Joe Testere nice guy", "uuid":"11111", "position":1}, "test-index", "test-type", bulk=True)
$ time python inserter.py 1000 1
@radu-gheorghe
radu-gheorghe / terms_facet.sh
Created November 15, 2012 07:51
Elasticsearch terms facet
curl -XDELETE localhost:9200/test
#{"ok":true,"acknowledged":true}
curl -XPOST localhost:9200/test/test/ -d '{"timestamp": "random_date", "tag":"red", "counter": 3}'
#{"ok":true,"_index":"test","_type":"test","_id":"MLv-xERyTFaIdkQeaxoiaw","_version":1}
curl -XPOST localhost:9200/test/test/ -d '{"timestamp": "random_date", "tag":"red", "counter": 7}'
#{"ok":true,"_index":"test","_type":"test","_id":"JaMQIZpbQaKrgdTOtaEEcg","_version":1}
curl -XPOST localhost:9200/test/test/ -d '{"timestamp": "random_date", "tag":"blue", "counter": 5}'
@radu-gheorghe
radu-gheorghe / terms_facet_fail
Created November 15, 2012 07:56
Elasticsearch terms facet string
$ curl -XPOST localhost:9200/test/test/ -d '{"timestamp": "random_date", "tag":"blue", "counter": "5"}'
{"ok":true,"_index":"test","_type":"test","_id":"qVmyc9otSqWqUOKWoteM5Q","_version":1}
$ curl -XGET localhost:9200/test/test/_mapping?pretty=true
{
"test" : {
"properties" : {
"counter" : {
"type" : "string"
},
@radu-gheorghe
radu-gheorghe / es_bulk1
Created November 15, 2012 12:11
Elasticsearch bulk insert with index name in URL
$ cat req
{ "index" : { "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "index" : { "_type" : "type1", "_id" : "2" } }
{ "field1" : "value2" }
$ curl -XPOST localhost:9200/testing/_bulk?pretty=true --data-binary @req
{
"took" : 5,
"items" : [ {
"index" : {
@radu-gheorghe
radu-gheorghe / traffic.sh
Created November 16, 2012 14:24
bash script for checking out traffic via /proc/net/dev
#!/bin/bash
#shows traffic on the specified device
function human_readable {
VALUE=$1
BIGGIFIERS=( B K M G )
CURRENT_BIGGIFIER=0
while [ $VALUE -gt 10000 ] ;do
VALUE=$(($VALUE/1000))