Bird
0
0

You want to find all documents where the field phone exists but is not empty or null. Which Elasticsearch query best achieves this?

hard🚀 Application Q15 of 15
Elasticsearch - Basic Search Queries
You want to find all documents where the field phone exists but is not empty or null. Which Elasticsearch query best achieves this?
A{"query": {"bool": {"must": [{"exists": {"field": "phone"}}, {"term": {"phone": ""}}]}}}
B{"query": {"bool": {"must": [{"exists": {"field": "phone"}}, {"script": {"script": "doc['phone'].value != ''"}}]}}}
C{"query": {"exists": {"field": "phone"}}}
D{"query": {"exists": {"field": "phone"}, "term": {"phone": ""}}}
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want documents where "phone" exists and is not empty or null.
  2. Step 2: Evaluate each option

    {"query": {"exists": {"field": "phone"}}} only checks existence, not emptiness. {"query": {"bool": {"must": [{"exists": {"field": "phone"}}, {"term": {"phone": ""}}]}}} incorrectly requires phone to be empty. {"query": {"exists": {"field": "phone"}, "term": {"phone": ""}}} has invalid syntax combining exists and term at same level. {"query": {"bool": {"must": [{"exists": {"field": "phone"}}, {"script": {"script": "doc['phone'].value != ''"}}]}}} uses a bool must with exists and a script checking phone is not empty, correctly filtering as needed.
  3. Final Answer:

    {"query": {"bool": {"must": [{"exists": {"field": "phone"}}, {"script": {"script": "doc['phone'].value != ''"}}]}}} -> Option B
  4. Quick Check:

    Exists + script to exclude empty = {"query": {"bool": {"must": [{"exists": {"field": "phone"}}, {"script": {"script": "doc['phone'].value != ''"}}]}}} [OK]
Quick Trick: Combine exists with script to exclude empty values [OK]
Common Mistakes:
MISTAKES
  • Using exists alone without checking empty values
  • Incorrectly combining queries at same level
  • Assuming term query with empty string works for exclusion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Elasticsearch Quizzes