This version of the OpenSearch documentation is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.

Exists query

Use the exists query to search for documents that contain a specific field.

An indexed value will not exist for a document field in any of the following cases:

  • The field has "index" : false specified in the mapping.
  • The field in the source JSON is null or [].
  • The length of the field value exceeds the ignore_above setting in the mapping.
  • The field value is malformed and ignore_malformed is defined in the mapping.

An indexed value will exist for a document field in any of the following cases:

  • The value is an array that contains one or more null elements and one or more non-null elements (for example, ["one", null]).
  • The value is an empty string ("" or "-").
  • The value is a custom null_value, as defined in the field mapping.

Example

For example, consider an index that contains the following two documents:

  1. PUT testindex/_doc/1
  2. {
  3. "title": "The wind rises"
  4. }

copy

  1. PUT testindex/_doc/2
  2. {
  3. "title": "Gone with the wind",
  4. "description": "A 1939 American epic historical film"
  5. }

copy

The following query searches for documents that contain the description field:

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "exists": {
  5. "field": "description"
  6. }
  7. }
  8. }

copy

The response contains the matching document:

  1. {
  2. "took": 3,
  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,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "2",
  20. "_score": 1,
  21. "_source": {
  22. "title": "Gone with the wind",
  23. "description": "A 1939 American epic historical film"
  24. }
  25. }
  26. ]
  27. }
  28. }

Finding documents with missing indexed values

To find documents with missing indexed values, you can use the must_not Boolean query with the inner exists query. For example, the following request searches for documents in which the description field is missing:

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must_not": {
  6. "exists": {
  7. "field": "description"
  8. }
  9. }
  10. }
  11. }
  12. }

copy

The response contains the matching document:

  1. {
  2. "took": 19,
  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": 0,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": 0,
  21. "_source": {
  22. "title": "The wind rises"
  23. }
  24. }
  25. ]
  26. }
  27. }

Parameters

The query accepts the name of the field (<field>) as a top-level parameter.