Skip to content

Instantly share code, notes, and snippets.

@bbelyeu
Created October 27, 2015 20:18
Show Gist options
  • Save bbelyeu/9033dc65991cf9b4d502 to your computer and use it in GitHub Desktop.
Save bbelyeu/9033dc65991cf9b4d502 to your computer and use it in GitHub Desktop.
# Create index
curl -XPUT 'http://localhost:9200/events/' | pjson
# Create explicit mapping
curl -XPUT 'http://localhost:9200/events/_mapping/event' -d '
{
"event" : {
"properties" : {
"event_id": {
"type": "integer",
"store": true
},
"title" : {
"type" : "string",
"store" : true
},
"description" : {
"type" : "string",
"store" : true
},
"organization_name" : {
"type" : "string",
"store" : true
},
"location_name" : {
"type" : "string",
"store" : true
},
"start_dt" : {
"type" : "date",
"format" : "yyyy-MM-dd HH:mm:ss",
"store" : true
},
"end_dt" : {
"type" : "date",
"format": "yyyy-MM-dd HH:mm:ss",
"store" : true
},
"location": {
"type": "geo_point",
"lat_lon": true,
"geohash": true,
"geohash_precision": "1m"
}
}
}
}
' | pjson
# Create a document
curl -XPOST 'http://localhost:9200/events/event/' -d '
{
"event_id" : 1,
"title" : "Brads Church Event",
"description" : "Come hear me preach about ElasticSearch",
"organization_name" : "Brad.Church",
"location_name": "Brads House",
"start_dt": "2015-10-22 10:00:00",
"end_dt": "2015-10-22 11:00:00",
"location": "35.652899,-97.477899"
}
' | pjson
# Get a document
curl -XGET 'http://localhost:9200/events/_search' -d '
{
"query" : {
"match" : {
"event_id": 1
}
}
}
' | pjson
# Update a docuement
curl -XPOST 'http://localhost:9200/events/event/AVCWrJviTcMGuCZlvu1L/_update' -d '
{
"doc" : {
"location": "35.652899,-97.477899",
"start_dt": "2015-10-29 10:00:00",
"end_dt": "2015-10-29 11:00:00"
}
}
' | pjson
# SQL query to get data to seed ES
select row_to_json(e.*) from
(
select events.id as event_id, title, description, subtitle as organization_name, name as location_name, geo_latitude||', '||geo_longitude as location, events.start as start_dt, events.end as end_dt
from events
inner join group_locations
on events.group_id = group_locations.group_id
where events.start >= NOW()
) e;
# Bulk add
curl -s -XPOST localhost:9200/_bulk --data-binary "@import_data"; echo
# Search for results
curl -XGET 'http://localhost:9200/events/event/_search' -d '
{
"sort" : [
{
"_geo_distance" : {
"location" : {
"lat" : 35.652899,
"lon" : -97.477899
},
"order" : "asc",
"unit" : "km"
}
}
],
"query": {
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "20km",
"location" : {
"lat" : 35.652899,
"lon" : -97.477899
}
}
}
}
}
}
' | pjson
# Search with date range
curl -XGET 'http://localhost:9200/events/event/_search' -d '
{
"query": {
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"range" : {
"start_dt" : {
"gte": "2015-10-27 00:00:00"
}
}
}
}
}
}
# Search with BOTH geo & date range
{
"sort" : [
{
"_geo_distance" : {
"location" : {
"lat" : 35.652899,
"lon" : -97.477899
},
"order" : "asc",
"unit" : "km"
}
}
],
"query": {
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"bool" : {
"must": [
{
"range": {
"start_dt": {
"gte": "2015-10-27 00:00:00"
}
}
},
{
"range": {
"end_dt": {
"lte": "2015-11-30 00:00:00"
}
}
},
{
"geo_distance" : {
"distance" : "200km",
"location" : {
"lat" : 35.652899,
"lon" : -97.477899
}
}
}
]
}
}
}
}
}
# Full text search to AND match:
GET /events/event/_search
{
"query": {
"bool": {
"must": [
{"match": {"title": "Church"}},
{"match": {"description": "ElasticSearch"}},
{"match": {"organization_name": "Brad.Church"}},
{"match": {"location_name": "Brads House"}}
]
}
}
}
# Only 1 result
# Full text search to match ANY:
GET /events/event/_search
{
"query": {
"bool": {
"should": [
{"match": {"title": "Church"}},
{"match": {"description": "ElasticSearch"}},
{"match": {"organization_name": "Brad.Church"}},
{"match": {"location_name": "Brads House"}}
]
}
}
}
# 68 results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment