Match query with only 2 word search in search query

Document 1:

{
    "id": 1,
    "title": "marble flooring"
}

Document 2:

{
    "id": 2,
    "title": "flooring marble"
}

Document 3:

{
    "id": 3,
    "title": "italian marble flooring"
}

Existing query which am using

Query:

{
    "track_total_hits": true,
    "_source": [
        "title"
    ],
    "query": {
        "bool": {
            "should": [
               
                {
                    "match_phrase": {
                        "title.standard": {
                            "query": "marble floor",
                            "boost": 1
                        }
                    }
                }
            ],
            "minimum_should_match": "1"
        }
    }
}

but this above query returning all 3 documents, but I need only document 1 & document 2, guide me with the same

As far as I understand, you want to restrict the search to only documents where the field contains exactly two words, like “marble flooring”. Unfortunately, there isn’t a direct way to enforce this restriction within a multi_match or match_phrase query.

To check if the field contains exactly two words:

  1. You can use a regular expression in the Painless script.
  2. You can calculate the length of the field value during indexing.
1 Like