# Or query in ES

**URL:** <https://discuss.elastic.co/t/or-query-in-es/82158>\
**Category:** Elasticsearch\
**Created:** [April 12, 2017, 12:17pm UTC](https://discuss.elastic.co/t/or-query-in-es/82158 "2017-04-12T12:17:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![E.Eric](https://avatars.discourse-cdn.com/v4/letter/e/dec6dc/32.png) [@E.Eric](https://discuss.elastic.co/u/E.Eric)\
**Post date:** [April 12, 2017, 12:17pm UTC](https://discuss.elastic.co/t/or-query-in-es/82158/1 "2017-04-12T12:17:24Z")

</div>

Hello,

Given a couple` <key,value>` I want to create a query to find documents where a nested field contains the couple ` <key,value>` .

The structure is like this:

> "f1":{  
> "key":"K1",  
> "value","V1"},  
> "f2":{  
> "key":"K2",  
> "value","V2"},  
> "f3":{  
> "key":"K3",  
> "value","V3"}

I've created this query for this couple ` <Key1,Value1>`:

> GET myIndex/environment/\_search  
> {  
> "query": {  
> "nested": {  
> "path" : "f1",  
> "query": {  
> "bool": {  
> "must": [{  
> "match": {  
> "f1.key": "Key1"  
> }  
> },  
> {"match": {  
> "f1.value": "Value1"  
> }  
> }]  
> }  
> }  
> }  
> }  
> }

It searches only in f1, I want to khnow if f1 or f2 or f3 contains that couple in one query.

Thanks fo help.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 12, 2017, 12:51pm UTC](https://discuss.elastic.co/t/or-query-in-es/82158/2 "2017-04-12T12:51:58Z")

</div>

I think you want `should` inside of `bool`. Also, I _think_ you want nested queries inside the `should`, but I'm not exactly sure what kind of search you are going for.

---

<div class="post-metadata">

**Author:** ![E.Eric](https://avatars.discourse-cdn.com/v4/letter/e/dec6dc/32.png) [@E.Eric](https://discuss.elastic.co/u/E.Eric)\
**Post date:** [April 12, 2017, 1:03pm UTC](https://discuss.elastic.co/t/or-query-in-es/82158/3 "2017-04-12T13:03:57Z")

</div>

I want to return document if one of f1 or f2 or f3 fileds matchs the couple `<Key1,Value1>` . I'm newbie in ES and can't create such query.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 12, 2017, 1:21pm UTC](https://discuss.elastic.co/t/or-query-in-es/82158/4 "2017-04-12T13:21:37Z")

</div>

To be honest nested makes you life harder as a newbie even if it is what you want in the end. Are you sure you need it yet? Without nested I'd reach for [multimatch](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html). With nested you'll write much more json.

---

<div class="post-metadata">

**Author:** ![E.Eric](https://avatars.discourse-cdn.com/v4/letter/e/dec6dc/32.png) [@E.Eric](https://discuss.elastic.co/u/E.Eric)\
**Post date:** [April 12, 2017, 1:26pm UTC](https://discuss.elastic.co/t/or-query-in-es/82158/5 "2017-04-12T13:26:54Z")

</div>

The problem without nested I can't match f1.key and f1.vlaue.. I should add the path so I need to use nested.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 12, 2017, 1:36pm UTC](https://discuss.elastic.co/t/or-query-in-es/82158/6 "2017-04-12T13:36:31Z")

</div>

Right. In that case it looks more like:

```auto
GET /_search
{
  "query": {
    "bool": {
      "should": [
        "nested" : {
            "path" : "f1",
            "query" : {
                "bool" : {
                    "must" : [
                      { "match" : {"f1.key" : "blue"} },
                      { "match" : {"f1.value" : "bar"} }
                    ]
                }
            }
        },
        "nested" : {
            "path" : "f2",
            "query" : {
                "bool" : {
                    "must" : [
                      { "match" : {"f2.key" : "blue"} },
                      { "match" : {"f2.value" : "bar"} }
                    ]
                }
            }
        },
        "nested" : {
            "path" : "f3",
            "query" : {
                "bool" : {
                    "must" : [
                      { "match" : {"f3.key" : "blue"} },
                      { "match" : {"f3.value" : "bar"} }
                    ]
                }
            }
        }
    }
}

```

But remember the whole point of nested is so that f1 can have multiple values and you can correlate them. Unless you really need f1, f2, and f3 to be separate fields for some other reason, it is worth looking at combining them.

---

<div class="post-metadata">

**Author:** ![E.Eric](https://avatars.discourse-cdn.com/v4/letter/e/dec6dc/32.png) [@E.Eric](https://discuss.elastic.co/u/E.Eric)\
**Post date:** [April 12, 2017, 1:57pm UTC](https://discuss.elastic.co/t/or-query-in-es/82158/7 "2017-04-12T13:57:01Z")

</div>

It works, Thank you very much !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 10, 2017, 2:04pm UTC](https://discuss.elastic.co/t/or-query-in-es/82158/8 "2017-05-10T14:04:33Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
