loretoparisi
loretoparisi

Reputation: 16301

combine query_string and must in elastic search node.js client

I'm doing a search query but combining "query_string" with "bool" because I want to match items that must the tags array field and that must match some query like entity.color:green, so I would do like

{
    body: {
        "query": {
            "bool": {
                "must": [{
                        "exists": {
                            "field": "tags"
                        }
                    },
                    {
                        "query_string": {
                            "query": "entity.color:green"
                        }
                    },
                    {
                        "match": {
                            "entity.color": "green"
                        }
                    }
                ],
                "must_not": [{
                        "term": {
                            "tags": ""
                        }
                    },
                    {
                        "term": {
                            "tags": "[]"
                        }
                    }
                ]
            }
        }
    },
    size: 10,
    index: myIndex
}

but I do not get any result. The query without the query_string return the right items filtered, but when adding it no. I'm running latest version of elastic search node.js client 15.2.0

I have also tried the nested bool/must approach but it does not work

{
    "bool": {
        "must": [{
                "exists": {
                    "field": "tags"
                }
            },
            {
                "bool": {
                    "must": [{
                            "query_string": {
                                "query": "entity.color:green"
                            }
                        },
                        {
                            "match": {
                                "entity.color": "green"
                            }
                        }
                    ]
                }
            }
        ],
        "must_not": [{
                "term": {
                    "tags": ""
                }
            },
            {
                "term": {
                    "tags": "[]"
                }
            }
        ]
    }
}

Upvotes: 0

Views: 494

Answers (1)

Wafia Mustafa
Wafia Mustafa

Reputation: 33

try writing query string like

query_string: { field: 'color', query: 'green' }

Upvotes: 1

Related Questions