Boolean queries

    The bool query is a go-to query because it allows you to construct an advanced query by chaining together several simple ones.

    Use the following clauses (subqueries) within the bool query:

    For example, assume you have the complete works of Shakespeare indexed in an OpenSearch cluster. You want to construct a single query that meets the following requirements:

    1. The text_entry field must contain the word love and should contain either life or grace.
    2. The speaker field must not contain ROMEO.
    3. Filter these results to the play Romeo and Juliet without affecting the relevancy score.

    Use the following query:

    1. GET shakespeare/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "match": {
    8. "text_entry": "love"
    9. }
    10. }
    11. ],
    12. "should": [
    13. {
    14. "match": {
    15. "text_entry": "life"
    16. }
    17. },
    18. {
    19. "match": {
    20. "text_entry": "grace"
    21. }
    22. }
    23. ],
    24. "minimum_should_match": 1,
    25. "must_not": [
    26. {
    27. "match": {
    28. "speaker": "ROMEO"
    29. }
    30. }
    31. ],
    32. "filter": {
    33. "term": {
    34. "play_name": "Romeo and Juliet"
    35. }
    36. }
    37. }
    38. }

    Sample output

    1. GET shakespeare/_search
    2. {
    3. "bool": {
    4. "must": [
    5. {
    6. "match": {
    7. "text_entry": {
    8. "query": "love",
    9. "_name": "love-must"
    10. }
    11. }
    12. }
    13. ],
    14. "should": [
    15. {
    16. "match": {
    17. "text_entry": {
    18. "query": "life",
    19. "_name": "life-should"
    20. }
    21. }
    22. },
    23. {
    24. "match": {
    25. "text_entry": {
    26. "query": "grace",
    27. "_name": "grace-should"
    28. }
    29. }
    30. }
    31. ],
    32. "minimum_should_match": 1,
    33. "must_not": [
    34. {
    35. "match": {
    36. "speaker": {
    37. "query": "ROMEO",
    38. "_name": "ROMEO-must-not"
    39. }
    40. }
    41. }
    42. ],
    43. "filter": {
    44. "term": {
    45. "play_name": "Romeo and Juliet"
    46. }
    47. }
    48. }
    49. }

    OpenSearch returns a matched_queries array that lists the queries that matched these results:

    If you remove the queries not in this list, you will still see the exact same result. By examining which should clause matched, you can better understand the relevancy score of the results.

    1. GET shakespeare/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "bool": {
    8. "should": [
    9. {
    10. "match": {
    11. "text_entry": "love"
    12. }
    13. },
    14. {
    15. "match": {
    16. "text": "hate"
    17. }
    18. }
    19. ]
    20. }
    21. },
    22. {
    23. "bool": {
    24. "should": [
    25. {
    26. "match": {
    27. "text_entry": "life"
    28. }
    29. },
    30. {
    31. "match": {
    32. "text": "grace"
    33. }
    34. }
    35. ]
    36. }
    37. }
    38. ],
    39. "filter": {
    40. "term": {
    41. "play_name": "Romeo and Juliet"
    42. }
    43. }
    44. }

    Sample output