# Matching fields in a if-then manner

**URL:** <https://discuss.elastic.co/t/matching-fields-in-a-if-then-manner/86670>\
**Category:** Elasticsearch\
**Created:** [May 22, 2017, 1:05pm UTC](https://discuss.elastic.co/t/matching-fields-in-a-if-then-manner/86670 "2017-05-22T13:05:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sumaya\_Baig](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@Sumaya\_Baig](https://discuss.elastic.co/u/Sumaya_Baig)\
**Post date:** [May 22, 2017, 1:05pm UTC](https://discuss.elastic.co/t/matching-fields-in-a-if-then-manner/86670/1 "2017-05-22T13:05:09Z")

</div>

Hello,  
I have a user object with nested objects such as "stores" (favorite shopping stores) inside which we have details such as the id of the store, the brand of the store, country of the store, etc.

I want to come with a query for 2 separate scenarios but have not been successful:

1. People who visit only Brand B in Country C1.
2. People who visit only store 1 for brand B in C1. (This should include people who go to brand d in store 2 but not those who go to brand B in store 2)

Any leads?

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 26, 2017, 8:25am UTC](https://discuss.elastic.co/t/matching-fields-in-a-if-then-manner/86670/2 "2017-05-26T08:25:05Z")

</div>

Your constraints aren't terribly clear, so I've reformulated the first one as: People who, in country C1, visit only Brand B and no other brands. It's OK for them to visit Brand B or any other brands in a different country.

Here's an example:

```auto
PUT t 
{
  "mappings": {
    "t": {
      "properties": {
        "stores": {
          "type": "nested",
          "properties": {
            "brand": {
              "type": "keyword"
            },
            "country": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

PUT t/t/1
{
  "stores": [
    {
      "brand": "B",
      "country": "C1"
    },
    {
      "brand": "A",
      "country": "C1"
    }
  ]
}

PUT t/t/2
{
  "stores": [
    {
      "brand": "B",
      "country": "C1"
    },
    {
      "brand": "A",
      "country": "C2"
    }
  ]
}

GET t/_search
{
  "query": {
    "bool": {
      "filter": {
        "nested": {
          "path": "stores",
          "query": {
            "bool": {
              "filter": [
                {
                  "match": {
                    "stores.brand": "B"
                  }
                },
                {
                  "match": {
                    "stores.country": "C1"
                  }
                }
              ]
            }
          }
        }
      },
      "must_not": {
        "nested": {
          "path": "stores",
          "query": {
            "bool": {
              "filter": {
                "match": {
                  "stores.country": "C1"
                }
              },
              "must_not": {
                "match": {
                  "stores.brand": "B"
                }
              }
            }
          }
        }
      }
    }
  }
}

```

---

<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:** [June 23, 2017, 8:25am UTC](https://discuss.elastic.co/t/matching-fields-in-a-if-then-manner/86670/3 "2017-06-23T08:25:29Z")

</div>

Closed on June 23, 2017, 8:25am UTC

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