Home > Blockchain >  how to search case condition in elastic search
how to search case condition in elastic search

Time:12-09

If there are age and name fields

When I look up here, if the name is A, the age is 30 or more, and the others are 20 or more. In this way, I want to give different conditions depending on the field value.

Does es provide this function? I would like to know which keywords to use.

You may or may not be able to tell us how to use it with QueryBuilders provided by Spring.

Thank you.

CodePudding user response:

select * from people
where (name = 'a' and age >=30) or age >=20

This site can convert sql to esdsl try this

{
    "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "must": [
                            {
                                "match_phrase": {
                                    "name": {
                                        "query": "a"
                                    }
                                }
                            },
                            {
                                "range": {
                                    "age": {
                                        "from": "30"
                                    }
                                }
                            }
                        ]
                    }
                },
                {
                    "range": {
                        "age": {
                            "from": "20"
                        }
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 1
}
  • Related