# Flexible search nested documents

**URL:** <https://discuss.elastic.co/t/flexible-search-nested-documents/259802>\
**Category:** Elasticsearch\
**Created:** [December 29, 2020, 1:43pm UTC](https://discuss.elastic.co/t/flexible-search-nested-documents/259802 "2020-12-29T13:43:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maksym1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maksym1/32/78018_2.png) [@Maksym1](https://discuss.elastic.co/u/Maksym1)\
**Post date:** [December 29, 2020, 1:43pm UTC](https://discuss.elastic.co/t/flexible-search-nested-documents/259802/1 "2020-12-29T13:43:52Z")

</div>

Maybe somebody has implemented such a search in nested documents.

Here is my mapping:

```auto
{

    "my-index": {

        "aliases": {},

        "mappings": {

            "properties": {

                "customer": {

                    "type": "nested",

                    "properties": {

                        "id": {

                            "type": "keyword"

                        },

                        "name": {

                            "type": "text"

                        }

                    }

                },

                "id": {

                    "type": "keyword"

                },

                "items": {

                    "type": "nested",

                    "properties": {

                        "id": {

                            "type": "keyword"

                        },

                        "name": {

                            "type": "text"

                        }

                    }

                },

                "number": {

                    "type": "text"

                }

            }

        }

    }

}

```

There is the search text like "Number CustomerName ItemName". But each of the fields is optional.

And here is my query:

```auto
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "number": "5785 SomeCustomerName SomeItemName"
                    }
                },
                {
                    "nested": {
                        "path": "customer",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "match": {
                                            "customer.name": {
                                                "query": "5785 SomeCustomerName SomeItemName",
                                                "operator": "and"
                                            }
                                        }
                                    }
                                ]
                            }
                        },
                        "inner_hits": {}
                    }
                },
                {
                    "nested": {
                        "path": "items",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "match": {
                                            "items.name": {
                                                "query": "5785 SomeCustomerName SomeItemName",
                                                "operator": "and"
                                            }
                                        }
                                    }
                                ]
                            }
                        },
                        "inner_hits": {}
                    }
                }
            ]
        }
    }
}

```

But if the operator is "and" I find nothing. If the operator is "or" I find too many documents where they contain one of these words.  
How do I set up this flexible search?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 29, 2020, 6:37pm UTC](https://discuss.elastic.co/t/flexible-search-nested-documents/259802/2 "2020-12-29T18:37:43Z")

</div>

The “minimum\_should\_match parameter is what you want to look at.  
If an OR requires 1 clause to match and an AND requires all clauses to match this is a way of having a slider between those two extremes.

---

<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:** [January 26, 2021, 6:37pm UTC](https://discuss.elastic.co/t/flexible-search-nested-documents/259802/3 "2021-01-26T18:37:44Z")

</div>

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