Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How find all documents in elasticsearch that matches with fields and values?

I have an index users with user type. Mapping is dynamic. For now user hs a following structure:

"user": {
        "nested": {
            "one":51
        },
        "other": {
            "two":"hi!"
        },
        "three":false
    }

I need to find all users which has other.two field with value hi or has three field with value false. E.g. user which has other.two must have hi value in it or three field with value false. How to select this from elastic search?

I have tried:

GET /users/user/_search
{"query": {"match": {"other.two": "hi"}}}

Returns a user for me, but

GET /users/user/_search
    {"query": {"match": {"other.two": "hi", "three":false}}}

Returns me a SearchPhaseExecutionException.

How combine several fields and values for searching?

like image 960
Cherry Avatar asked Dec 05 '25 11:12

Cherry


2 Answers

As @rvheddeg suggested above, here is the query that works for me:

{
"query": {
    "bool": {
        "should": [
                { "match": { "other.two":  "hi" }},
                { "match": { "three": false  }}
            ],
            "minimum_should_match": 1
        }
    }
}
like image 184
Bipul Dutta Avatar answered Dec 08 '25 12:12

Bipul Dutta


Use a Bool filter or Bool Query

like image 22
Roeland Van Heddegem Avatar answered Dec 08 '25 10:12

Roeland Van Heddegem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!