elastic
GET ?q=post_text:wonderful
Get containing wonderful and blog seperately
GET my_blog/post/_search
{
"query" : {
"match" : {
"post_text" : "wonderful blog"
}
}
}
Get containing "wonderful blog"
GET my_blog/post/_search
{
"query" : {
"match_phrase" : {
"post_text" : "wonderful blog"
}
}
}
GET my_blog/post/_search
{
"query" : {
"filtered" : {
"query" : {
"match" : {
"post_text" : "blog"
}
}
}
},
"filter" : {
"range" : {
"post_date" : {
"gt" : "2014-09-16"
}
}
}
}
Add after query:
"highlight" : {
"fields" : {
"field_name" : {}
}
}
e.g.
Counts the words in a field1 Add after query:
"aggs" : {
"all_words" : {
"terms" : {
"field" : "field1"
}
}
}
Average word count for the field2 Add after query:
"aggs" : {
"avg_word_count" : {
"avg" : {
"field" : "field2"
}
}
}
Cluster Node (server) Index (db) Shard (blocks of data)
List indexes:
GET _cat/indices
List indexes w column headers:
GET _cat/indices?v
Create index:
POST /my_blog
{
"mappings" : {
"post" : {
"properties" : {
"user_id" : {
"type" : "integer"
},
"post_text" : {
"type" : "string"
},
"post_date" : {
"type" : "date"
}
}
}
}
}
Create index w shard option:
POST /my_blog
{
"settings" : {
"index" : {
"number_of_shards" : 5
}
},
"mappings" : {
"post" : {
"properties" : {
"user_id" : {
"type" : "integer"
},
"post_text" : {
"type" : "string"
},
"post_date" : {
"type" : "date",
"format" : "YYYY-MM-DD"
}
}
}
}
}
Create index without storing all of the fields inside a doc
POST /my_blog
{
"mappings" : {
"post" : {
"_source" : {
"enabled" : false
},
"properties" : {
"user_id" : {
"type" : "integer",
"store" : true
},
"post_text" : {
"type" : "string"
},
"post_date" : {
"type" : "date"
}
}
}
}
}
Get mappings (kinda columns) of the index
GET /my_blog/_mapping
Push new document
POST my_blog/post
{
"post_date" : "2014-08-20",
"post_text" : "This is a real blogpost",
"user_id" : 1
}
Push new document by defining the id:
POST my_blog/post/1
{
"post_date" : "2014-08-23",
"post_text" : "This is post with id 1",
"user_id" : 2
}
Basic Search:
GET /my_blog/post/_search
Search w fields:
GET /my_blog/post/1?fields=user_id, post_text
Delete Index:
DELETE my_blog
Define an alias
POST _aliases
{
"actions" : [
{
"add" : {
"index" : "eventlog-date1", "alias" : "eventlog"
}
}
]
}
Examples for built in:
Default. Good for general purpose Breaks up text strings by natural word boundaries, takes away punctuation, and lower cases terms
Breaks up text by whitespace only. More useful for strings like computer code or logs.
Breaks up strings by anything that is not a number, lowercases terms.
Whitespace can be standard or other.
POST /_analyze?analyzer=whitespace
Body = test string