TL;DR

  • Scenario: ES zero foundation getting started, quickly complete index creation, document CRUD, search query
  • Conclusion: ES getting started simple, minimum example can run through in 3-10 minutes
  • Output: Complete CRUD operation examples, architecture concepts, error quick reference

ES Simple Usage

Create Index

PUT http://h121.wzk.icu:9200/wzk_blog01/?pretty

Return result:

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "wzk_blog01"
}

Insert Document

POST http://h121.wzk.icu:9200/wzk_blog01/_doc/1?pretty
{"id": "1", "title": "What is lucene"}

Return result:

{
    "_index": "wzk_blog01",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

Query Document

GET http://h121.wzk.icu:9200/wzk_blog01/_doc/1?pretty

Return result:

{
  "_index": "wzk_blog01",
  "_id": "1",
  "_version": 3,
  "_seq_no": 2,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "id": "1",
    "title": "Apache Spark is a unified analytics engine for large-scale data processing"
  }
}

Update Document

POST http://h121.wzk.icu:9200/wzk_blog01/_doc/1?pretty
{"id": "1", "title": " What is elasticsearch"}

Return result:

{
  "_index": "wzk_blog01",
  "_id": "1",
  "_version": 4,
  "result": "updated",
  "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}

Search Document

POST http://h121.wzk.icu:9200/wzk_blog01/_search?pretty
{"query": {"match": {"title": "What"}}}

Return result:

{
  "query": {
    "match": {
      "title": "What"
    }
  }
}

Architecture and Concepts

Basic Introduction

Elasticsearch is a document-oriented distributed search and analysis engine, it can store and manage data as documents.

Comparison with Relational Database

ElasticsearchMySQL
IndexDatabase
Type (removed in 7.x)Table
DocumentRow record
FieldColumn

Core Concepts

ConceptDescription
IndexCollection of documents with similar characteristics
TypeLogical classification/partition of index (removed after 7.x)
FieldTable field
MappingDefines each field’s type (schema)
DocumentBasic information unit that can be indexed (JSON)
Near Real-time NRTSlight delay from indexing document to searchable (usually within 1 second)
ClusterMultiple nodes compose, identified by cluster name
NodeOne ES instance
ShardHorizontal partitioning of index (primary shard + replica shard)
ReplicaCopy of primary shard, improves recovery capability and performance

Error Quick Reference

SymptomRoot CauseFix
?pertty invalid/errorParameter spelling errorChange to ?pretty
Create index invalidUsed GET to access /indexUse PUT /{index}?pretty
Insert document 405/400Used GET to send JSONUse POST/PUT /{index}/_doc/{id} and put JSON in request body
/{index}/_doc/_search/1 returns errorEndpoint wrongQuery by ID: GET /{index}/_doc/{id}; search: POST /{index}/_search
Search no resultsPut query DSL in “return result” instead of request bodyPut DSL in request body, POST /_search
HTTP/HTTPS mixed or CORShttp/https mixed under same domainUnify protocol; if browser, configure CORS/reverse proxy
401/403 Unauthorized8.x enables security by defaultEnable built-in user/API Key, or authenticate via intranet/reverse proxy
index_not_found_exceptionIndex not created/name wrongFirst PUT /{index} then write; check lowercase naming
version_conflict_engine_exceptionConcurrent updateUse _update or optimistic locking with if_seq_no/if_primary_term
Cluster Red/shard allocation failedShard/disk issueFree up disk, fix shards, adjust replica count