# Phrase\_query for multiple should and must

**URL:** <https://discuss.elastic.co/t/phrase-query-for-multiple-should-and-must/309998>\
**Category:** Elasticsearch\
**Created:** [July 19, 2022, 9:44am UTC](https://discuss.elastic.co/t/phrase-query-for-multiple-should-and-must/309998 "2022-07-19T09:44:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![puff2fakie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/puff2fakie/32/108499_2.png) [@puff2fakie](https://discuss.elastic.co/u/puff2fakie)\
**Post date:** [July 19, 2022, 9:44am UTC](https://discuss.elastic.co/t/phrase-query-for-multiple-should-and-must/309998/1 "2022-07-19T09:44:57Z")

</div>

I've indexed my documents as follows:

```auto
doc = {
    "_index": index_name,
    "_source": {
        "field": "bm25",
        "passage": passage,
        "passage_id": pid,
        "level1": level1,
        "level2": level2,
        "level3": level3,
        "level4": level4
    },
    "_op_type": "index"
}

```

With the ES index, I'm trying to retrieve relevant documents using a passage as a query (linking a passage to relevant passages). However, on top of the input query passage being closely matched with the passage field from the indexed documents, I'd like to consider level1~4 fields as well.

Given the q`uery_passage`, `query_level1`, `query_level2`, `query_level3`, and `query_level4`, I want to retrieve documents that share similar passages but also share the same level1~4.

I've wrote phrase\_query as below, but it's giving me `opensearchpy.exceptions.RequestError: RequestError(400, 'parsing_exception', 'unknown query [query]')` error message.

```auto
phrase_query = {
    "query": {
        "bool": {
            "should": [
                {"multi_match":
                     {"query": query_passage,
                      "fields": ["passage"]
                      }
                 }
            ],
            "must": [
                {
                    "match": {
                        "level1": {
                            "query": query_level1
                        }
                    }
                },
                {
                    "match": {
                        "level2": {
                            "query": query_level2
                        }
                    }
                },
                {
                    "match": {
                        "level3": {
                            "query": query_level3
                        }
                    }
                },
                {
                    "match": {
                        "level4": {
                            "query": query_level4
                        }
                    }
                }
            ]
        }
    }
}

```

I want query levels to exactly match levels and auery\_passage to be the ingredient where BM25 score is calculated w.r.t the `passage` field. Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 16, 2022, 9:45am UTC](https://discuss.elastic.co/t/phrase-query-for-multiple-should-and-must/309998/2 "2022-08-16T09:45:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
