Skip to content

Instantly share code, notes, and snippets.

@peschlowp
Last active August 29, 2015 14:07
Show Gist options
  • Save peschlowp/560411af9bac3be909c0 to your computer and use it in GitHub Desktop.
Save peschlowp/560411af9bac3be909c0 to your computer and use it in GitHub Desktop.
Elasticsearch Sense contents shown in my "Elasticsearch Lessons Learned" talk. Just copy paste into Sense and run the statements top to bottom.
###################################################
###################################################
# Elasticsearch Lessons Learned
###################################################
###################################################
# Delete the "books" index, just in case it is still there from previous experiments.
DELETE /books
# Create an empty "books" index.
PUT /books
# Add a book of type "tech" to the index.
PUT /books/tech/1
{
"author": "Joshua Bloch",
"title": "Effective Java",
"date": "2008-05-08",
"text": "Are you looking for a deeper understanding of the Java programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further!"
}
# Retrieve the book again by ID.
GET /books/tech/1
# Add two more books.
PUT /books/tech/2
{
"author": "Robert C. Martin",
"title": "Clean Code",
"date": "2008-08-01",
"text": "Even bad code can function. But if code isn't clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn't have to be that way."
}
PUT /books/tech/3
{
"author": "Brian Goetz",
"title": "Java Concurrency in Practice",
"date": "2006-05-09",
"text": "Writing correct programs is hard; writing correct concurrent programs is harder."
}
# Search for books with "code" in the text and "java" in the title.
GET /books/tech/_search
{
"query": {
"bool": {
"must": [
{"match": {"text": "code"}},
{"match": {"title": "java"}}
]
}
}
}
# Search for books with "resource" in the text. Won't match because no stemming is applied.
GET /books/tech/_search
{
"query": {
"match": {"text": "resource"}
}
}
# But "resources" does match.
GET /books/tech/_search
{
"query": {
"match": {"text": "resources"}
}
}
# Check the mapping generated by Elasticsearch.
GET /books/_mapping
# Create our own mapping with an English language analyzer for the text field.
DELETE /books
PUT /books
{
"mappings": {
"tech": {
"properties": {
"author": {
"type": "string",
"analyzer": "standard"
},
"title": {
"type": "string",
"analyzer": "simple"
},
"date": {
"type": "date"
},
"text": {
"type": "string",
"analyzer": "english"
}
}
}
}
}
# Reindex the documents via bulk action.
POST /books/tech/_bulk
{"index": {"_id": "1"}}
{"author": "Joshua Bloch", "title": "Effective Java", "date": "2008-05-08", "text": "Are you looking for a deeper understanding of the Java programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further!"}
{"index": {"_id": "2"}}
{"author": "Robert C. Martin", "title":"Clean Code", "date":"2008-08-01", "text":"Even bad code can function. But if code isn't clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn't have to be that way."}
{"index": {"_id": "3"}}
{"author": "Brian Goetz", "title": "Java Concurrency in Practice", "date": "2006-05-09", "text":"Writing correct programs is hard; writing correct concurrent programs is harder."}
# Example with stemming, as we have now configured the English analyzer for the text field.
GET /books/tech/_search
{
"query": {
"match": {"text": "resource"}
}
}
# Highlighting.
GET /books/tech/_search
{
"query": {
"match": {"text": "resource"}
},
"highlight": {
"fields": { "text": {}}
}
}
# Suggestions. Here: May help to fix a typo.
GET /books/tech/_search
{
"query": {
"match": { "text": "claen"}
},
"suggest": {
"my-suggestion-1": {
"text": "claen",
"term": { "field": "text"}
}
}
}
# More like this. Here: Title "Effective Java".
GET /books/tech/_search
{
"query": {
"more_like_this": {
"fields": ["title"],
"ids": ["1"],
"min_term_freq": 1,
"min_doc_freq": 1
}
}
}
# Percolator.
GET /books/.percolator/1
{
"query" : {
"match": {"title" : "java"}
}
}
# Percolate a new document. Note: Indexing would need to be done in a separate call.
GET /books/tech/_percolate
{
"doc": {
"author": ["Charlie Hunt", "Binu John"],
"title": "Java Performance",
"date": "2011-10-14",
"text": "Improvements in the Java platform and new multicore/multiprocessor hardware have made it possible to dramatically improve the performance and scalability of Java software."
}
}
# Aggregation.
GET /books/tech/_search?search_type=count
{
"aggs": {
"year_of_publication": {
"date_histogram": {
"field": "date",
"interval": "year"
}
}
}
}
### Show that the documents live in different shards (head plugin).
###################################################
# Map a field multiple times
###################################################
# Start all over.
DELETE /books
PUT /books
{
"mappings": {
"tech": {
"properties": {
"author": {
"type": "string",
"analyzer": "standard"
},
"title": {
"type": "string",
"analyzer": "simple"
},
"date": {
"type": "date"
},
"text": {
"type": "string",
"analyzer": "english"
}
}
}
}
}
POST /books/tech/_bulk
{"index": {"_id": "1"}}
{"author": "Joshua Bloch", "title": "Effective Java", "date": "2008-05-08", "text": "Are you looking for a deeper understanding of the Java programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further!"}
{"index": {"_id": "2"}}
{"author": "Robert C. Martin", "title":"Clean Code", "date":"2008-08-01", "text":"Even bad code can function. But if code isn't clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn't have to be that way."}
{"index": {"_id": "3"}}
{"author": "Brian Goetz", "title": "Java Concurrency in Practice", "date": "2006-05-09", "text":"Writing correct programs is hard; writing correct concurrent programs is harder."}
# See what a search for "programming" brings.
GET /books/tech/_search
{
"query": {
"match": {"text": "programming"}
},
"highlight": {
"fields": {"text": {}}
}
}
# Map a field twice to improve search precision.
DELETE /books
PUT /books
{
"mappings": {
"tech": {
"properties": {
"author": {
"type": "string",
"analyzer": "standard"
},
"title": {
"type": "string",
"analyzer": "simple"
},
"date": {
"type": "date"
},
"text": {
"type": "string",
"analyzer": "english",
"fields": {
"standard": {
"type": "string",
"analyzer": "standard"
}
}
}
}
}
}
}
POST /books/tech/_bulk
{"index": {"_id": "1"}}
{"author": "Joshua Bloch", "title": "Effective Java", "date": "2008-05-08", "text": "Are you looking for a deeper understanding of the Java programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further!"}
{"index": {"_id": "2"}}
{"author": "Robert C. Martin", "title":"Clean Code", "date":"2008-08-01", "text":"Even bad code can function. But if code isn't clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn't have to be that way."}
{"index": {"_id": "3"}}
{"author": "Brian Goetz", "title": "Java Concurrency in Practice", "date": "2006-05-09", "text":"Writing correct programs is hard; writing correct concurrent programs is harder."}
# Example with higher score for documents that match both fields.
GET /books/tech/_search
{
"query": {
"bool": {
"should": [
{"match": {"text": "programming"}},
{"match": {"text.standard": "programming"}}
]
}
},
"highlight": {
"fields": {"text": {}}
}
}
###################################################
# Filters
###################################################
# Let's say we have a publisher field.
PUT /books/tech/_mapping
{
"tech": {
"properties": {
"publisher": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
# Reindex a book with publisher information.
PUT /books/tech/1
{
"author": "Joshua Bloch",
"title": "Effective Java",
"date": "2008-05-08",
"text": "Are you looking for a deeper understanding of the Java programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further!",
"publisher": "Addison Wesley"
}
# Combine a query with a filter for publisher and a date filter.
GET /books/tech/_search
{
"query": {
"filtered": {
"query": {
"match": {"text": "program"}
},
"filter": {
"bool": {
"must": [
{"term": {"publisher": "Addison Wesley"}},
{"range": {"date": {"gte": "2008-01-01"}}}
]
}
}
}
}
}
###################################################
# Explain
###################################################
# Same query as before but with score explanation.
GET /books/tech/_search?explain
{
"query": {
"bool": {
"should": [
{"match": {"text": "programming"}},
{"match": {"text.standard": "programming"}}
]
}
},
"highlight": {
"fields": {"text": {}}
}
}
###################################################
# Validate
###################################################
# Search for multiple terms.
GET /books/tech/_search
{
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"terms": {
"title": [
"effective",
"java"
]
}
}
}
}
}
# Check explanation.
GET /books/tech/_validate/query?explain
{
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"terms": {
"title": [
"effective",
"java"
]
}
}
}
}
}
# Now with "and" execution mode.
GET /books/tech/_search
{
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"terms": {
"title": [
"effective",
"java"
],
"execution": "and"
}
}
}
}
}
# Check explanation.
GET /books/tech/_validate/query?explain
{
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"terms": {
"title": [
"effective",
"java"
],
"execution": "and"
}
}
}
}
}
###################################################
# Updates
###################################################
# Update the title of a book.
POST /books/tech/2/_update
{
"doc": {
"title": "Clean Code: A Handbook of Agile Software Craftsmanship"
}
}
# Add two comments to a book.
POST /books/tech/1/_update
{
"doc": {
"comments": [
{
"author": "Patrick Peschlow",
"text": "Great book!"
},
{
"author": "Daniel Schneller",
"text": "I don't like it."
}
]
}
}
GET books/tech/1
###################################################
# Relations
###################################################
# See what search for those comments might bring.
GET /books/tech/_search
{
"query": {
"bool": {
"must": [
{"match": {"comments.author": "Patrick Peschlow"}},
{"match": {"comments.text": "I don't like it."}
}
]
}
}
}
# Let's create a new mapping with type "nested" for comments.
DELETE /books
PUT /books
{
"mappings": {
"tech": {
"properties": {
"author": {
"type": "string",
"analyzer": "standard"
},
"title": {
"type": "string",
"analyzer": "simple"
},
"date": {
"type": "date"
},
"text": {
"type": "string",
"analyzer": "english"
},
"comments": {
"type": "nested"
}
}
}
}
}
POST /books/tech/_bulk
{"index": {"_id": "1"}}
{"author": "Joshua Bloch", "title": "Effective Java", "date": "2008-05-08", "text": "Are you looking for a deeper understanding of the Java programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further!"}
{"index": {"_id": "2"}}
{"author": "Robert C. Martin", "title":"Clean Code", "date":"2008-08-01", "text":"Even bad code can function. But if code isn't clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn't have to be that way."}
{"index": {"_id": "3"}}
{"author": "Brian Goetz", "title": "Java Concurrency in Practice", "date": "2006-05-09", "text":"Writing correct programs is hard; writing correct concurrent programs is harder."}
POST /books/tech/1/_update
{
"doc": {
"comments": [
{
"author": "Patrick Peschlow",
"text": "Great book!"
},
{
"author": "Daniel Schneller",
"text": "I don't like it."
}
]
}
}
# Now search again. No result anymore!
GET /books/tech/_search
{
"query": {
"bool": {
"must": [
{"match": {"comments.author": "Patrick Peschlow"}},
{"match": {"comments.text": "I don't like it."}}
]
}
}
}
# Do a nested search.
GET /books/tech/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{"match": {"comments.author": "Patrick Peschlow"}},
{"match": {"comments.text": "Great book!"}}
]
}
}
}
}
}
# Double-check. Shouldn't match the bad combination.
GET /books/tech/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{"match": {"comments.author": "Patrick Peschlow"}},
{"match": {"comments.text": "I don't like it."}}
]
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment