Multi Match Query for multiple words with operator AND


(pafheuch) #1

My scenario is that in my application there is an inline search just like the one we have here on Udemy site's header bar and the user can type more than one word in it. Now, I want to use that multi word search text entered by user to be queried against multi fields.

Multi Fields against which I am querying have the following mapping

_mapping
{
"category": {
"type": "keyword"
},
"designers": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
}
}
},
"story": {
"type": "text"
},
"foundryName": {
"type": "text",
}
}

My problem here is how can I do a multi word search like "designerFirstName1 category1 foundryName1" and get results where the matched document has each word from any one of the multifields I am searching in also as I continue to add more words the result set should get reduced.

Query which I am trying

{
    "query": {
        "bool": {
            "should": [
                {
                    "nested": {
                        "path": "designers",
                        "query": {
                            "match": {
                                "designers.name": {
                                    "query": "designerFirstName1 category1 foundryName1",
                                    "fuzziness": "auto"
                                }
                            }
                        }
                    }
                },
                {
                    "multi_match": {
                        "query": "designerFirstName1 category1 foundryName1",
                        "type": "cross_fields",
                        "fields": [
                            "story",
                            "foundryName",
                            "category",
                        ]
                    }
                }
            ],
            "minimum_should_match": 1
        }
    }
}

Expected Result is that this kind of document should be higher and then as we go down the results start having not all the multiwords in any one of the field(as shown below)

{
    "category": [
        "category1",
        "category2"
    ],
    "designers": [
        {
            "name": "designerFirstName1 designerLastName1"
        },
        {
            "name": "designerFirstName2 designerLastName2"
        }
    ],
    "story": "Sphinx of black quartz, judge my vow! Sex-charged fop blew my junk TV quiz.",
    "foundryName": "foundryName1"
},
{
    "category": [
        "category2", /*changed here comparing with the above document*/
        "category3"
    ],
    "designers": [
        {
            "name": "designerFirstName1 designerLastName1"
        },
        {
            "name": "designerFirstName2 designerLastName2"
        }
    ],
    "story": "Sphinx of black quartz, judge my vow! Sex-charged fop blew my junk TV quiz.",
    "foundryName": "foundryName1"
},
{
    "category": [
        "category1",
        "category3"
    ],
    "designers": [
        {
            "name": "designerFirstName3 designerLastName1" /*changed here comparing with the above document*/
        },
        {
            "name": "designerFirstName2 designerLastName2"
        }
    ],
    "story": "Sphinx of black quartz, judge my vow! Sex-charged fop blew my junk TV quiz.",
    "foundryName": "foundryName1"
},
{
    "category": [
        "category2",
        "category3"
    ],
    "designers": [
        {
            "name": "designerFirstName3 designerLastName1" /*changed here comparing with the above document*/
        },
        {
            "name": "designerFirstName2 designerLastName2"
        }
    ],
    "story": "Sphinx of black quartz, judge my vow! Sex-charged fop blew my junk TV quiz.",
    "foundryName": "foundryName1"
},

(system) #2

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