Cherry
Cherry

Reputation: 33618

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?

Upvotes: 3

Views: 4187

Answers (2)

Bipul Dutta
Bipul Dutta

Reputation: 74

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
        }
    }
}

Upvotes: 3

Roeland Van Heddegem
Roeland Van Heddegem

Reputation: 1735

Use a Bool filter or Bool Query

Upvotes: 2

Related Questions