Mapper-size plugin

The mapper-size plugin enables the use of the _size field in OpenSearch indexes. The _size field stores the size, in bytes, of each document.

Installing the plugin

You can install the mapper-size plugin using the following command:

  1. ./bin/opensearch-plugin install mapper-size

Examples

After starting up a cluster, you can create an index with size mapping enabled, index a document, and search for documents, as shown in the following examples.

Create an index with size mapping enabled

  1. curl -XPUT example-index -H "Content-Type: application/json" -d '{
  2. "mappings": {
  3. "_size": {
  4. "enabled": true
  5. },
  6. "properties": {
  7. "name": {
  8. "type": "text"
  9. },
  10. "age": {
  11. "type": "integer"
  12. }
  13. }
  14. }
  15. }'

Index a document

  1. curl -XPOST example-index/_doc -H "Content-Type: application/json" -d '{
  2. "name": "John Doe",
  3. "age": 30
  4. }'

Query the index

  1. curl -XGET example-index/_search -H "Content-Type: application/json" -d '{
  2. "query": {
  3. "match_all": {}
  4. },
  5. "stored_fields": ["_size", "_source"]
  6. }'

Query results

In the following example, the _size field is included in the query results and shows the size, in bytes, of the indexed document:

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.0,
  16. "hits": [
  17. {
  18. "_index": "example_index",
  19. "_id": "Pctw0I8BLto8I5f_NLKK",
  20. "_score": 1.0,
  21. "_size": 37,
  22. "_source": {
  23. "name": "John Doe",
  24. "age": 30
  25. }
  26. }
  27. ]
  28. }
  29. }