Creating the user index.
curl -XPUT http://localhost:9200/user?pretty=true -H 'Content-Type: application/json' -d ' { "settings" : { "index" : { "number_of_shards" : 5, "number_of_replicas" : 0 } } } '
Create the mapping for the user index and type of profile.
curl -XPUT http://localhost:9200/user/_mapping/profile?pretty=true -H 'Content-Type: application/json' -d ' { "profile" : { "properties" : { "full_name" : { "type" : "string", "store" : true }, "bio" : { "type" : "string", "store" : true }, "age" : { "type" : "integer" }, "location" : { "type" : "geo_point" }, "enjoys_coffee" : { "type" : "boolean" }, "created_on" : { "type" : "date", "format" : "date_time" } } } } '
Now let's insert some data!
curl -XPOST http://localhost:9200/user/profile/1?pretty=true -H 'Content-Type: application/json' -d ' { "full_name" : "Andrew Puch", "bio" : "My name is Andrew. I am an agile DevOps Engineer who is passionate about working with Software as a Service based applications, REST APIs, and various web application frameworks.", "age" : 26, "location" : "41.1246110,-73.4232880", "enjoys_coffee" : true, "created_on" : "2015-05-02T14:45:10.000-04:00" } '
curl -XPOST http://localhost:9200/user/profile/2?pretty=true -d ' { "full_name" : "Elon Musk", "bio" : "Elon Reeve Musk is a Canadian-American entrepreneur, engineer, inventor and investor. He is the CEO and CTO of SpaceX, CEO and product architect of Tesla Motors, and chairman of SolarCity.", "age" : 43, "location" : "37.7749290,-122.4194160", "enjoys_coffee" : false, "created_on" : "2015-05-02T15:45:10.000-04:00" } '
curl -XPOST http://localhost:9200/user/profile/3?pretty=true -H 'Content-Type: application/json' -d ' { "full_name" : "Some Hacker", "bio" : "I am a haxor user who you should end up deleting.", "age" : 1000, "location" : "37.7749290,-122.4194160", "enjoys_coffee" : true, "created_on" : "2015-05-02T16:45:10.000-04:00" } '
Now time to update a record.
curl -XPOST http://localhost:9200/user/profile/1/_update?pretty=true -H 'Content-Type: application/json' -d ' { "doc" : { "location" : "40.7127840,-74.0059410" } } '
We noticed a bad user. Let's delete them.
curl -XDELETE http://localhost:9200/user/profile/3?pretty=true -H 'Content-Type: application/json'
Updating more than one at a time!
curl -XPOST http://localhost:9200/_bulk?pretty=true -H 'Content-Type: application/json' --data-binary @bulk.json
Now let's query that data!
curl -XGET http://localhost:9200/user/profile/_search?pretty=true -H 'Content-Type: application/json' -d ' { "query" : { "query_string" : { "query" : "engineer" } } } '
curl -XGET http://localhost:9200/user/profile/_search?pretty=true -H 'Content-Type: application/json' -d ' { "query" : { "query_string" : { "query" : "investor" } } } '
curl -XGET http://localhost:9200/user/profile/_search?pretty=true -H 'Content-Type: application/json' -d ' { "query" : { "bool" : { "must" : [ { "term" : { "enjoys_coffee" : true } } ] } } } '
curl -XGET http://localhost:9200/user/profile/_search?pretty=true -H 'Content-Type: application/json' -d ' { "query" : { "bool" : { "must" : [ { "term" : { "enjoys_coffee" : false } }, { "range" : { "created_on" : { "gte" : "2015-05-02T15:44:10.000-04:00", "lte" : "2015-05-02T15:46:10.000-04:00" } } } ] } } } '
curl -XGET http://localhost:9200/user/profile/_search?pretty=true -H 'Content-Type: application/json' -d ' { "query" : { "bool" : { "must" : [ { "term" : { "enjoys_coffee" : false } }, { "range" : { "created_on" : { "gte" : "2015-05-02T15:44:10.000-04:00", "lte" : "2015-05-02T15:46:10.000-04:00" } } }, { "range" : { "age" : { "lt" : 44 } } } ] } } } '
curl -XGET http://localhost:9200/user/profile/_search?pretty=true -H 'Content-Type: application/json' -d ' { "query" : { "bool" : { "should" : [ { "term" : { "enjoys_coffee" : false } }, { "range" : { "age" : { "lt" : 30 } } } ] } } } '
Conclusion
Hope that was extremely useful for anyone trying to figure out how to do different things within elasticsearch. I know it is a big struggle learning from the get go. There is great documentation everywhere but no real good step by step solution to putting some data in and getting it out. Hopefully this helped!!!!! If you want to get notified when I post new repositories for tutorials or post new videos on youtube please follow me here and subscribe on youtube. Thanks everyone!