Home > Back-end >  Query "like" and "or" in Elasticsearch
Query "like" and "or" in Elasticsearch

Time:10-14

I am using elasticsearch for filtering and searching from json file and I am newbie in this technology. So I am little bit confused how to write like query in elasticsearch.

select * from table_name where 'field_name1' like 'a%' or 'field_name2' like 'a%'

This is oracle query. How do I write this query in Elasticsearch? I am using elasticsearch version 6.8.13

CodePudding user response:

You will need to use the Elasticsearch Wildcard query, combined inside a Bool query.
In case you're new, should in Elasticsearch bool query context is pretty much equals to OR in SQL language.

{
    "query": {
        "bool": {
            "should": [
                {
                    "wildcard": {
                        "field_name1": {
                            "value": "a*"
                        }
                    }
                },
                {
                    "wildcard": {
                        "field_name2": {
                            "value": "a*"
                        }
                    }
                }
            ]
        }
    }
}

  • Related