Skip to content

Instantly share code, notes, and snippets.

@lmangani
Last active January 14, 2018 16:16
Show Gist options
  • Save lmangani/8f0ee70428113c947bd0dea918f3778d to your computer and use it in GitHub Desktop.
Save lmangani/8f0ee70428113c947bd0dea918f3778d to your computer and use it in GitHub Desktop.
Elassandra Example

Elassandra Cheat-Sheet

This guide requires Elassandra 5.5.0.9

Elassandra Example:

Try indexing a document on a non-existing index:

curl -XPUT 'http://localhost:9200/twitter/doc/1?pretty' -H 'Content-Type: application/json' -d '
{
    "message": "Elassandra adds dynamic mapping to Cassandra",
    "post_date": "2017/10/4 13:12:00",
    "user": "Poulpy"
}'

Then look-up in Cassandra:

bin/cqlsh -c "SELECT * from twitter.doc"

Behind the scene, Elassandra has created a new Keyspace twitter and table doc.

Now, insert a row with CQL:

INSERT INTO twitter.doc ("_id", user, post_date, message)
VALUES ( '2', ['Jimmy'], [dateof(now())], ['New data is indexed automatically']);

Search for our new row using CQL:

bin/cqlsh -c "SELECT * from twitter.doc WHERE user CONTAINS 'Jimmy' ALLOW FILTERING"
 _id | message                               | post_date                           | user
-----+---------------------------------------+-------------------------------------+-----------
   2 | ['New data is indexed automatically'] | ['2018-01-04 19:49:55.302000+0000'] | ['Jimmy']

Then search for it with the Elasticsearch API:

curl "localhost:9200/twitter/_search?q=user:Jimmy&pretty"

And here is a sample response :

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.9808292,
    "hits" : [
      {
        "_index" : "twitter",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.9808292,
        "_source" : {
          "post_date" : "2017/10/04 13:20:00",
          "message" : "New data is indexed automatically",
          "user" : "Jimmy"
        }
      }
    ]
  }
}

Index existing CQL Tables to Elasticsearch

Make sure keyspace is correctly configured for Elassandra:

ALTER KEYSPACE gun_db WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'DC1' : 1 } AND DURABLE_WRITES = true ;

Create mapping for gun_db.gun_data with field auto-discovery and custom index on field soul:

PUT gun_db
{
  "mappings": {
    "gun_data": {
      "discover" : ".*",
      "properties": {
        "soul": {
          "type": "text"
        }
      }
    }
  }
}

Query Cassandra data from ES with aggs pseudo-range

GET gun_db/_search
{
  "size": 10,
  "aggs": {
    "by_range": {
      "range": {
        "field": "state",
        "ranges": [
          {
            "from": 1515860865490,
            "to": 1515860865499
          }
        ]
      },
      "aggs": {
        "soul": {
          "terms": {
            "field": "soul"
          },
          "aggs": {
            "field": {
              "terms": {
                "field": "field"
              },
              "aggs": {
                "value": {
                  "terms": {
                    "field": "value"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Extras

Add ExtendedElasticSecondaryIndex index for user column:

CREATE CUSTOM INDEX elastic_tweet_idx_user ON twitter.tweet (user) USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment