# Query String OR between main type and nested objects

**URL:** https://discuss.elastic.co/t/query-string-or-between-main-type-and-nested-objects/216689
**Category:** Elasticsearch
**Created:** [January 27, 2020, 3:49pm UTC](https://discuss.elastic.co/t/query-string-or-between-main-type-and-nested-objects/216689 "2020-01-27T15:49:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Cristian\_Rodriguez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cristian_rodriguez/32/51130_2.png) [@Cristian\_Rodriguez](https://discuss.elastic.co/u/Cristian_Rodriguez)
#### Post date: [January 27, 2020, 3:49pm UTC](https://discuss.elastic.co/t/query-string-or-between-main-type-and-nested-objects/216689/1 "2020-01-27T15:49:13Z")

</div>

Hi all.  
I'm using ElasticSearch 2.4

I need to create a search query between main object and nested object, if I use the AND condition it works correctly, but the problem is if I try to use OR conditional between main object and nested object:

Please review the code below and tell me if there is a way to make it work using OR conditional.

Create mapping:

```
    PUT /example_contact_purchases
    {
      "mappings": {
        "contact": {
          "dynamic": false,
          "properties": {
            "name": {
              "type": "string"
            },
            "country": {
              "type": "string"
            },
            "purchases": {
              "type": "nested",
              "properties": {
                "uuid":{
                  "type":"string"
                }
              }
            }
          }
        }
      }
    }

```

Mapping result:

```
        GET example_contact_purchases/_mapping
        {
      "example_contact_purchases": {
        "mappings": {
          "contact": {
            "dynamic": "false",
            "properties": {
              "country": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "purchases": {
                "type": "nested",
                "properties": {
                  "uuid": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    }

```

Create First Item:

```
    POST example_contact_purchases/contact
    {
      "name" : "Fran",
      "country": "ES",
      "purchases" : [
        {
          "uuid" : "23"
        }
      ]
    }

```

Create Second Item:

```
    POST example_contact_purchases/contact
    {
      "name" : "Jhon",
      "country": "UK",
      "purchases" : [
        {
          "uuid" : "45"
        }
      ]
    }

```

Create Third Item:

```
    POST example_contact_purchases/contact
    {
      "name" : "Leonardo",
      "country": "IT",
      "purchases" : [
        {
          "uuid" : "45"
        }
      ]
    }

```

Example Query: Country == ES AND purchase.uuid == 23

```
    GET example_contact_purchases/_search
    { 
       "query":{ 
          "filtered":{ 
             "query":{ 
                "query_string":{ 
                   "query":"country:ES"
                }
             }
          }
       },
       "filter":{ 
          "nested":{ 
             "path":"purchases",
             "filter":{ 
                "query":{ 
                   "query_string":{ 
                      "query":"(purchases.uuid:23)"
                   }
                }
             }
          }
       }
    }

```

Result:

```
    {
      "took": 3,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "example_contact_purchases",
            "_type": "contact",
            "_id": "AW_nkURti9zva2kl7ESR",
            "_score": 1,
            "_source": {
              "name": "Fran",
              "country": "ES",
              "purchases": [
                {
                  "uuid": "23"
                }
              ]
            }
          }
        ]
      }
    }

```

Target Query: Country== "ES" OR purchase.uuid== 45

---

<div class="post-metadata">

### Author: ![Cristian\_Rodriguez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cristian_rodriguez/32/51130_2.png) [@Cristian\_Rodriguez](https://discuss.elastic.co/u/Cristian_Rodriguez)
#### Post date: [February 18, 2020, 3:56pm UTC](https://discuss.elastic.co/t/query-string-or-between-main-type-and-nested-objects/216689/2 "2020-02-18T15:56:08Z")

</div>

Answer in **Stackoverflow** : [https://stackoverflow.com/a/59949881/10092054](https://stackoverflow.com/a/59949881/10092054)

---

<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: [March 17, 2020, 4:02pm UTC](https://discuss.elastic.co/t/query-string-or-between-main-type-and-nested-objects/216689/3 "2020-03-17T16:02:25Z")

</div>

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