skynyrd
2/12/2017 - 11:40 AM

elastic

elastic

Basic Query:
GET ?q=post_text:wonderful
DSL

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"
    }
  }
}
Filtering
GET my_blog/post/_search
{
  "query" : {
    "filtered" : {
      "query" : {
        "match" : {
          "post_text" : "blog"
        }
      }
    }
  },
  "filter" : {
    "range" : {
      "post_date" : {
        "gt" : "2014-09-16"
      }
    }
  }
}

http://stackoverflow.com/questions/26001002/elastic-search-difference-between-term-match-phrase-and-query-string

Highlight

Add after query:

"highlight" : {
  "fields" : {
    "field_name" : {}
  }
}
Aggregation

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"
      }
    }
    ]
}
  • Comes w a lot of build in analyzers
  • You can even make your own

Examples for built in:

Standard

Default. Good for general purpose Breaks up text strings by natural word boundaries, takes away punctuation, and lower cases terms

Whitespace

Breaks up text by whitespace only. More useful for strings like computer code or logs.

Simple

Breaks up strings by anything that is not a number, lowercases terms.

Analyze API

Whitespace can be standard or other.

POST /_analyze?analyzer=whitespace
Body = test string