# Can anybody help me with a difficult query?

**URL:** <https://discuss.elastic.co/t/can-anybody-help-me-with-a-difficult-query/136889>\
**Category:** Elasticsearch\
**Created:** [June 21, 2018, 1:48pm UTC](https://discuss.elastic.co/t/can-anybody-help-me-with-a-difficult-query/136889 "2018-06-21T13:48:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![magorbe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magorbe/32/36871_2.png) [@magorbe](https://discuss.elastic.co/u/magorbe)\
**Post date:** [June 21, 2018, 1:48pm UTC](https://discuss.elastic.co/t/can-anybody-help-me-with-a-difficult-query/136889/1 "2018-06-21T13:48:50Z")

</div>

Hello !! I have this mapping :

```
"c_index": {
    "aliases": {},
    "mappings": {
        "an": {
            "properties": {
                "id": {
                    "type": "string"
                },
                "sm": {
                    "type": "nested",
                    "properties": {
                        "cr": {
                            "type": "nested",
                            "properties": {
                                "c": {
                                    "type": "string"
                                },
                                "e": {
                                    "type": "long"
                                },
                                "id": {
                                    "type": "string"
                                },
                                "s": {
                                    "type": "long"
                                }
                            }
                        },
                        "id": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }

```

And I need a query than gives me all the cr's when:

an.id == x and sm.id == y

I tried with :

```
{ 
  "query": {
    "bool": {
      "filter": [
        { "term": { "_id": "x" } },
        { "term": { "sm.id": "y" } }
      ]
    }
  }
}

```

And there is no error, but returns zero elements, and I am sure that there is one element for the query... If I do :

> { "query": { "bool": { "filter": [{ "term": { "\_id": "x" } }] } } }

Everything is OK, but the problem is when i tried to add the second term... Any idea ? Thanks!

---

<div class="post-metadata">

**Author:** ![RahulD](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@RahulD](https://discuss.elastic.co/u/RahulD)\
**Post date:** [June 21, 2018, 8:15pm UTC](https://discuss.elastic.co/t/can-anybody-help-me-with-a-difficult-query/136889/2 "2018-06-21T20:15:35Z")

</div>

You need to use bool query with a must block:

Something like this

```
{
  "size" : 0,
  "query" : {
    "bool": {
      "must": [
        {
          "match": {
            "id": "X"
          }
        },
        {
          "nested": {
            "path": "sm",
            "query": {
              "match": {
                "sm.id": "Y"
              }
            }
          }
        }
      ]
    }
  }
}

```

Hopefully that helps.

---

<div class="post-metadata">

**Author:** ![magorbe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magorbe/32/36871_2.png) [@magorbe](https://discuss.elastic.co/u/magorbe)\
**Post date:** [June 22, 2018, 7:51am UTC](https://discuss.elastic.co/t/can-anybody-help-me-with-a-difficult-query/136889/3 "2018-06-22T07:51:02Z")

</div>

thanks!!! It works fine but I have a problem... the query returns the content of all the 'an's' with id=X that has a 'sm' wit id = Y, but I don't need all the content of the "an", I need only the info of the sm's with id=Y, it's possible?? Thanks again!

---

<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:** [July 20, 2018, 7:51am UTC](https://discuss.elastic.co/t/can-anybody-help-me-with-a-difficult-query/136889/4 "2018-07-20T07:51:03Z")

</div>

Closed on July 20, 2018, 7:51am UTC

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