Flexible search nested documents

Maybe somebody has implemented such a search in nested documents.

Here is my mapping:

{

    "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:

{
    "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?

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.

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