Elasticsearch

)

Terms

System

sequenceDiagram
	participant C as Client
	participant I as Ingest Node
	participant D as Coordination Node
	participant DN1 as Data Node 1
	participant DN2 as Data Node 2

	C ->> I: Send document for indexing
	I -->> I: Process document (Ingest Pipeline)
	I ->> DN1: Forward processed document
	I ->> DN2: Forward process document (replica)
	DN1 -->> I: ACK
	DN2 -->> I: ACK
	I ->> C: Confirm document indexed
	C ->> D: Send search request
	D ->> DN1: Query relevant shards
	D ->> DN2: Query relevant shards
	DN1 -->> D: Return partial results
	DN2 -->> D: Return partial results
	D -->> D: Merge results
	D -->> C: Return final results

Operation

// PUT /books
{
  "settings": {
	  "number_of_shards": 1,
	  "number_of_replicas": 1
  }
}
// PUT /books/_mapping
{
	"properties": {
		"title": { "type": "text" },
		"author": { "type": "text" },
		"price": { "type": "float" },
		"reviews": {
			"type": "nested",
			"properties": {
				"user": { "type": "keyword" },
				"rating": { "type": "integer" }
			}
		}
	}
}
// POST /books/_doc
{
	"title": "ABC",
	"author": "unknown",
	"price": 9.99,
	"reviews": [
		{
			"user": "reader1",
			"rating": 0
		},
		{
			"user": "reader2",
			"rating": 4
		}
	]
}
// Result
{
	"_index": "books",
	"_id": "Kqawoeiru234asdkj",
	"_version": 1,
	"result": "created",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 0,
	"_primary_term": 1
}
// PUT /books/_doc/Kqawoeiru234asdkj?version=1
{
	"title": "ABC",
	"author": "unknown",
	"price": 13.99,
	"reviews": [
		{
			"user": "reader1",
			"rating": 0
		},
		{
			"user": "reader2",
			"rating": 4
		}
	]
} 
// POST /books/_update/Kqawoeiru234asdkj
{
	"doc": {
		"price": 14.99
	}
}
// GET /books/_search
{
	"query": {
		"match": {
			"title": "ABC"
		}
	}
}

// GET /books/_search
{
	"query": {
		"bool": {
			"must": [
				{ "match": { "title": "ABC" } },
				{ "match": { "price": { "lte": 15 } } }
			]
		}
	}
}

// Result
{
	"took": 7,
	"timed_out": false,
	"_shards": {
		"total": 5,
		"successful": 5,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 2.18023,
		"hits": [
			{
				"_index": "books",
				"_type": "_doc",
				"_id": 1,
				"_score": 2.18023,
				"_source": {
					"title": "ABC",
					"author": "unknown",
					"price": 12.99
				}
			}
		]
	},
	
	
}
// GET /books/_search
{
	"sort": [
		{ "price": "asc" },
		{ "publish_date": "desc" }
	],
	"query"": ...
}

// GET /books/_search
{
	"sort": [
		{
			"_script": {
				"type": "number",
				"script": {
					"source": "doc['price'].value = 0.9"
				},
				"order": "asc"
			}
		}
	],
	...
}

// GET /books/_search
{
	"sort": [
		{
			"reviews.rating": {
				"order": "desc",
				"mode": "max",
				"nested": {
					"path": "reviews"
				}
			}
		}
	],
	...
}

Scoring

TF-IDF - Term Frequency and Inverse Document frequency

Pagination

// GET /books/_search
{
	"from": 0,
	"size": 10,
	"query": ...
}

// GET /books/_search
{
	"size": 10,
	"query": ...,
	"sort": [
		{ "date": "desc" },
		{ "_id": "desc" }
	],
	"search_after": [1463538857, "654323"]
}
// POST /books/_pit?keep_alive=1m
// return an id: "46To..."

// GET /_search
{
	"size": 10,
	"query": {
		"match": {
			"title": "ABC"
		}
	},
	"pit": {
		"id": "46To...",
		"keep_alive": "1m"
	},
	"sort": [
		{"_score": "desc"},
		{"_id": "asc"}
	]
}

// DELETE /_pit
// remember to delete the point-in-time snapshot to avoid excessive memory allocation
{
	"id": "46To..."
}

Considerations

Issues