# Query with AND operator across inner hits of a document?

**URL:** https://discuss.elastic.co/t/query-with-and-operator-across-inner-hits-of-a-document/347175
**Category:** Elasticsearch
**Created:** [November 14, 2023, 11:40pm UTC](https://discuss.elastic.co/t/query-with-and-operator-across-inner-hits-of-a-document/347175 "2023-11-14T23:40:46Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![cphramington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cphramington/32/127566_2.png) [@cphramington](https://discuss.elastic.co/u/cphramington)
#### Post date: [November 14, 2023, 11:40pm UTC](https://discuss.elastic.co/t/query-with-and-operator-across-inner-hits-of-a-document/347175/1 "2023-11-14T23:40:46Z")

</div>

I'm currently using a query like this to prioritize hits that contain multiple terms from the query string over those that contain fewer.

```auto
                     "nested": {
                          "path": "my_nested_field",
                          "query": {
                                "bool": {
                                    "should": [
                                        {
                                            "query_string": {
                                                "query": query_str,
                                                "default_operator": "AND"
                                            },
                                        },
                                        {
                                            "query_string": {
                                                "query": query_str,
                                                "boost": 0.5
                           }}]}}}

```

I'd like the scoring to reflect:

1. Hits that contain the all of the query string terms in the same "my\_nested\_field"
2. Hits that contain the all of the query string terms across all nested "my\_nested\_field"s in the same document
3. Hits that contain fewer than all of the query string terms

I know that BM25 doesn't take into account number of individual terms matched in the document, which makes this a tough problem, but are there any sort of other heuristics to maximize scores for documents whose inner hits contain the highest number of found search terms?

---

<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: [December 12, 2023, 11:41pm UTC](https://discuss.elastic.co/t/query-with-and-operator-across-inner-hits-of-a-document/347175/2 "2023-12-12T23:41:24Z")

</div>

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