limmouyleng
limmouyleng

Reputation: 127

Bool query or filter query in Elastic Search

I have an expression/query : ("text" started_with "hello" OR "text" started_with "hi") OR ("select" is "first_option" AND "correct" is "true" ) . Can you show me the way to use both of bool and filter query correctly in Elastic Search to solve this expression?

Upvotes: 0

Views: 448

Answers (1)

Prabin Meitei
Prabin Meitei

Reputation: 2000

You can achieve the given expression/query using "prefix query" along with boolean query and term query. Try as follows

{
    "bool": {
        "should": [
            {
                "prefix": {
                    "text": {
                        "value": "hello"
                    }
                }
            },
            {
                "prefix": {
                    "text": {
                        "value": "hi"
                    }
                }
            },
            {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "select": [
                                    "first_option"
                                ]
                            }
                        },
                        {
                            "term": {
                                "correct": [
                                    "true"
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    }
}

Upvotes: 2

Related Questions