ElasticSearch Query - MacKittipat/note-developer GitHub Wiki

Index API

// Get mapping of index
GET /kibana_sample_data_ecommerce/_mapping

// Update index mapping
PUT /kibana_sample_data_ecommerce/_mapping
{
  "properties": {
    "email": {
      "type": "keyword"
    }
  }
}

// Add multi-fields to an existing field
// We can search with `city` (Full text queries) or `city.raw` (Term-level Query) field
PUT /kibana_sample_data_ecommerce/_mapping
{
  "properties": {
    "city": {
      "type": "text",
      "fields": {
        "raw": {
          "type": "keyword"
        }
      }
    }
  }
}

// Delete index 
DELETE /kibana_sample_data_ecommerce

Document API

Create document (Index document)

// Create with specify id
POST /user/_create/1
{
  "name": "A",
  "age": 11
}

// Create with random generate id
POST /user/_doc/
{
  "name": "B",
  "age": 22
}

Get document

// Return only source not including metadata such as _index, _id, _version
GET /user/_source/1

// Return source and metadata
GET /user/_doc/1

// Get multiple documents
GET /user/_mget
{
  "docs": [
    {
      "_id": "1"
    },
    {
      "_id": "2"
    }
  ]
}

Update document

PUT /user/_doc/1
{
  "age": 12,
  "home": "BKK"
}

// Optimistic concurrency control
PUT /user/_doc/1?if_seq_no=0&if_primary_term=1
{
  "age": 13,
  "home": "BKK"
}

Delete document

DELETE /user/_doc/1

Search API

Full text queries


// Match single field
GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "match": {
      "customer_full_name": "Eddie Underwood"
    }
  }
}

GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "match": {
      "products.product_name": "navy"
    }
  }
}

// Match single field
GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "multi_match" : {
      "query":    "this is a test", 
      "fields": [ "subject", "message" ] 
    }
  }
}

Term-level quries

GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "term": {
      "customer_gender": "MALE"
    }
  }
}

GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "wildcard": {
      "customer_full_name.keyword": "Ma*"
    }
  }
}

GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "wildcard": {
      "customer_full_name.keyword": "Ma*"
    }
  },
  "from": 31,
  "size": 10
}